Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * dir.c |
| 3 | * |
| 4 | * Copyright (C) 1995, 1996 by Volker Lendecke |
| 5 | * Modified for big endian by J.F. Chadima and David S. Miller |
| 6 | * Modified 1997 Peter Waltenberg, Bill Hawes, David Woodhouse for 2.1 dcache |
| 7 | * Modified 1998, 1999 Wolfram Pienkoss for NLS |
| 8 | * Modified 1999 Wolfram Pienkoss for directory caching |
| 9 | * Modified 2000 Ben Harris, University of Cambridge for NFS NS meta-info |
| 10 | * |
| 11 | */ |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
| 14 | #include <linux/time.h> |
| 15 | #include <linux/errno.h> |
| 16 | #include <linux/stat.h> |
| 17 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/vmalloc.h> |
| 19 | #include <linux/mm.h> |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 20 | #include <linux/namei.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 21 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/byteorder.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Al Viro | 32c419d | 2011-01-12 17:37:47 -0500 | [diff] [blame] | 24 | #include "ncp_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 26 | static void ncp_read_volume_list(struct file *, struct dir_context *, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | struct ncp_cache_control *); |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 28 | static void ncp_do_readdir(struct file *, struct dir_context *, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | struct ncp_cache_control *); |
| 30 | |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 31 | static int ncp_readdir(struct file *, struct dir_context *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 33 | static int ncp_create(struct inode *, struct dentry *, umode_t, bool); |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 34 | static struct dentry *ncp_lookup(struct inode *, struct dentry *, unsigned int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | static int ncp_unlink(struct inode *, struct dentry *); |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 36 | static int ncp_mkdir(struct inode *, struct dentry *, umode_t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | static int ncp_rmdir(struct inode *, struct dentry *); |
| 38 | static int ncp_rename(struct inode *, struct dentry *, |
Miklos Szeredi | 1cd66c9 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 39 | struct inode *, struct dentry *, unsigned int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | static int ncp_mknod(struct inode * dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 41 | umode_t mode, dev_t rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #if defined(CONFIG_NCPFS_EXTRAS) || defined(CONFIG_NCPFS_NFS_NS) |
| 43 | extern int ncp_symlink(struct inode *, struct dentry *, const char *); |
| 44 | #else |
| 45 | #define ncp_symlink NULL |
| 46 | #endif |
| 47 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 48 | const struct file_operations ncp_dir_operations = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
jan Blunck | ca57272 | 2010-05-26 14:44:53 -0700 | [diff] [blame] | 50 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | .read = generic_read_dir, |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 52 | .iterate = ncp_readdir, |
John Kacur | 93d84b6 | 2010-05-05 15:15:37 +0200 | [diff] [blame] | 53 | .unlocked_ioctl = ncp_ioctl, |
Petr Vandrovec | 54f67f6 | 2006-09-30 23:27:55 -0700 | [diff] [blame] | 54 | #ifdef CONFIG_COMPAT |
| 55 | .compat_ioctl = ncp_compat_ioctl, |
| 56 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 59 | const struct inode_operations ncp_dir_inode_operations = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | { |
| 61 | .create = ncp_create, |
| 62 | .lookup = ncp_lookup, |
| 63 | .unlink = ncp_unlink, |
| 64 | .symlink = ncp_symlink, |
| 65 | .mkdir = ncp_mkdir, |
| 66 | .rmdir = ncp_rmdir, |
| 67 | .mknod = ncp_mknod, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 68 | .rename = ncp_rename, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | .setattr = ncp_notify_change, |
| 70 | }; |
| 71 | |
| 72 | /* |
| 73 | * Dentry operations routines |
| 74 | */ |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 75 | static int ncp_lookup_validate(struct dentry *, unsigned int); |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 76 | static int ncp_hash_dentry(const struct dentry *, struct qstr *); |
Al Viro | 6fa67e7 | 2016-07-31 16:37:25 -0400 | [diff] [blame] | 77 | static int ncp_compare_dentry(const struct dentry *, |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 78 | unsigned int, const char *, const struct qstr *); |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 79 | static int ncp_delete_dentry(const struct dentry *); |
Al Viro | 5e993e2 | 2014-12-24 21:41:47 -0500 | [diff] [blame] | 80 | static void ncp_d_prune(struct dentry *dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Al Viro | 0378c40 | 2011-01-12 17:25:03 -0500 | [diff] [blame] | 82 | const struct dentry_operations ncp_dentry_operations = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | { |
| 84 | .d_revalidate = ncp_lookup_validate, |
| 85 | .d_hash = ncp_hash_dentry, |
| 86 | .d_compare = ncp_compare_dentry, |
| 87 | .d_delete = ncp_delete_dentry, |
Al Viro | 5e993e2 | 2014-12-24 21:41:47 -0500 | [diff] [blame] | 88 | .d_prune = ncp_d_prune, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | }; |
| 90 | |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 91 | #define ncp_namespace(i) (NCP_SERVER(i)->name_space[NCP_FINFO(i)->volNumber]) |
| 92 | |
| 93 | static inline int ncp_preserve_entry_case(struct inode *i, __u32 nscreator) |
| 94 | { |
| 95 | #ifdef CONFIG_NCPFS_SMALLDOS |
| 96 | int ns = ncp_namespace(i); |
| 97 | |
| 98 | if ((ns == NW_NS_DOS) |
| 99 | #ifdef CONFIG_NCPFS_OS2_NS |
| 100 | || ((ns == NW_NS_OS2) && (nscreator == NW_NS_DOS)) |
| 101 | #endif /* CONFIG_NCPFS_OS2_NS */ |
| 102 | ) |
| 103 | return 0; |
| 104 | #endif /* CONFIG_NCPFS_SMALLDOS */ |
| 105 | return 1; |
| 106 | } |
| 107 | |
| 108 | #define ncp_preserve_case(i) (ncp_namespace(i) != NW_NS_DOS) |
| 109 | |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 110 | static inline int ncp_case_sensitive(const struct inode *i) |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 111 | { |
| 112 | #ifdef CONFIG_NCPFS_NFS_NS |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 113 | return ncp_namespace(i) == NW_NS_NFS; |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 114 | #else |
| 115 | return 0; |
| 116 | #endif /* CONFIG_NCPFS_NFS_NS */ |
| 117 | } |
| 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | /* |
| 120 | * Note: leave the hash unchanged if the directory |
| 121 | * is case-sensitive. |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 122 | * |
| 123 | * Accessing the parent inode can be racy under RCU pathwalking. |
| 124 | * Use ACCESS_ONCE() to make sure we use _one_ particular inode, |
| 125 | * the callers will handle races. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | */ |
| 127 | static int |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 128 | ncp_hash_dentry(const struct dentry *dentry, struct qstr *this) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 130 | struct inode *inode = d_inode_rcu(dentry); |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 131 | |
| 132 | if (!inode) |
| 133 | return 0; |
| 134 | |
Nick Piggin | b1e6a01 | 2011-01-07 17:49:28 +1100 | [diff] [blame] | 135 | if (!ncp_case_sensitive(inode)) { |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 136 | struct nls_table *t; |
| 137 | unsigned long hash; |
| 138 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Miklos Szeredi | 9a232de | 2016-09-27 11:03:57 +0200 | [diff] [blame] | 140 | t = NCP_IO_TABLE(dentry->d_sb); |
Linus Torvalds | 8387ff2 | 2016-06-10 07:51:30 -0700 | [diff] [blame] | 141 | hash = init_name_hash(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | for (i=0; i<this->len ; i++) |
| 143 | hash = partial_name_hash(ncp_tolower(t, this->name[i]), |
| 144 | hash); |
| 145 | this->hash = end_name_hash(hash); |
| 146 | } |
| 147 | return 0; |
| 148 | } |
| 149 | |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 150 | /* |
| 151 | * Accessing the parent inode can be racy under RCU pathwalking. |
| 152 | * Use ACCESS_ONCE() to make sure we use _one_ particular inode, |
| 153 | * the callers will handle races. |
| 154 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | static int |
Al Viro | 6fa67e7 | 2016-07-31 16:37:25 -0400 | [diff] [blame] | 156 | ncp_compare_dentry(const struct dentry *dentry, |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 157 | unsigned int len, const char *str, const struct qstr *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 159 | struct inode *pinode; |
| 160 | |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 161 | if (len != name->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | return 1; |
| 163 | |
Al Viro | 6fa67e7 | 2016-07-31 16:37:25 -0400 | [diff] [blame] | 164 | pinode = d_inode_rcu(dentry->d_parent); |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 165 | if (!pinode) |
| 166 | return 1; |
| 167 | |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 168 | if (ncp_case_sensitive(pinode)) |
| 169 | return strncmp(str, name->name, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 171 | return ncp_strnicmp(NCP_IO_TABLE(pinode->i_sb), str, name->name, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /* |
| 175 | * This is the callback from dput() when d_count is going to 0. |
| 176 | * We use this to unhash dentries with bad inodes. |
| 177 | * Closing files can be safely postponed until iput() - it's done there anyway. |
| 178 | */ |
| 179 | static int |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 180 | ncp_delete_dentry(const struct dentry * dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 182 | struct inode *inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
| 184 | if (inode) { |
| 185 | if (is_bad_inode(inode)) |
| 186 | return 1; |
| 187 | } else |
| 188 | { |
| 189 | /* N.B. Unhash negative dentries? */ |
| 190 | } |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static inline int |
| 195 | ncp_single_volume(struct ncp_server *server) |
| 196 | { |
| 197 | return (server->m.mounted_vol[0] != '\0'); |
| 198 | } |
| 199 | |
| 200 | static inline int ncp_is_server_root(struct inode *inode) |
| 201 | { |
Al Viro | a740022 | 2014-10-21 15:20:42 -0400 | [diff] [blame] | 202 | return !ncp_single_volume(NCP_SERVER(inode)) && |
| 203 | is_root_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | |
| 207 | /* |
| 208 | * This is the callback when the dcache has a lookup hit. |
| 209 | */ |
| 210 | |
| 211 | |
| 212 | #ifdef CONFIG_NCPFS_STRONG |
| 213 | /* try to delete a readonly file (NW R bit set) */ |
| 214 | |
| 215 | static int |
| 216 | ncp_force_unlink(struct inode *dir, struct dentry* dentry) |
| 217 | { |
| 218 | int res=0x9c,res2; |
| 219 | struct nw_modify_dos_info info; |
| 220 | __le32 old_nwattr; |
| 221 | struct inode *inode; |
| 222 | |
| 223 | memset(&info, 0, sizeof(info)); |
| 224 | |
| 225 | /* remove the Read-Only flag on the NW server */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 226 | inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
| 228 | old_nwattr = NCP_FINFO(inode)->nwattr; |
| 229 | info.attributes = old_nwattr & ~(aRONLY|aDELETEINHIBIT|aRENAMEINHIBIT); |
| 230 | res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(inode), inode, NULL, DM_ATTRIBUTES, &info); |
| 231 | if (res2) |
| 232 | goto leave_me; |
| 233 | |
| 234 | /* now try again the delete operation */ |
| 235 | res = ncp_del_file_or_subdir2(NCP_SERVER(dir), dentry); |
| 236 | |
| 237 | if (res) /* delete failed, set R bit again */ |
| 238 | { |
| 239 | info.attributes = old_nwattr; |
| 240 | res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(inode), inode, NULL, DM_ATTRIBUTES, &info); |
| 241 | if (res2) |
| 242 | goto leave_me; |
| 243 | } |
| 244 | leave_me: |
| 245 | return(res); |
| 246 | } |
| 247 | #endif /* CONFIG_NCPFS_STRONG */ |
| 248 | |
| 249 | #ifdef CONFIG_NCPFS_STRONG |
| 250 | static int |
| 251 | ncp_force_rename(struct inode *old_dir, struct dentry* old_dentry, char *_old_name, |
| 252 | struct inode *new_dir, struct dentry* new_dentry, char *_new_name) |
| 253 | { |
| 254 | struct nw_modify_dos_info info; |
| 255 | int res=0x90,res2; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 256 | struct inode *old_inode = d_inode(old_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | __le32 old_nwattr = NCP_FINFO(old_inode)->nwattr; |
| 258 | __le32 new_nwattr = 0; /* shut compiler warning */ |
| 259 | int old_nwattr_changed = 0; |
| 260 | int new_nwattr_changed = 0; |
| 261 | |
| 262 | memset(&info, 0, sizeof(info)); |
| 263 | |
| 264 | /* remove the Read-Only flag on the NW server */ |
| 265 | |
| 266 | info.attributes = old_nwattr & ~(aRONLY|aRENAMEINHIBIT|aDELETEINHIBIT); |
| 267 | res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(old_inode), old_inode, NULL, DM_ATTRIBUTES, &info); |
| 268 | if (!res2) |
| 269 | old_nwattr_changed = 1; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 270 | if (new_dentry && d_really_is_positive(new_dentry)) { |
| 271 | new_nwattr = NCP_FINFO(d_inode(new_dentry))->nwattr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | info.attributes = new_nwattr & ~(aRONLY|aRENAMEINHIBIT|aDELETEINHIBIT); |
| 273 | res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(new_dir), new_dir, _new_name, DM_ATTRIBUTES, &info); |
| 274 | if (!res2) |
| 275 | new_nwattr_changed = 1; |
| 276 | } |
| 277 | /* now try again the rename operation */ |
| 278 | /* but only if something really happened */ |
| 279 | if (new_nwattr_changed || old_nwattr_changed) { |
| 280 | res = ncp_ren_or_mov_file_or_subdir(NCP_SERVER(old_dir), |
| 281 | old_dir, _old_name, |
| 282 | new_dir, _new_name); |
| 283 | } |
| 284 | if (res) |
| 285 | goto leave_me; |
| 286 | /* file was successfully renamed, so: |
| 287 | do not set attributes on old file - it no longer exists |
| 288 | copy attributes from old file to new */ |
| 289 | new_nwattr_changed = old_nwattr_changed; |
| 290 | new_nwattr = old_nwattr; |
| 291 | old_nwattr_changed = 0; |
| 292 | |
| 293 | leave_me:; |
| 294 | if (old_nwattr_changed) { |
| 295 | info.attributes = old_nwattr; |
| 296 | res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(old_inode), old_inode, NULL, DM_ATTRIBUTES, &info); |
| 297 | /* ignore errors */ |
| 298 | } |
| 299 | if (new_nwattr_changed) { |
| 300 | info.attributes = new_nwattr; |
| 301 | res2 = ncp_modify_file_or_subdir_dos_info_path(NCP_SERVER(new_dir), new_dir, _new_name, DM_ATTRIBUTES, &info); |
| 302 | /* ignore errors */ |
| 303 | } |
| 304 | return(res); |
| 305 | } |
| 306 | #endif /* CONFIG_NCPFS_STRONG */ |
| 307 | |
| 308 | |
| 309 | static int |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 310 | ncp_lookup_validate(struct dentry *dentry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | { |
| 312 | struct ncp_server *server; |
| 313 | struct dentry *parent; |
| 314 | struct inode *dir; |
| 315 | struct ncp_entry_info finfo; |
| 316 | int res, val = 0, len; |
| 317 | __u8 __name[NCP_MAXPATHLEN + 1]; |
| 318 | |
Al Viro | 0378c40 | 2011-01-12 17:25:03 -0500 | [diff] [blame] | 319 | if (dentry == dentry->d_sb->s_root) |
| 320 | return 1; |
| 321 | |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 322 | if (flags & LOOKUP_RCU) |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 323 | return -ECHILD; |
| 324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | parent = dget_parent(dentry); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 326 | dir = d_inode(parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 328 | if (d_really_is_negative(dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | goto finished; |
| 330 | |
| 331 | server = NCP_SERVER(dir); |
| 332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | /* |
| 334 | * Inspired by smbfs: |
| 335 | * The default validation is based on dentry age: |
| 336 | * We set the max age at mount time. (But each |
| 337 | * successful server lookup renews the timestamp.) |
| 338 | */ |
| 339 | val = NCP_TEST_AGE(server, dentry); |
| 340 | if (val) |
| 341 | goto finished; |
| 342 | |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 343 | ncp_dbg(2, "%pd2 not valid, age=%ld, server lookup\n", |
Al Viro | 84eb353 | 2013-09-16 10:59:55 -0400 | [diff] [blame] | 344 | dentry, NCP_GET_AGE(dentry)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
| 346 | len = sizeof(__name); |
| 347 | if (ncp_is_server_root(dir)) { |
| 348 | res = ncp_io2vol(server, __name, &len, dentry->d_name.name, |
| 349 | dentry->d_name.len, 1); |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 350 | if (!res) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | res = ncp_lookup_volume(server, __name, &(finfo.i)); |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 352 | if (!res) |
| 353 | ncp_update_known_namespace(server, finfo.i.volNumber, NULL); |
| 354 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | } else { |
| 356 | res = ncp_io2vol(server, __name, &len, dentry->d_name.name, |
| 357 | dentry->d_name.len, !ncp_preserve_case(dir)); |
| 358 | if (!res) |
| 359 | res = ncp_obtain_info(server, dir, __name, &(finfo.i)); |
| 360 | } |
| 361 | finfo.volume = finfo.i.volNumber; |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 362 | ncp_dbg(2, "looked for %pd/%s, res=%d\n", |
Al Viro | 84eb353 | 2013-09-16 10:59:55 -0400 | [diff] [blame] | 363 | dentry->d_parent, __name, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | /* |
| 365 | * If we didn't find it, or if it has a different dirEntNum to |
| 366 | * what we remember, it's not valid any more. |
| 367 | */ |
| 368 | if (!res) { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 369 | struct inode *inode = d_inode(dentry); |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 370 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 371 | inode_lock(inode); |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 372 | if (finfo.i.dirEntNum == NCP_FINFO(inode)->dirEntNum) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | ncp_new_dentry(dentry); |
| 374 | val=1; |
| 375 | } else |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 376 | ncp_dbg(2, "found, but dirEntNum changed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 378 | ncp_update_inode2(inode, &finfo); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 379 | inode_unlock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | finished: |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 383 | ncp_dbg(2, "result=%d\n", val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | dput(parent); |
| 385 | return val; |
| 386 | } |
| 387 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | static time_t ncp_obtain_mtime(struct dentry *dentry) |
| 389 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 390 | struct inode *inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | struct ncp_server *server = NCP_SERVER(inode); |
| 392 | struct nw_info_struct i; |
| 393 | |
| 394 | if (!ncp_conn_valid(server) || ncp_is_server_root(inode)) |
| 395 | return 0; |
| 396 | |
| 397 | if (ncp_obtain_info(server, inode, NULL, &i)) |
| 398 | return 0; |
| 399 | |
| 400 | return ncp_date_dos2unix(i.modifyTime, i.modifyDate); |
| 401 | } |
| 402 | |
Al Viro | 5e993e2 | 2014-12-24 21:41:47 -0500 | [diff] [blame] | 403 | static inline void |
| 404 | ncp_invalidate_dircache_entries(struct dentry *parent) |
| 405 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 406 | struct ncp_server *server = NCP_SERVER(d_inode(parent)); |
Al Viro | 5e993e2 | 2014-12-24 21:41:47 -0500 | [diff] [blame] | 407 | struct dentry *dentry; |
| 408 | |
| 409 | spin_lock(&parent->d_lock); |
| 410 | list_for_each_entry(dentry, &parent->d_subdirs, d_child) { |
| 411 | dentry->d_fsdata = NULL; |
| 412 | ncp_age_dentry(server, dentry); |
| 413 | } |
| 414 | spin_unlock(&parent->d_lock); |
| 415 | } |
| 416 | |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 417 | static int ncp_readdir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | { |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 419 | struct dentry *dentry = file->f_path.dentry; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 420 | struct inode *inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | struct page *page = NULL; |
| 422 | struct ncp_server *server = NCP_SERVER(inode); |
| 423 | union ncp_dir_cache *cache = NULL; |
| 424 | struct ncp_cache_control ctl; |
| 425 | int result, mtime_valid = 0; |
| 426 | time_t mtime = 0; |
| 427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | ctl.page = NULL; |
| 429 | ctl.cache = NULL; |
| 430 | |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 431 | ncp_dbg(2, "reading %pD2, pos=%d\n", file, (int)ctx->pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | |
| 433 | result = -EIO; |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 434 | /* Do not generate '.' and '..' when server is dead. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | if (!ncp_conn_valid(server)) |
| 436 | goto out; |
| 437 | |
| 438 | result = 0; |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 439 | if (!dir_emit_dots(file, ctx)) |
| 440 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
| 442 | page = grab_cache_page(&inode->i_data, 0); |
| 443 | if (!page) |
| 444 | goto read_really; |
| 445 | |
| 446 | ctl.cache = cache = kmap(page); |
| 447 | ctl.head = cache->head; |
| 448 | |
| 449 | if (!PageUptodate(page) || !ctl.head.eof) |
| 450 | goto init_cache; |
| 451 | |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 452 | if (ctx->pos == 2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | if (jiffies - ctl.head.time >= NCP_MAX_AGE(server)) |
| 454 | goto init_cache; |
| 455 | |
| 456 | mtime = ncp_obtain_mtime(dentry); |
| 457 | mtime_valid = 1; |
| 458 | if ((!mtime) || (mtime != ctl.head.mtime)) |
| 459 | goto init_cache; |
| 460 | } |
| 461 | |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 462 | if (ctx->pos > ctl.head.end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | goto finished; |
| 464 | |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 465 | ctl.fpos = ctx->pos + (NCP_DIRCACHE_START - 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | ctl.ofs = ctl.fpos / NCP_DIRCACHE_SIZE; |
| 467 | ctl.idx = ctl.fpos % NCP_DIRCACHE_SIZE; |
| 468 | |
| 469 | for (;;) { |
| 470 | if (ctl.ofs != 0) { |
| 471 | ctl.page = find_lock_page(&inode->i_data, ctl.ofs); |
| 472 | if (!ctl.page) |
| 473 | goto invalid_cache; |
| 474 | ctl.cache = kmap(ctl.page); |
| 475 | if (!PageUptodate(ctl.page)) |
| 476 | goto invalid_cache; |
| 477 | } |
| 478 | while (ctl.idx < NCP_DIRCACHE_SIZE) { |
| 479 | struct dentry *dent; |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 480 | bool over; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | |
Al Viro | 5e993e2 | 2014-12-24 21:41:47 -0500 | [diff] [blame] | 482 | spin_lock(&dentry->d_lock); |
| 483 | if (!(NCP_FINFO(inode)->flags & NCPI_DIR_CACHE)) { |
| 484 | spin_unlock(&dentry->d_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | goto invalid_cache; |
Al Viro | 5e993e2 | 2014-12-24 21:41:47 -0500 | [diff] [blame] | 486 | } |
| 487 | dent = ctl.cache->dentry[ctl.idx]; |
| 488 | if (unlikely(!lockref_get_not_dead(&dent->d_lockref))) { |
| 489 | spin_unlock(&dentry->d_lock); |
| 490 | goto invalid_cache; |
| 491 | } |
| 492 | spin_unlock(&dentry->d_lock); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 493 | if (d_really_is_negative(dent)) { |
Al Viro | 5e993e2 | 2014-12-24 21:41:47 -0500 | [diff] [blame] | 494 | dput(dent); |
| 495 | goto invalid_cache; |
| 496 | } |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 497 | over = !dir_emit(ctx, dent->d_name.name, |
| 498 | dent->d_name.len, |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 499 | d_inode(dent)->i_ino, DT_UNKNOWN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | dput(dent); |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 501 | if (over) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | goto finished; |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 503 | ctx->pos += 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | ctl.idx += 1; |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 505 | if (ctx->pos > ctl.head.end) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | goto finished; |
| 507 | } |
| 508 | if (ctl.page) { |
| 509 | kunmap(ctl.page); |
| 510 | SetPageUptodate(ctl.page); |
| 511 | unlock_page(ctl.page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 512 | put_page(ctl.page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | ctl.page = NULL; |
| 514 | } |
| 515 | ctl.idx = 0; |
| 516 | ctl.ofs += 1; |
| 517 | } |
| 518 | invalid_cache: |
| 519 | if (ctl.page) { |
| 520 | kunmap(ctl.page); |
| 521 | unlock_page(ctl.page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 522 | put_page(ctl.page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | ctl.page = NULL; |
| 524 | } |
| 525 | ctl.cache = cache; |
| 526 | init_cache: |
| 527 | ncp_invalidate_dircache_entries(dentry); |
| 528 | if (!mtime_valid) { |
| 529 | mtime = ncp_obtain_mtime(dentry); |
| 530 | mtime_valid = 1; |
| 531 | } |
| 532 | ctl.head.mtime = mtime; |
| 533 | ctl.head.time = jiffies; |
| 534 | ctl.head.eof = 0; |
| 535 | ctl.fpos = 2; |
| 536 | ctl.ofs = 0; |
| 537 | ctl.idx = NCP_DIRCACHE_START; |
| 538 | ctl.filled = 0; |
| 539 | ctl.valid = 1; |
| 540 | read_really: |
Al Viro | 5e993e2 | 2014-12-24 21:41:47 -0500 | [diff] [blame] | 541 | spin_lock(&dentry->d_lock); |
| 542 | NCP_FINFO(inode)->flags |= NCPI_DIR_CACHE; |
| 543 | spin_unlock(&dentry->d_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | if (ncp_is_server_root(inode)) { |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 545 | ncp_read_volume_list(file, ctx, &ctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | } else { |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 547 | ncp_do_readdir(file, ctx, &ctl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | } |
| 549 | ctl.head.end = ctl.fpos - 1; |
| 550 | ctl.head.eof = ctl.valid; |
| 551 | finished: |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 552 | if (ctl.page) { |
| 553 | kunmap(ctl.page); |
| 554 | SetPageUptodate(ctl.page); |
| 555 | unlock_page(ctl.page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 556 | put_page(ctl.page); |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 557 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | if (page) { |
| 559 | cache->head = ctl.head; |
| 560 | kunmap(page); |
| 561 | SetPageUptodate(page); |
| 562 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 563 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | return result; |
| 567 | } |
| 568 | |
Al Viro | 5e993e2 | 2014-12-24 21:41:47 -0500 | [diff] [blame] | 569 | static void ncp_d_prune(struct dentry *dentry) |
| 570 | { |
| 571 | if (!dentry->d_fsdata) /* not referenced from page cache */ |
| 572 | return; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 573 | NCP_FINFO(d_inode(dentry->d_parent))->flags &= ~NCPI_DIR_CACHE; |
Al Viro | 5e993e2 | 2014-12-24 21:41:47 -0500 | [diff] [blame] | 574 | } |
| 575 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | static int |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 577 | ncp_fill_cache(struct file *file, struct dir_context *ctx, |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 578 | struct ncp_cache_control *ctrl, struct ncp_entry_info *entry, |
| 579 | int inval_childs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | { |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 581 | struct dentry *newdent, *dentry = file->f_path.dentry; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 582 | struct inode *dir = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | struct ncp_cache_control ctl = *ctrl; |
| 584 | struct qstr qname; |
| 585 | int valid = 0; |
| 586 | int hashed = 0; |
| 587 | ino_t ino = 0; |
| 588 | __u8 __name[NCP_MAXPATHLEN + 1]; |
| 589 | |
| 590 | qname.len = sizeof(__name); |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 591 | if (ncp_vol2io(NCP_SERVER(dir), __name, &qname.len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | entry->i.entryName, entry->i.nameLen, |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 593 | !ncp_preserve_entry_case(dir, entry->i.NSCreator))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | return 1; /* I'm not sure */ |
| 595 | |
| 596 | qname.name = __name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
Al Viro | 4f522a2 | 2013-02-11 23:20:37 -0500 | [diff] [blame] | 598 | newdent = d_hash_and_lookup(dentry, &qname); |
Viresh Kumar | a1c8368 | 2015-08-12 15:59:44 +0530 | [diff] [blame] | 599 | if (IS_ERR(newdent)) |
Al Viro | 4f522a2 | 2013-02-11 23:20:37 -0500 | [diff] [blame] | 600 | goto end_advance; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | if (!newdent) { |
| 602 | newdent = d_alloc(dentry, &qname); |
| 603 | if (!newdent) |
| 604 | goto end_advance; |
| 605 | } else { |
| 606 | hashed = 1; |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 607 | |
| 608 | /* If case sensitivity changed for this volume, all entries below this one |
| 609 | should be thrown away. This entry itself is not affected, as its case |
| 610 | sensitivity is controlled by its own parent. */ |
| 611 | if (inval_childs) |
| 612 | shrink_dcache_parent(newdent); |
| 613 | |
| 614 | /* |
Nick Piggin | fb2d5b8 | 2011-01-07 17:49:26 +1100 | [diff] [blame] | 615 | * NetWare's OS2 namespace is case preserving yet case |
| 616 | * insensitive. So we update dentry's name as received from |
| 617 | * server. Parent dir's i_mutex is locked because we're in |
| 618 | * readdir. |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 619 | */ |
Nick Piggin | fb2d5b8 | 2011-01-07 17:49:26 +1100 | [diff] [blame] | 620 | dentry_update_name_case(newdent, &qname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | } |
| 622 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 623 | if (d_really_is_negative(newdent)) { |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 624 | struct inode *inode; |
| 625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | entry->opened = 0; |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 627 | entry->ino = iunique(dir->i_sb, 2); |
| 628 | inode = ncp_iget(dir->i_sb, entry); |
| 629 | if (inode) { |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 630 | d_instantiate(newdent, inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | if (!hashed) |
| 632 | d_rehash(newdent); |
Al Viro | 5e993e2 | 2014-12-24 21:41:47 -0500 | [diff] [blame] | 633 | } else { |
| 634 | spin_lock(&dentry->d_lock); |
Al Viro | 803c001 | 2016-03-07 22:17:07 -0500 | [diff] [blame] | 635 | NCP_FINFO(dir)->flags &= ~NCPI_DIR_CACHE; |
Al Viro | 5e993e2 | 2014-12-24 21:41:47 -0500 | [diff] [blame] | 636 | spin_unlock(&dentry->d_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | } |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 638 | } else { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 639 | struct inode *inode = d_inode(newdent); |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 640 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 641 | inode_lock_nested(inode, I_MUTEX_CHILD); |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 642 | ncp_update_inode2(inode, entry); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 643 | inode_unlock(inode); |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 644 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | if (ctl.idx >= NCP_DIRCACHE_SIZE) { |
| 647 | if (ctl.page) { |
| 648 | kunmap(ctl.page); |
| 649 | SetPageUptodate(ctl.page); |
| 650 | unlock_page(ctl.page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 651 | put_page(ctl.page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | } |
| 653 | ctl.cache = NULL; |
| 654 | ctl.idx -= NCP_DIRCACHE_SIZE; |
| 655 | ctl.ofs += 1; |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 656 | ctl.page = grab_cache_page(&dir->i_data, ctl.ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | if (ctl.page) |
| 658 | ctl.cache = kmap(ctl.page); |
| 659 | } |
| 660 | if (ctl.cache) { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 661 | if (d_really_is_positive(newdent)) { |
Al Viro | 5e993e2 | 2014-12-24 21:41:47 -0500 | [diff] [blame] | 662 | newdent->d_fsdata = newdent; |
| 663 | ctl.cache->dentry[ctl.idx] = newdent; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 664 | ino = d_inode(newdent)->i_ino; |
Al Viro | 5e993e2 | 2014-12-24 21:41:47 -0500 | [diff] [blame] | 665 | ncp_new_dentry(newdent); |
| 666 | } |
| 667 | valid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | } |
| 669 | dput(newdent); |
| 670 | end_advance: |
| 671 | if (!valid) |
| 672 | ctl.valid = 0; |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 673 | if (!ctl.filled && (ctl.fpos == ctx->pos)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | if (!ino) |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 675 | ino = iunique(dir->i_sb, 2); |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 676 | ctl.filled = !dir_emit(ctx, qname.name, qname.len, |
| 677 | ino, DT_UNKNOWN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | if (!ctl.filled) |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 679 | ctx->pos += 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | } |
| 681 | ctl.fpos += 1; |
| 682 | ctl.idx += 1; |
| 683 | *ctrl = ctl; |
| 684 | return (ctl.valid || !ctl.filled); |
| 685 | } |
| 686 | |
| 687 | static void |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 688 | ncp_read_volume_list(struct file *file, struct dir_context *ctx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | struct ncp_cache_control *ctl) |
| 690 | { |
Al Viro | a67f797 | 2014-10-31 02:41:28 -0400 | [diff] [blame] | 691 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | struct ncp_server *server = NCP_SERVER(inode); |
| 693 | struct ncp_volume_info info; |
| 694 | struct ncp_entry_info entry; |
| 695 | int i; |
| 696 | |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 697 | ncp_dbg(1, "pos=%ld\n", (unsigned long)ctx->pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | |
| 699 | for (i = 0; i < NCP_NUMBER_OF_VOLUMES; i++) { |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 700 | int inval_dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | |
| 702 | if (ncp_get_volume_info_with_number(server, i, &info) != 0) |
| 703 | return; |
| 704 | if (!strlen(info.volume_name)) |
| 705 | continue; |
| 706 | |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 707 | ncp_dbg(1, "found vol: %s\n", info.volume_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | |
| 709 | if (ncp_lookup_volume(server, info.volume_name, |
| 710 | &entry.i)) { |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 711 | ncp_dbg(1, "could not lookup vol %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | info.volume_name); |
| 713 | continue; |
| 714 | } |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 715 | inval_dentry = ncp_update_known_namespace(server, entry.i.volNumber, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | entry.volume = entry.i.volNumber; |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 717 | if (!ncp_fill_cache(file, ctx, ctl, &entry, inval_dentry)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | return; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | static void |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 723 | ncp_do_readdir(struct file *file, struct dir_context *ctx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | struct ncp_cache_control *ctl) |
| 725 | { |
Al Viro | a67f797 | 2014-10-31 02:41:28 -0400 | [diff] [blame] | 726 | struct inode *dir = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | struct ncp_server *server = NCP_SERVER(dir); |
| 728 | struct nw_search_sequence seq; |
| 729 | struct ncp_entry_info entry; |
| 730 | int err; |
| 731 | void* buf; |
| 732 | int more; |
| 733 | size_t bufsize; |
| 734 | |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 735 | ncp_dbg(1, "%pD2, fpos=%ld\n", file, (unsigned long)ctx->pos); |
Joe Perches | e45ca8b | 2014-04-08 16:04:16 -0700 | [diff] [blame] | 736 | ncp_vdbg("init %pD, volnum=%d, dirent=%u\n", |
| 737 | file, NCP_FINFO(dir)->volNumber, NCP_FINFO(dir)->dirEntNum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | |
| 739 | err = ncp_initialize_search(server, dir, &seq); |
| 740 | if (err) { |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 741 | ncp_dbg(1, "init failed, err=%d\n", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | return; |
| 743 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | /* We MUST NOT use server->buffer_size handshaked with server if we are |
| 745 | using UDP, as for UDP server uses max. buffer size determined by |
| 746 | MTU, and for TCP server uses hardwired value 65KB (== 66560 bytes). |
| 747 | So we use 128KB, just to be sure, as there is no way how to know |
| 748 | this value in advance. */ |
| 749 | bufsize = 131072; |
| 750 | buf = vmalloc(bufsize); |
| 751 | if (!buf) |
| 752 | return; |
| 753 | do { |
| 754 | int cnt; |
| 755 | char* rpl; |
| 756 | size_t rpls; |
| 757 | |
| 758 | err = ncp_search_for_fileset(server, &seq, &more, &cnt, buf, bufsize, &rpl, &rpls); |
| 759 | if (err) /* Error */ |
| 760 | break; |
| 761 | if (!cnt) /* prevent endless loop */ |
| 762 | break; |
| 763 | while (cnt--) { |
| 764 | size_t onerpl; |
| 765 | |
| 766 | if (rpls < offsetof(struct nw_info_struct, entryName)) |
| 767 | break; /* short packet */ |
| 768 | ncp_extract_file_info(rpl, &entry.i); |
| 769 | onerpl = offsetof(struct nw_info_struct, entryName) + entry.i.nameLen; |
| 770 | if (rpls < onerpl) |
| 771 | break; /* short packet */ |
| 772 | (void)ncp_obtain_nfs_info(server, &entry.i); |
| 773 | rpl += onerpl; |
| 774 | rpls -= onerpl; |
| 775 | entry.volume = entry.i.volNumber; |
Al Viro | 76f582a | 2013-05-22 15:11:27 -0400 | [diff] [blame] | 776 | if (!ncp_fill_cache(file, ctx, ctl, &entry, 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | break; |
| 778 | } |
| 779 | } while (more); |
| 780 | vfree(buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | return; |
| 782 | } |
| 783 | |
| 784 | int ncp_conn_logged_in(struct super_block *sb) |
| 785 | { |
| 786 | struct ncp_server* server = NCP_SBP(sb); |
| 787 | int result; |
| 788 | |
| 789 | if (ncp_single_volume(server)) { |
| 790 | int len; |
| 791 | struct dentry* dent; |
| 792 | __u32 volNumber; |
| 793 | __le32 dirEntNum; |
| 794 | __le32 DosDirNum; |
| 795 | __u8 __name[NCP_MAXPATHLEN + 1]; |
| 796 | |
| 797 | len = sizeof(__name); |
| 798 | result = ncp_io2vol(server, __name, &len, server->m.mounted_vol, |
| 799 | strlen(server->m.mounted_vol), 1); |
| 800 | if (result) |
| 801 | goto out; |
| 802 | result = -ENOENT; |
| 803 | if (ncp_get_volume_root(server, __name, &volNumber, &dirEntNum, &DosDirNum)) { |
Joe Perches | e45ca8b | 2014-04-08 16:04:16 -0700 | [diff] [blame] | 804 | ncp_vdbg("%s not found\n", server->m.mounted_vol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | goto out; |
| 806 | } |
| 807 | dent = sb->s_root; |
| 808 | if (dent) { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 809 | struct inode* ino = d_inode(dent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | if (ino) { |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 811 | ncp_update_known_namespace(server, volNumber, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | NCP_FINFO(ino)->volNumber = volNumber; |
| 813 | NCP_FINFO(ino)->dirEntNum = dirEntNum; |
| 814 | NCP_FINFO(ino)->DosDirNum = DosDirNum; |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 815 | result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | } else { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 817 | ncp_dbg(1, "d_inode(sb->s_root) == NULL!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | } |
| 819 | } else { |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 820 | ncp_dbg(1, "sb->s_root == NULL!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | } |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 822 | } else |
| 823 | result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | |
| 825 | out: |
| 826 | return result; |
| 827 | } |
| 828 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 829 | static struct dentry *ncp_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | { |
| 831 | struct ncp_server *server = NCP_SERVER(dir); |
| 832 | struct inode *inode = NULL; |
| 833 | struct ncp_entry_info finfo; |
| 834 | int error, res, len; |
| 835 | __u8 __name[NCP_MAXPATHLEN + 1]; |
| 836 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 | error = -EIO; |
| 838 | if (!ncp_conn_valid(server)) |
| 839 | goto finished; |
| 840 | |
Joe Perches | e45ca8b | 2014-04-08 16:04:16 -0700 | [diff] [blame] | 841 | ncp_vdbg("server lookup for %pd2\n", dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | |
| 843 | len = sizeof(__name); |
| 844 | if (ncp_is_server_root(dir)) { |
| 845 | res = ncp_io2vol(server, __name, &len, dentry->d_name.name, |
| 846 | dentry->d_name.len, 1); |
| 847 | if (!res) |
| 848 | res = ncp_lookup_volume(server, __name, &(finfo.i)); |
Dan Carpenter | ffddc5f | 2014-04-08 16:04:19 -0700 | [diff] [blame] | 849 | if (!res) |
| 850 | ncp_update_known_namespace(server, finfo.i.volNumber, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | } else { |
| 852 | res = ncp_io2vol(server, __name, &len, dentry->d_name.name, |
| 853 | dentry->d_name.len, !ncp_preserve_case(dir)); |
| 854 | if (!res) |
| 855 | res = ncp_obtain_info(server, dir, __name, &(finfo.i)); |
| 856 | } |
Joe Perches | e45ca8b | 2014-04-08 16:04:16 -0700 | [diff] [blame] | 857 | ncp_vdbg("looked for %pd2, res=%d\n", dentry, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | /* |
| 859 | * If we didn't find an entry, make a negative dentry. |
| 860 | */ |
| 861 | if (res) |
| 862 | goto add_entry; |
| 863 | |
| 864 | /* |
| 865 | * Create an inode for the entry. |
| 866 | */ |
| 867 | finfo.opened = 0; |
| 868 | finfo.ino = iunique(dir->i_sb, 2); |
| 869 | finfo.volume = finfo.i.volNumber; |
| 870 | error = -EACCES; |
| 871 | inode = ncp_iget(dir->i_sb, &finfo); |
| 872 | |
| 873 | if (inode) { |
| 874 | ncp_new_dentry(dentry); |
| 875 | add_entry: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | d_add(dentry, inode); |
| 877 | error = 0; |
| 878 | } |
| 879 | |
| 880 | finished: |
Joe Perches | e45ca8b | 2014-04-08 16:04:16 -0700 | [diff] [blame] | 881 | ncp_vdbg("result=%d\n", error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | return ERR_PTR(error); |
| 883 | } |
| 884 | |
| 885 | /* |
| 886 | * This code is common to create, mkdir, and mknod. |
| 887 | */ |
| 888 | static int ncp_instantiate(struct inode *dir, struct dentry *dentry, |
| 889 | struct ncp_entry_info *finfo) |
| 890 | { |
| 891 | struct inode *inode; |
| 892 | int error = -EINVAL; |
| 893 | |
| 894 | finfo->ino = iunique(dir->i_sb, 2); |
| 895 | inode = ncp_iget(dir->i_sb, finfo); |
| 896 | if (!inode) |
| 897 | goto out_close; |
| 898 | d_instantiate(dentry,inode); |
| 899 | error = 0; |
| 900 | out: |
| 901 | return error; |
| 902 | |
| 903 | out_close: |
Joe Perches | e45ca8b | 2014-04-08 16:04:16 -0700 | [diff] [blame] | 904 | ncp_vdbg("%pd2 failed, closing file\n", dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | ncp_close_file(NCP_SERVER(dir), finfo->file_handle); |
| 906 | goto out; |
| 907 | } |
| 908 | |
Al Viro | 5eee25c | 2011-07-26 03:12:16 -0400 | [diff] [blame] | 909 | int ncp_create_new(struct inode *dir, struct dentry *dentry, umode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | dev_t rdev, __le32 attributes) |
| 911 | { |
| 912 | struct ncp_server *server = NCP_SERVER(dir); |
| 913 | struct ncp_entry_info finfo; |
| 914 | int error, result, len; |
| 915 | int opmode; |
| 916 | __u8 __name[NCP_MAXPATHLEN + 1]; |
| 917 | |
Joe Perches | e45ca8b | 2014-04-08 16:04:16 -0700 | [diff] [blame] | 918 | ncp_vdbg("creating %pd2, mode=%hx\n", dentry, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | ncp_age_dentry(server, dentry); |
| 921 | len = sizeof(__name); |
| 922 | error = ncp_io2vol(server, __name, &len, dentry->d_name.name, |
| 923 | dentry->d_name.len, !ncp_preserve_case(dir)); |
| 924 | if (error) |
| 925 | goto out; |
| 926 | |
| 927 | error = -EACCES; |
| 928 | |
| 929 | if (S_ISREG(mode) && |
| 930 | (server->m.flags & NCP_MOUNT_EXTRAS) && |
| 931 | (mode & S_IXUGO)) |
| 932 | attributes |= aSYSTEM | aSHARED; |
| 933 | |
| 934 | result = ncp_open_create_file_or_subdir(server, dir, __name, |
| 935 | OC_MODE_CREATE | OC_MODE_OPEN | OC_MODE_REPLACE, |
| 936 | attributes, AR_READ | AR_WRITE, &finfo); |
| 937 | opmode = O_RDWR; |
| 938 | if (result) { |
| 939 | result = ncp_open_create_file_or_subdir(server, dir, __name, |
| 940 | OC_MODE_CREATE | OC_MODE_OPEN | OC_MODE_REPLACE, |
| 941 | attributes, AR_WRITE, &finfo); |
| 942 | if (result) { |
| 943 | if (result == 0x87) |
| 944 | error = -ENAMETOOLONG; |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 945 | else if (result < 0) |
| 946 | error = result; |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 947 | ncp_dbg(1, "%pd2 failed\n", dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | goto out; |
| 949 | } |
| 950 | opmode = O_WRONLY; |
| 951 | } |
| 952 | finfo.access = opmode; |
| 953 | if (ncp_is_nfs_extras(server, finfo.volume)) { |
| 954 | finfo.i.nfs.mode = mode; |
| 955 | finfo.i.nfs.rdev = new_encode_dev(rdev); |
| 956 | if (ncp_modify_nfs_info(server, finfo.volume, |
| 957 | finfo.i.dirEntNum, |
| 958 | mode, new_encode_dev(rdev)) != 0) |
| 959 | goto out; |
| 960 | } |
| 961 | |
| 962 | error = ncp_instantiate(dir, dentry, &finfo); |
| 963 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | return error; |
| 965 | } |
| 966 | |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 967 | static int ncp_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 968 | bool excl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | { |
| 970 | return ncp_create_new(dir, dentry, mode, 0, 0); |
| 971 | } |
| 972 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 973 | static int ncp_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | { |
| 975 | struct ncp_entry_info finfo; |
| 976 | struct ncp_server *server = NCP_SERVER(dir); |
| 977 | int error, len; |
| 978 | __u8 __name[NCP_MAXPATHLEN + 1]; |
| 979 | |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 980 | ncp_dbg(1, "making %pd2\n", dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | ncp_age_dentry(server, dentry); |
| 983 | len = sizeof(__name); |
| 984 | error = ncp_io2vol(server, __name, &len, dentry->d_name.name, |
| 985 | dentry->d_name.len, !ncp_preserve_case(dir)); |
| 986 | if (error) |
| 987 | goto out; |
| 988 | |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 989 | error = ncp_open_create_file_or_subdir(server, dir, __name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | OC_MODE_CREATE, aDIR, |
| 991 | cpu_to_le16(0xffff), |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 992 | &finfo); |
| 993 | if (error == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | if (ncp_is_nfs_extras(server, finfo.volume)) { |
| 995 | mode |= S_IFDIR; |
| 996 | finfo.i.nfs.mode = mode; |
| 997 | if (ncp_modify_nfs_info(server, |
| 998 | finfo.volume, |
| 999 | finfo.i.dirEntNum, |
| 1000 | mode, 0) != 0) |
| 1001 | goto out; |
| 1002 | } |
| 1003 | error = ncp_instantiate(dir, dentry, &finfo); |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 1004 | } else if (error > 0) { |
| 1005 | error = -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | } |
| 1007 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | return error; |
| 1009 | } |
| 1010 | |
| 1011 | static int ncp_rmdir(struct inode *dir, struct dentry *dentry) |
| 1012 | { |
| 1013 | struct ncp_server *server = NCP_SERVER(dir); |
| 1014 | int error, result, len; |
| 1015 | __u8 __name[NCP_MAXPATHLEN + 1]; |
| 1016 | |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 1017 | ncp_dbg(1, "removing %pd2\n", dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | len = sizeof(__name); |
| 1020 | error = ncp_io2vol(server, __name, &len, dentry->d_name.name, |
| 1021 | dentry->d_name.len, !ncp_preserve_case(dir)); |
| 1022 | if (error) |
| 1023 | goto out; |
| 1024 | |
| 1025 | result = ncp_del_file_or_subdir(server, dir, __name); |
| 1026 | switch (result) { |
| 1027 | case 0x00: |
| 1028 | error = 0; |
| 1029 | break; |
| 1030 | case 0x85: /* unauthorized to delete file */ |
| 1031 | case 0x8A: /* unauthorized to delete file */ |
| 1032 | error = -EACCES; |
| 1033 | break; |
| 1034 | case 0x8F: |
| 1035 | case 0x90: /* read only */ |
| 1036 | error = -EPERM; |
| 1037 | break; |
| 1038 | case 0x9F: /* in use by another client */ |
| 1039 | error = -EBUSY; |
| 1040 | break; |
| 1041 | case 0xA0: /* directory not empty */ |
| 1042 | error = -ENOTEMPTY; |
| 1043 | break; |
| 1044 | case 0xFF: /* someone deleted file */ |
| 1045 | error = -ENOENT; |
| 1046 | break; |
| 1047 | default: |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 1048 | error = result < 0 ? result : -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | break; |
| 1050 | } |
| 1051 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | return error; |
| 1053 | } |
| 1054 | |
| 1055 | static int ncp_unlink(struct inode *dir, struct dentry *dentry) |
| 1056 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1057 | struct inode *inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | struct ncp_server *server; |
| 1059 | int error; |
| 1060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | server = NCP_SERVER(dir); |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 1062 | ncp_dbg(1, "unlinking %pd2\n", dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | /* |
| 1065 | * Check whether to close the file ... |
| 1066 | */ |
| 1067 | if (inode) { |
Joe Perches | e45ca8b | 2014-04-08 16:04:16 -0700 | [diff] [blame] | 1068 | ncp_vdbg("closing file\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | ncp_make_closed(inode); |
| 1070 | } |
| 1071 | |
| 1072 | error = ncp_del_file_or_subdir2(server, dentry); |
| 1073 | #ifdef CONFIG_NCPFS_STRONG |
| 1074 | /* 9C is Invalid path.. It should be 8F, 90 - read only, but |
| 1075 | it is not :-( */ |
| 1076 | if ((error == 0x9C || error == 0x90) && server->m.flags & NCP_MOUNT_STRONG) { /* R/O */ |
| 1077 | error = ncp_force_unlink(dir, dentry); |
| 1078 | } |
| 1079 | #endif |
| 1080 | switch (error) { |
| 1081 | case 0x00: |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 1082 | ncp_dbg(1, "removed %pd2\n", dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | break; |
| 1084 | case 0x85: |
| 1085 | case 0x8A: |
| 1086 | error = -EACCES; |
| 1087 | break; |
| 1088 | case 0x8D: /* some files in use */ |
| 1089 | case 0x8E: /* all files in use */ |
| 1090 | error = -EBUSY; |
| 1091 | break; |
| 1092 | case 0x8F: /* some read only */ |
| 1093 | case 0x90: /* all read only */ |
| 1094 | case 0x9C: /* !!! returned when in-use or read-only by NW4 */ |
| 1095 | error = -EPERM; |
| 1096 | break; |
| 1097 | case 0xFF: |
| 1098 | error = -ENOENT; |
| 1099 | break; |
| 1100 | default: |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 1101 | error = error < 0 ? error : -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | break; |
| 1103 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | return error; |
| 1105 | } |
| 1106 | |
| 1107 | static int ncp_rename(struct inode *old_dir, struct dentry *old_dentry, |
Miklos Szeredi | 1cd66c9 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 1108 | struct inode *new_dir, struct dentry *new_dentry, |
| 1109 | unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | { |
| 1111 | struct ncp_server *server = NCP_SERVER(old_dir); |
| 1112 | int error; |
| 1113 | int old_len, new_len; |
| 1114 | __u8 __old_name[NCP_MAXPATHLEN + 1], __new_name[NCP_MAXPATHLEN + 1]; |
| 1115 | |
Miklos Szeredi | 1cd66c9 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 1116 | if (flags) |
| 1117 | return -EINVAL; |
| 1118 | |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 1119 | ncp_dbg(1, "%pd2 to %pd2\n", old_dentry, new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | ncp_age_dentry(server, old_dentry); |
| 1122 | ncp_age_dentry(server, new_dentry); |
| 1123 | |
| 1124 | old_len = sizeof(__old_name); |
| 1125 | error = ncp_io2vol(server, __old_name, &old_len, |
| 1126 | old_dentry->d_name.name, old_dentry->d_name.len, |
| 1127 | !ncp_preserve_case(old_dir)); |
| 1128 | if (error) |
| 1129 | goto out; |
| 1130 | |
| 1131 | new_len = sizeof(__new_name); |
| 1132 | error = ncp_io2vol(server, __new_name, &new_len, |
| 1133 | new_dentry->d_name.name, new_dentry->d_name.len, |
| 1134 | !ncp_preserve_case(new_dir)); |
| 1135 | if (error) |
| 1136 | goto out; |
| 1137 | |
| 1138 | error = ncp_ren_or_mov_file_or_subdir(server, old_dir, __old_name, |
| 1139 | new_dir, __new_name); |
| 1140 | #ifdef CONFIG_NCPFS_STRONG |
| 1141 | if ((error == 0x90 || error == 0x8B || error == -EACCES) && |
| 1142 | server->m.flags & NCP_MOUNT_STRONG) { /* RO */ |
| 1143 | error = ncp_force_rename(old_dir, old_dentry, __old_name, |
| 1144 | new_dir, new_dentry, __new_name); |
| 1145 | } |
| 1146 | #endif |
| 1147 | switch (error) { |
| 1148 | case 0x00: |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 1149 | ncp_dbg(1, "renamed %pd -> %pd\n", |
| 1150 | old_dentry, new_dentry); |
Al Viro | 3f4a949 | 2015-06-06 09:15:55 -0400 | [diff] [blame] | 1151 | ncp_d_prune(old_dentry); |
| 1152 | ncp_d_prune(new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | break; |
| 1154 | case 0x9E: |
| 1155 | error = -ENAMETOOLONG; |
| 1156 | break; |
| 1157 | case 0xFF: |
| 1158 | error = -ENOENT; |
| 1159 | break; |
| 1160 | default: |
Petr Vandrovec | 2e54eb9 | 2010-09-27 01:47:33 +0200 | [diff] [blame] | 1161 | error = error < 0 ? error : -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | break; |
| 1163 | } |
| 1164 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | return error; |
| 1166 | } |
| 1167 | |
| 1168 | static int ncp_mknod(struct inode * dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 1169 | umode_t mode, dev_t rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | if (ncp_is_nfs_extras(NCP_SERVER(dir), NCP_FINFO(dir)->volNumber)) { |
Joe Perches | d3b73ca | 2014-04-08 16:04:15 -0700 | [diff] [blame] | 1172 | ncp_dbg(1, "mode = 0%ho\n", mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | return ncp_create_new(dir, dentry, mode, rdev, 0); |
| 1174 | } |
| 1175 | return -EPERM; /* Strange, but true */ |
| 1176 | } |
| 1177 | |
| 1178 | /* The following routines are taken directly from msdos-fs */ |
| 1179 | |
| 1180 | /* Linear day numbers of the respective 1sts in non-leap years. */ |
| 1181 | |
| 1182 | static int day_n[] = |
| 1183 | {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0}; |
| 1184 | /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */ |
| 1185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | static int utc2local(int time) |
| 1187 | { |
| 1188 | return time - sys_tz.tz_minuteswest * 60; |
| 1189 | } |
| 1190 | |
| 1191 | static int local2utc(int time) |
| 1192 | { |
| 1193 | return time + sys_tz.tz_minuteswest * 60; |
| 1194 | } |
| 1195 | |
| 1196 | /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */ |
| 1197 | int |
| 1198 | ncp_date_dos2unix(__le16 t, __le16 d) |
| 1199 | { |
| 1200 | unsigned short time = le16_to_cpu(t), date = le16_to_cpu(d); |
| 1201 | int month, year, secs; |
| 1202 | |
| 1203 | /* first subtract and mask after that... Otherwise, if |
| 1204 | date == 0, bad things happen */ |
| 1205 | month = ((date >> 5) - 1) & 15; |
| 1206 | year = date >> 9; |
| 1207 | secs = (time & 31) * 2 + 60 * ((time >> 5) & 63) + (time >> 11) * 3600 + |
| 1208 | 86400 * ((date & 31) - 1 + day_n[month] + (year / 4) + |
| 1209 | year * 365 - ((year & 3) == 0 && month < 2 ? 1 : 0) + 3653); |
| 1210 | /* days since 1.1.70 plus 80's leap day */ |
| 1211 | return local2utc(secs); |
| 1212 | } |
| 1213 | |
| 1214 | |
| 1215 | /* Convert linear UNIX date to a MS-DOS time/date pair. */ |
| 1216 | void |
| 1217 | ncp_date_unix2dos(int unix_date, __le16 *time, __le16 *date) |
| 1218 | { |
| 1219 | int day, year, nl_day, month; |
| 1220 | |
| 1221 | unix_date = utc2local(unix_date); |
| 1222 | *time = cpu_to_le16( |
| 1223 | (unix_date % 60) / 2 + (((unix_date / 60) % 60) << 5) + |
| 1224 | (((unix_date / 3600) % 24) << 11)); |
| 1225 | day = unix_date / 86400 - 3652; |
| 1226 | year = day / 365; |
| 1227 | if ((year + 3) / 4 + 365 * year > day) |
| 1228 | year--; |
| 1229 | day -= (year + 3) / 4 + 365 * year; |
| 1230 | if (day == 59 && !(year & 3)) { |
| 1231 | nl_day = day; |
| 1232 | month = 2; |
| 1233 | } else { |
| 1234 | nl_day = (year & 3) || day <= 59 ? day : day - 1; |
Roel Kluin | c5df591 | 2009-09-22 16:45:54 -0700 | [diff] [blame] | 1235 | for (month = 1; month < 12; month++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | if (day_n[month] > nl_day) |
| 1237 | break; |
| 1238 | } |
| 1239 | *date = cpu_to_le16(nl_day - day_n[month - 1] + 1 + (month << 5) + (year << 9)); |
| 1240 | } |