Commit 407938e79edcadba1b5a17cf928584d8a191a8d7

Authored by Al Viro
1 parent dd7dd556e4

LOOKUP_CREATE and LOOKUP_RENAME_TARGET can be set only on the last step

Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>

Showing 3 changed files with 6 additions and 12 deletions Inline Diff

1 /* 1 /*
2 * fs/cifs/dir.c 2 * fs/cifs/dir.c
3 * 3 *
4 * vfs operations that deal with dentries 4 * vfs operations that deal with dentries
5 * 5 *
6 * Copyright (C) International Business Machines Corp., 2002,2009 6 * Copyright (C) International Business Machines Corp., 2002,2009
7 * Author(s): Steve French (sfrench@us.ibm.com) 7 * Author(s): Steve French (sfrench@us.ibm.com)
8 * 8 *
9 * This library is free software; you can redistribute it and/or modify 9 * This library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published 10 * it under the terms of the GNU Lesser General Public License as published
11 * by the Free Software Foundation; either version 2.1 of the License, or 11 * by the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version. 12 * (at your option) any later version.
13 * 13 *
14 * This library is distributed in the hope that it will be useful, 14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU Lesser General Public License for more details. 17 * the GNU Lesser General Public License for more details.
18 * 18 *
19 * You should have received a copy of the GNU Lesser General Public License 19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this library; if not, write to the Free Software 20 * along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */ 22 */
23 #include <linux/fs.h> 23 #include <linux/fs.h>
24 #include <linux/stat.h> 24 #include <linux/stat.h>
25 #include <linux/slab.h> 25 #include <linux/slab.h>
26 #include <linux/namei.h> 26 #include <linux/namei.h>
27 #include <linux/mount.h> 27 #include <linux/mount.h>
28 #include <linux/file.h> 28 #include <linux/file.h>
29 #include "cifsfs.h" 29 #include "cifsfs.h"
30 #include "cifspdu.h" 30 #include "cifspdu.h"
31 #include "cifsglob.h" 31 #include "cifsglob.h"
32 #include "cifsproto.h" 32 #include "cifsproto.h"
33 #include "cifs_debug.h" 33 #include "cifs_debug.h"
34 #include "cifs_fs_sb.h" 34 #include "cifs_fs_sb.h"
35 35
36 static void 36 static void
37 renew_parental_timestamps(struct dentry *direntry) 37 renew_parental_timestamps(struct dentry *direntry)
38 { 38 {
39 /* BB check if there is a way to get the kernel to do this or if we 39 /* BB check if there is a way to get the kernel to do this or if we
40 really need this */ 40 really need this */
41 do { 41 do {
42 direntry->d_time = jiffies; 42 direntry->d_time = jiffies;
43 direntry = direntry->d_parent; 43 direntry = direntry->d_parent;
44 } while (!IS_ROOT(direntry)); 44 } while (!IS_ROOT(direntry));
45 } 45 }
46 46
47 /* Note: caller must free return buffer */ 47 /* Note: caller must free return buffer */
48 char * 48 char *
49 build_path_from_dentry(struct dentry *direntry) 49 build_path_from_dentry(struct dentry *direntry)
50 { 50 {
51 struct dentry *temp; 51 struct dentry *temp;
52 int namelen; 52 int namelen;
53 int dfsplen; 53 int dfsplen;
54 char *full_path; 54 char *full_path;
55 char dirsep; 55 char dirsep;
56 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb); 56 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
57 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb); 57 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
58 unsigned seq; 58 unsigned seq;
59 59
60 if (direntry == NULL) 60 if (direntry == NULL)
61 return NULL; /* not much we can do if dentry is freed and 61 return NULL; /* not much we can do if dentry is freed and
62 we need to reopen the file after it was closed implicitly 62 we need to reopen the file after it was closed implicitly
63 when the server crashed */ 63 when the server crashed */
64 64
65 dirsep = CIFS_DIR_SEP(cifs_sb); 65 dirsep = CIFS_DIR_SEP(cifs_sb);
66 if (tcon->Flags & SMB_SHARE_IS_IN_DFS) 66 if (tcon->Flags & SMB_SHARE_IS_IN_DFS)
67 dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1); 67 dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1);
68 else 68 else
69 dfsplen = 0; 69 dfsplen = 0;
70 cifs_bp_rename_retry: 70 cifs_bp_rename_retry:
71 namelen = dfsplen; 71 namelen = dfsplen;
72 seq = read_seqbegin(&rename_lock); 72 seq = read_seqbegin(&rename_lock);
73 rcu_read_lock(); 73 rcu_read_lock();
74 for (temp = direntry; !IS_ROOT(temp);) { 74 for (temp = direntry; !IS_ROOT(temp);) {
75 namelen += (1 + temp->d_name.len); 75 namelen += (1 + temp->d_name.len);
76 temp = temp->d_parent; 76 temp = temp->d_parent;
77 if (temp == NULL) { 77 if (temp == NULL) {
78 cERROR(1, "corrupt dentry"); 78 cERROR(1, "corrupt dentry");
79 rcu_read_unlock(); 79 rcu_read_unlock();
80 return NULL; 80 return NULL;
81 } 81 }
82 } 82 }
83 rcu_read_unlock(); 83 rcu_read_unlock();
84 84
85 full_path = kmalloc(namelen+1, GFP_KERNEL); 85 full_path = kmalloc(namelen+1, GFP_KERNEL);
86 if (full_path == NULL) 86 if (full_path == NULL)
87 return full_path; 87 return full_path;
88 full_path[namelen] = 0; /* trailing null */ 88 full_path[namelen] = 0; /* trailing null */
89 rcu_read_lock(); 89 rcu_read_lock();
90 for (temp = direntry; !IS_ROOT(temp);) { 90 for (temp = direntry; !IS_ROOT(temp);) {
91 spin_lock(&temp->d_lock); 91 spin_lock(&temp->d_lock);
92 namelen -= 1 + temp->d_name.len; 92 namelen -= 1 + temp->d_name.len;
93 if (namelen < 0) { 93 if (namelen < 0) {
94 spin_unlock(&temp->d_lock); 94 spin_unlock(&temp->d_lock);
95 break; 95 break;
96 } else { 96 } else {
97 full_path[namelen] = dirsep; 97 full_path[namelen] = dirsep;
98 strncpy(full_path + namelen + 1, temp->d_name.name, 98 strncpy(full_path + namelen + 1, temp->d_name.name,
99 temp->d_name.len); 99 temp->d_name.len);
100 cFYI(0, "name: %s", full_path + namelen); 100 cFYI(0, "name: %s", full_path + namelen);
101 } 101 }
102 spin_unlock(&temp->d_lock); 102 spin_unlock(&temp->d_lock);
103 temp = temp->d_parent; 103 temp = temp->d_parent;
104 if (temp == NULL) { 104 if (temp == NULL) {
105 cERROR(1, "corrupt dentry"); 105 cERROR(1, "corrupt dentry");
106 rcu_read_unlock(); 106 rcu_read_unlock();
107 kfree(full_path); 107 kfree(full_path);
108 return NULL; 108 return NULL;
109 } 109 }
110 } 110 }
111 rcu_read_unlock(); 111 rcu_read_unlock();
112 if (namelen != dfsplen || read_seqretry(&rename_lock, seq)) { 112 if (namelen != dfsplen || read_seqretry(&rename_lock, seq)) {
113 cERROR(1, "did not end path lookup where expected namelen is %d", 113 cERROR(1, "did not end path lookup where expected namelen is %d",
114 namelen); 114 namelen);
115 /* presumably this is only possible if racing with a rename 115 /* presumably this is only possible if racing with a rename
116 of one of the parent directories (we can not lock the dentries 116 of one of the parent directories (we can not lock the dentries
117 above us to prevent this, but retrying should be harmless) */ 117 above us to prevent this, but retrying should be harmless) */
118 kfree(full_path); 118 kfree(full_path);
119 goto cifs_bp_rename_retry; 119 goto cifs_bp_rename_retry;
120 } 120 }
121 /* DIR_SEP already set for byte 0 / vs \ but not for 121 /* DIR_SEP already set for byte 0 / vs \ but not for
122 subsequent slashes in prepath which currently must 122 subsequent slashes in prepath which currently must
123 be entered the right way - not sure if there is an alternative 123 be entered the right way - not sure if there is an alternative
124 since the '\' is a valid posix character so we can not switch 124 since the '\' is a valid posix character so we can not switch
125 those safely to '/' if any are found in the middle of the prepath */ 125 those safely to '/' if any are found in the middle of the prepath */
126 /* BB test paths to Windows with '/' in the midst of prepath */ 126 /* BB test paths to Windows with '/' in the midst of prepath */
127 127
128 if (dfsplen) { 128 if (dfsplen) {
129 strncpy(full_path, tcon->treeName, dfsplen); 129 strncpy(full_path, tcon->treeName, dfsplen);
130 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) { 130 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) {
131 int i; 131 int i;
132 for (i = 0; i < dfsplen; i++) { 132 for (i = 0; i < dfsplen; i++) {
133 if (full_path[i] == '\\') 133 if (full_path[i] == '\\')
134 full_path[i] = '/'; 134 full_path[i] = '/';
135 } 135 }
136 } 136 }
137 } 137 }
138 return full_path; 138 return full_path;
139 } 139 }
140 140
141 /* Inode operations in similar order to how they appear in Linux file fs.h */ 141 /* Inode operations in similar order to how they appear in Linux file fs.h */
142 142
143 int 143 int
144 cifs_create(struct inode *inode, struct dentry *direntry, int mode, 144 cifs_create(struct inode *inode, struct dentry *direntry, int mode,
145 struct nameidata *nd) 145 struct nameidata *nd)
146 { 146 {
147 int rc = -ENOENT; 147 int rc = -ENOENT;
148 int xid; 148 int xid;
149 int create_options = CREATE_NOT_DIR; 149 int create_options = CREATE_NOT_DIR;
150 __u32 oplock = 0; 150 __u32 oplock = 0;
151 int oflags; 151 int oflags;
152 /* 152 /*
153 * BB below access is probably too much for mknod to request 153 * BB below access is probably too much for mknod to request
154 * but we have to do query and setpathinfo so requesting 154 * but we have to do query and setpathinfo so requesting
155 * less could fail (unless we want to request getatr and setatr 155 * less could fail (unless we want to request getatr and setatr
156 * permissions (only). At least for POSIX we do not have to 156 * permissions (only). At least for POSIX we do not have to
157 * request so much. 157 * request so much.
158 */ 158 */
159 int desiredAccess = GENERIC_READ | GENERIC_WRITE; 159 int desiredAccess = GENERIC_READ | GENERIC_WRITE;
160 __u16 fileHandle; 160 __u16 fileHandle;
161 struct cifs_sb_info *cifs_sb; 161 struct cifs_sb_info *cifs_sb;
162 struct tcon_link *tlink; 162 struct tcon_link *tlink;
163 struct cifs_tcon *tcon; 163 struct cifs_tcon *tcon;
164 char *full_path = NULL; 164 char *full_path = NULL;
165 FILE_ALL_INFO *buf = NULL; 165 FILE_ALL_INFO *buf = NULL;
166 struct inode *newinode = NULL; 166 struct inode *newinode = NULL;
167 int disposition = FILE_OVERWRITE_IF; 167 int disposition = FILE_OVERWRITE_IF;
168 168
169 xid = GetXid(); 169 xid = GetXid();
170 170
171 cifs_sb = CIFS_SB(inode->i_sb); 171 cifs_sb = CIFS_SB(inode->i_sb);
172 tlink = cifs_sb_tlink(cifs_sb); 172 tlink = cifs_sb_tlink(cifs_sb);
173 if (IS_ERR(tlink)) { 173 if (IS_ERR(tlink)) {
174 FreeXid(xid); 174 FreeXid(xid);
175 return PTR_ERR(tlink); 175 return PTR_ERR(tlink);
176 } 176 }
177 tcon = tlink_tcon(tlink); 177 tcon = tlink_tcon(tlink);
178 178
179 if (oplockEnabled) 179 if (oplockEnabled)
180 oplock = REQ_OPLOCK; 180 oplock = REQ_OPLOCK;
181 181
182 if (nd) 182 if (nd)
183 oflags = nd->intent.open.file->f_flags; 183 oflags = nd->intent.open.file->f_flags;
184 else 184 else
185 oflags = O_RDONLY | O_CREAT; 185 oflags = O_RDONLY | O_CREAT;
186 186
187 full_path = build_path_from_dentry(direntry); 187 full_path = build_path_from_dentry(direntry);
188 if (full_path == NULL) { 188 if (full_path == NULL) {
189 rc = -ENOMEM; 189 rc = -ENOMEM;
190 goto cifs_create_out; 190 goto cifs_create_out;
191 } 191 }
192 192
193 if (tcon->unix_ext && (tcon->ses->capabilities & CAP_UNIX) && 193 if (tcon->unix_ext && (tcon->ses->capabilities & CAP_UNIX) &&
194 (CIFS_UNIX_POSIX_PATH_OPS_CAP & 194 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
195 le64_to_cpu(tcon->fsUnixInfo.Capability))) { 195 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
196 rc = cifs_posix_open(full_path, &newinode, 196 rc = cifs_posix_open(full_path, &newinode,
197 inode->i_sb, mode, oflags, &oplock, &fileHandle, xid); 197 inode->i_sb, mode, oflags, &oplock, &fileHandle, xid);
198 /* EIO could indicate that (posix open) operation is not 198 /* EIO could indicate that (posix open) operation is not
199 supported, despite what server claimed in capability 199 supported, despite what server claimed in capability
200 negotiation. EREMOTE indicates DFS junction, which is not 200 negotiation. EREMOTE indicates DFS junction, which is not
201 handled in posix open */ 201 handled in posix open */
202 202
203 if (rc == 0) { 203 if (rc == 0) {
204 if (newinode == NULL) /* query inode info */ 204 if (newinode == NULL) /* query inode info */
205 goto cifs_create_get_file_info; 205 goto cifs_create_get_file_info;
206 else /* success, no need to query */ 206 else /* success, no need to query */
207 goto cifs_create_set_dentry; 207 goto cifs_create_set_dentry;
208 } else if ((rc != -EIO) && (rc != -EREMOTE) && 208 } else if ((rc != -EIO) && (rc != -EREMOTE) &&
209 (rc != -EOPNOTSUPP) && (rc != -EINVAL)) 209 (rc != -EOPNOTSUPP) && (rc != -EINVAL))
210 goto cifs_create_out; 210 goto cifs_create_out;
211 /* else fallthrough to retry, using older open call, this is 211 /* else fallthrough to retry, using older open call, this is
212 case where server does not support this SMB level, and 212 case where server does not support this SMB level, and
213 falsely claims capability (also get here for DFS case 213 falsely claims capability (also get here for DFS case
214 which should be rare for path not covered on files) */ 214 which should be rare for path not covered on files) */
215 } 215 }
216 216
217 if (nd) { 217 if (nd) {
218 /* if the file is going to stay open, then we 218 /* if the file is going to stay open, then we
219 need to set the desired access properly */ 219 need to set the desired access properly */
220 desiredAccess = 0; 220 desiredAccess = 0;
221 if (OPEN_FMODE(oflags) & FMODE_READ) 221 if (OPEN_FMODE(oflags) & FMODE_READ)
222 desiredAccess |= GENERIC_READ; /* is this too little? */ 222 desiredAccess |= GENERIC_READ; /* is this too little? */
223 if (OPEN_FMODE(oflags) & FMODE_WRITE) 223 if (OPEN_FMODE(oflags) & FMODE_WRITE)
224 desiredAccess |= GENERIC_WRITE; 224 desiredAccess |= GENERIC_WRITE;
225 225
226 if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) 226 if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
227 disposition = FILE_CREATE; 227 disposition = FILE_CREATE;
228 else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC)) 228 else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
229 disposition = FILE_OVERWRITE_IF; 229 disposition = FILE_OVERWRITE_IF;
230 else if ((oflags & O_CREAT) == O_CREAT) 230 else if ((oflags & O_CREAT) == O_CREAT)
231 disposition = FILE_OPEN_IF; 231 disposition = FILE_OPEN_IF;
232 else 232 else
233 cFYI(1, "Create flag not set in create function"); 233 cFYI(1, "Create flag not set in create function");
234 } 234 }
235 235
236 /* BB add processing to set equivalent of mode - e.g. via CreateX with 236 /* BB add processing to set equivalent of mode - e.g. via CreateX with
237 ACLs */ 237 ACLs */
238 238
239 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); 239 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
240 if (buf == NULL) { 240 if (buf == NULL) {
241 rc = -ENOMEM; 241 rc = -ENOMEM;
242 goto cifs_create_out; 242 goto cifs_create_out;
243 } 243 }
244 244
245 /* 245 /*
246 * if we're not using unix extensions, see if we need to set 246 * if we're not using unix extensions, see if we need to set
247 * ATTR_READONLY on the create call 247 * ATTR_READONLY on the create call
248 */ 248 */
249 if (!tcon->unix_ext && (mode & S_IWUGO) == 0) 249 if (!tcon->unix_ext && (mode & S_IWUGO) == 0)
250 create_options |= CREATE_OPTION_READONLY; 250 create_options |= CREATE_OPTION_READONLY;
251 251
252 if (tcon->ses->capabilities & CAP_NT_SMBS) 252 if (tcon->ses->capabilities & CAP_NT_SMBS)
253 rc = CIFSSMBOpen(xid, tcon, full_path, disposition, 253 rc = CIFSSMBOpen(xid, tcon, full_path, disposition,
254 desiredAccess, create_options, 254 desiredAccess, create_options,
255 &fileHandle, &oplock, buf, cifs_sb->local_nls, 255 &fileHandle, &oplock, buf, cifs_sb->local_nls,
256 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); 256 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
257 else 257 else
258 rc = -EIO; /* no NT SMB support fall into legacy open below */ 258 rc = -EIO; /* no NT SMB support fall into legacy open below */
259 259
260 if (rc == -EIO) { 260 if (rc == -EIO) {
261 /* old server, retry the open legacy style */ 261 /* old server, retry the open legacy style */
262 rc = SMBLegacyOpen(xid, tcon, full_path, disposition, 262 rc = SMBLegacyOpen(xid, tcon, full_path, disposition,
263 desiredAccess, create_options, 263 desiredAccess, create_options,
264 &fileHandle, &oplock, buf, cifs_sb->local_nls, 264 &fileHandle, &oplock, buf, cifs_sb->local_nls,
265 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); 265 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
266 } 266 }
267 if (rc) { 267 if (rc) {
268 cFYI(1, "cifs_create returned 0x%x", rc); 268 cFYI(1, "cifs_create returned 0x%x", rc);
269 goto cifs_create_out; 269 goto cifs_create_out;
270 } 270 }
271 271
272 /* If Open reported that we actually created a file 272 /* If Open reported that we actually created a file
273 then we now have to set the mode if possible */ 273 then we now have to set the mode if possible */
274 if ((tcon->unix_ext) && (oplock & CIFS_CREATE_ACTION)) { 274 if ((tcon->unix_ext) && (oplock & CIFS_CREATE_ACTION)) {
275 struct cifs_unix_set_info_args args = { 275 struct cifs_unix_set_info_args args = {
276 .mode = mode, 276 .mode = mode,
277 .ctime = NO_CHANGE_64, 277 .ctime = NO_CHANGE_64,
278 .atime = NO_CHANGE_64, 278 .atime = NO_CHANGE_64,
279 .mtime = NO_CHANGE_64, 279 .mtime = NO_CHANGE_64,
280 .device = 0, 280 .device = 0,
281 }; 281 };
282 282
283 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { 283 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
284 args.uid = (__u64) current_fsuid(); 284 args.uid = (__u64) current_fsuid();
285 if (inode->i_mode & S_ISGID) 285 if (inode->i_mode & S_ISGID)
286 args.gid = (__u64) inode->i_gid; 286 args.gid = (__u64) inode->i_gid;
287 else 287 else
288 args.gid = (__u64) current_fsgid(); 288 args.gid = (__u64) current_fsgid();
289 } else { 289 } else {
290 args.uid = NO_CHANGE_64; 290 args.uid = NO_CHANGE_64;
291 args.gid = NO_CHANGE_64; 291 args.gid = NO_CHANGE_64;
292 } 292 }
293 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fileHandle, 293 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fileHandle,
294 current->tgid); 294 current->tgid);
295 } else { 295 } else {
296 /* BB implement mode setting via Windows security 296 /* BB implement mode setting via Windows security
297 descriptors e.g. */ 297 descriptors e.g. */
298 /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/ 298 /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/
299 299
300 /* Could set r/o dos attribute if mode & 0222 == 0 */ 300 /* Could set r/o dos attribute if mode & 0222 == 0 */
301 } 301 }
302 302
303 cifs_create_get_file_info: 303 cifs_create_get_file_info:
304 /* server might mask mode so we have to query for it */ 304 /* server might mask mode so we have to query for it */
305 if (tcon->unix_ext) 305 if (tcon->unix_ext)
306 rc = cifs_get_inode_info_unix(&newinode, full_path, 306 rc = cifs_get_inode_info_unix(&newinode, full_path,
307 inode->i_sb, xid); 307 inode->i_sb, xid);
308 else { 308 else {
309 rc = cifs_get_inode_info(&newinode, full_path, buf, 309 rc = cifs_get_inode_info(&newinode, full_path, buf,
310 inode->i_sb, xid, &fileHandle); 310 inode->i_sb, xid, &fileHandle);
311 if (newinode) { 311 if (newinode) {
312 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) 312 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
313 newinode->i_mode = mode; 313 newinode->i_mode = mode;
314 if ((oplock & CIFS_CREATE_ACTION) && 314 if ((oplock & CIFS_CREATE_ACTION) &&
315 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)) { 315 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID)) {
316 newinode->i_uid = current_fsuid(); 316 newinode->i_uid = current_fsuid();
317 if (inode->i_mode & S_ISGID) 317 if (inode->i_mode & S_ISGID)
318 newinode->i_gid = inode->i_gid; 318 newinode->i_gid = inode->i_gid;
319 else 319 else
320 newinode->i_gid = current_fsgid(); 320 newinode->i_gid = current_fsgid();
321 } 321 }
322 } 322 }
323 } 323 }
324 324
325 cifs_create_set_dentry: 325 cifs_create_set_dentry:
326 if (rc == 0) 326 if (rc == 0)
327 d_instantiate(direntry, newinode); 327 d_instantiate(direntry, newinode);
328 else 328 else
329 cFYI(1, "Create worked, get_inode_info failed rc = %d", rc); 329 cFYI(1, "Create worked, get_inode_info failed rc = %d", rc);
330 330
331 if (newinode && nd) { 331 if (newinode && nd) {
332 struct cifsFileInfo *pfile_info; 332 struct cifsFileInfo *pfile_info;
333 struct file *filp; 333 struct file *filp;
334 334
335 filp = lookup_instantiate_filp(nd, direntry, generic_file_open); 335 filp = lookup_instantiate_filp(nd, direntry, generic_file_open);
336 if (IS_ERR(filp)) { 336 if (IS_ERR(filp)) {
337 rc = PTR_ERR(filp); 337 rc = PTR_ERR(filp);
338 CIFSSMBClose(xid, tcon, fileHandle); 338 CIFSSMBClose(xid, tcon, fileHandle);
339 goto cifs_create_out; 339 goto cifs_create_out;
340 } 340 }
341 341
342 pfile_info = cifs_new_fileinfo(fileHandle, filp, tlink, oplock); 342 pfile_info = cifs_new_fileinfo(fileHandle, filp, tlink, oplock);
343 if (pfile_info == NULL) { 343 if (pfile_info == NULL) {
344 fput(filp); 344 fput(filp);
345 CIFSSMBClose(xid, tcon, fileHandle); 345 CIFSSMBClose(xid, tcon, fileHandle);
346 rc = -ENOMEM; 346 rc = -ENOMEM;
347 } 347 }
348 } else { 348 } else {
349 CIFSSMBClose(xid, tcon, fileHandle); 349 CIFSSMBClose(xid, tcon, fileHandle);
350 } 350 }
351 351
352 cifs_create_out: 352 cifs_create_out:
353 kfree(buf); 353 kfree(buf);
354 kfree(full_path); 354 kfree(full_path);
355 cifs_put_tlink(tlink); 355 cifs_put_tlink(tlink);
356 FreeXid(xid); 356 FreeXid(xid);
357 return rc; 357 return rc;
358 } 358 }
359 359
360 int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode, 360 int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode,
361 dev_t device_number) 361 dev_t device_number)
362 { 362 {
363 int rc = -EPERM; 363 int rc = -EPERM;
364 int xid; 364 int xid;
365 struct cifs_sb_info *cifs_sb; 365 struct cifs_sb_info *cifs_sb;
366 struct tcon_link *tlink; 366 struct tcon_link *tlink;
367 struct cifs_tcon *pTcon; 367 struct cifs_tcon *pTcon;
368 struct cifs_io_parms io_parms; 368 struct cifs_io_parms io_parms;
369 char *full_path = NULL; 369 char *full_path = NULL;
370 struct inode *newinode = NULL; 370 struct inode *newinode = NULL;
371 int oplock = 0; 371 int oplock = 0;
372 u16 fileHandle; 372 u16 fileHandle;
373 FILE_ALL_INFO *buf = NULL; 373 FILE_ALL_INFO *buf = NULL;
374 unsigned int bytes_written; 374 unsigned int bytes_written;
375 struct win_dev *pdev; 375 struct win_dev *pdev;
376 376
377 if (!old_valid_dev(device_number)) 377 if (!old_valid_dev(device_number))
378 return -EINVAL; 378 return -EINVAL;
379 379
380 cifs_sb = CIFS_SB(inode->i_sb); 380 cifs_sb = CIFS_SB(inode->i_sb);
381 tlink = cifs_sb_tlink(cifs_sb); 381 tlink = cifs_sb_tlink(cifs_sb);
382 if (IS_ERR(tlink)) 382 if (IS_ERR(tlink))
383 return PTR_ERR(tlink); 383 return PTR_ERR(tlink);
384 384
385 pTcon = tlink_tcon(tlink); 385 pTcon = tlink_tcon(tlink);
386 386
387 xid = GetXid(); 387 xid = GetXid();
388 388
389 full_path = build_path_from_dentry(direntry); 389 full_path = build_path_from_dentry(direntry);
390 if (full_path == NULL) { 390 if (full_path == NULL) {
391 rc = -ENOMEM; 391 rc = -ENOMEM;
392 goto mknod_out; 392 goto mknod_out;
393 } 393 }
394 394
395 if (pTcon->unix_ext) { 395 if (pTcon->unix_ext) {
396 struct cifs_unix_set_info_args args = { 396 struct cifs_unix_set_info_args args = {
397 .mode = mode & ~current_umask(), 397 .mode = mode & ~current_umask(),
398 .ctime = NO_CHANGE_64, 398 .ctime = NO_CHANGE_64,
399 .atime = NO_CHANGE_64, 399 .atime = NO_CHANGE_64,
400 .mtime = NO_CHANGE_64, 400 .mtime = NO_CHANGE_64,
401 .device = device_number, 401 .device = device_number,
402 }; 402 };
403 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { 403 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
404 args.uid = (__u64) current_fsuid(); 404 args.uid = (__u64) current_fsuid();
405 args.gid = (__u64) current_fsgid(); 405 args.gid = (__u64) current_fsgid();
406 } else { 406 } else {
407 args.uid = NO_CHANGE_64; 407 args.uid = NO_CHANGE_64;
408 args.gid = NO_CHANGE_64; 408 args.gid = NO_CHANGE_64;
409 } 409 }
410 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, &args, 410 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, &args,
411 cifs_sb->local_nls, 411 cifs_sb->local_nls,
412 cifs_sb->mnt_cifs_flags & 412 cifs_sb->mnt_cifs_flags &
413 CIFS_MOUNT_MAP_SPECIAL_CHR); 413 CIFS_MOUNT_MAP_SPECIAL_CHR);
414 if (rc) 414 if (rc)
415 goto mknod_out; 415 goto mknod_out;
416 416
417 rc = cifs_get_inode_info_unix(&newinode, full_path, 417 rc = cifs_get_inode_info_unix(&newinode, full_path,
418 inode->i_sb, xid); 418 inode->i_sb, xid);
419 419
420 if (rc == 0) 420 if (rc == 0)
421 d_instantiate(direntry, newinode); 421 d_instantiate(direntry, newinode);
422 goto mknod_out; 422 goto mknod_out;
423 } 423 }
424 424
425 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) 425 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
426 goto mknod_out; 426 goto mknod_out;
427 427
428 428
429 cFYI(1, "sfu compat create special file"); 429 cFYI(1, "sfu compat create special file");
430 430
431 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); 431 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
432 if (buf == NULL) { 432 if (buf == NULL) {
433 kfree(full_path); 433 kfree(full_path);
434 rc = -ENOMEM; 434 rc = -ENOMEM;
435 FreeXid(xid); 435 FreeXid(xid);
436 return rc; 436 return rc;
437 } 437 }
438 438
439 /* FIXME: would WRITE_OWNER | WRITE_DAC be better? */ 439 /* FIXME: would WRITE_OWNER | WRITE_DAC be better? */
440 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_CREATE, 440 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_CREATE,
441 GENERIC_WRITE, CREATE_NOT_DIR | CREATE_OPTION_SPECIAL, 441 GENERIC_WRITE, CREATE_NOT_DIR | CREATE_OPTION_SPECIAL,
442 &fileHandle, &oplock, buf, cifs_sb->local_nls, 442 &fileHandle, &oplock, buf, cifs_sb->local_nls,
443 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); 443 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
444 if (rc) 444 if (rc)
445 goto mknod_out; 445 goto mknod_out;
446 446
447 /* BB Do not bother to decode buf since no local inode yet to put 447 /* BB Do not bother to decode buf since no local inode yet to put
448 * timestamps in, but we can reuse it safely */ 448 * timestamps in, but we can reuse it safely */
449 449
450 pdev = (struct win_dev *)buf; 450 pdev = (struct win_dev *)buf;
451 io_parms.netfid = fileHandle; 451 io_parms.netfid = fileHandle;
452 io_parms.pid = current->tgid; 452 io_parms.pid = current->tgid;
453 io_parms.tcon = pTcon; 453 io_parms.tcon = pTcon;
454 io_parms.offset = 0; 454 io_parms.offset = 0;
455 io_parms.length = sizeof(struct win_dev); 455 io_parms.length = sizeof(struct win_dev);
456 if (S_ISCHR(mode)) { 456 if (S_ISCHR(mode)) {
457 memcpy(pdev->type, "IntxCHR", 8); 457 memcpy(pdev->type, "IntxCHR", 8);
458 pdev->major = 458 pdev->major =
459 cpu_to_le64(MAJOR(device_number)); 459 cpu_to_le64(MAJOR(device_number));
460 pdev->minor = 460 pdev->minor =
461 cpu_to_le64(MINOR(device_number)); 461 cpu_to_le64(MINOR(device_number));
462 rc = CIFSSMBWrite(xid, &io_parms, 462 rc = CIFSSMBWrite(xid, &io_parms,
463 &bytes_written, (char *)pdev, 463 &bytes_written, (char *)pdev,
464 NULL, 0); 464 NULL, 0);
465 } else if (S_ISBLK(mode)) { 465 } else if (S_ISBLK(mode)) {
466 memcpy(pdev->type, "IntxBLK", 8); 466 memcpy(pdev->type, "IntxBLK", 8);
467 pdev->major = 467 pdev->major =
468 cpu_to_le64(MAJOR(device_number)); 468 cpu_to_le64(MAJOR(device_number));
469 pdev->minor = 469 pdev->minor =
470 cpu_to_le64(MINOR(device_number)); 470 cpu_to_le64(MINOR(device_number));
471 rc = CIFSSMBWrite(xid, &io_parms, 471 rc = CIFSSMBWrite(xid, &io_parms,
472 &bytes_written, (char *)pdev, 472 &bytes_written, (char *)pdev,
473 NULL, 0); 473 NULL, 0);
474 } /* else if (S_ISFIFO) */ 474 } /* else if (S_ISFIFO) */
475 CIFSSMBClose(xid, pTcon, fileHandle); 475 CIFSSMBClose(xid, pTcon, fileHandle);
476 d_drop(direntry); 476 d_drop(direntry);
477 477
478 /* FIXME: add code here to set EAs */ 478 /* FIXME: add code here to set EAs */
479 479
480 mknod_out: 480 mknod_out:
481 kfree(full_path); 481 kfree(full_path);
482 kfree(buf); 482 kfree(buf);
483 FreeXid(xid); 483 FreeXid(xid);
484 cifs_put_tlink(tlink); 484 cifs_put_tlink(tlink);
485 return rc; 485 return rc;
486 } 486 }
487 487
488 struct dentry * 488 struct dentry *
489 cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry, 489 cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
490 struct nameidata *nd) 490 struct nameidata *nd)
491 { 491 {
492 int xid; 492 int xid;
493 int rc = 0; /* to get around spurious gcc warning, set to zero here */ 493 int rc = 0; /* to get around spurious gcc warning, set to zero here */
494 __u32 oplock = 0; 494 __u32 oplock = 0;
495 __u16 fileHandle = 0; 495 __u16 fileHandle = 0;
496 bool posix_open = false; 496 bool posix_open = false;
497 struct cifs_sb_info *cifs_sb; 497 struct cifs_sb_info *cifs_sb;
498 struct tcon_link *tlink; 498 struct tcon_link *tlink;
499 struct cifs_tcon *pTcon; 499 struct cifs_tcon *pTcon;
500 struct cifsFileInfo *cfile; 500 struct cifsFileInfo *cfile;
501 struct inode *newInode = NULL; 501 struct inode *newInode = NULL;
502 char *full_path = NULL; 502 char *full_path = NULL;
503 struct file *filp; 503 struct file *filp;
504 504
505 xid = GetXid(); 505 xid = GetXid();
506 506
507 cFYI(1, "parent inode = 0x%p name is: %s and dentry = 0x%p", 507 cFYI(1, "parent inode = 0x%p name is: %s and dentry = 0x%p",
508 parent_dir_inode, direntry->d_name.name, direntry); 508 parent_dir_inode, direntry->d_name.name, direntry);
509 509
510 /* check whether path exists */ 510 /* check whether path exists */
511 511
512 cifs_sb = CIFS_SB(parent_dir_inode->i_sb); 512 cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
513 tlink = cifs_sb_tlink(cifs_sb); 513 tlink = cifs_sb_tlink(cifs_sb);
514 if (IS_ERR(tlink)) { 514 if (IS_ERR(tlink)) {
515 FreeXid(xid); 515 FreeXid(xid);
516 return (struct dentry *)tlink; 516 return (struct dentry *)tlink;
517 } 517 }
518 pTcon = tlink_tcon(tlink); 518 pTcon = tlink_tcon(tlink);
519 519
520 /* 520 /*
521 * Don't allow the separator character in a path component. 521 * Don't allow the separator character in a path component.
522 * The VFS will not allow "/", but "\" is allowed by posix. 522 * The VFS will not allow "/", but "\" is allowed by posix.
523 */ 523 */
524 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) { 524 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) {
525 int i; 525 int i;
526 for (i = 0; i < direntry->d_name.len; i++) 526 for (i = 0; i < direntry->d_name.len; i++)
527 if (direntry->d_name.name[i] == '\\') { 527 if (direntry->d_name.name[i] == '\\') {
528 cFYI(1, "Invalid file name"); 528 cFYI(1, "Invalid file name");
529 rc = -EINVAL; 529 rc = -EINVAL;
530 goto lookup_out; 530 goto lookup_out;
531 } 531 }
532 } 532 }
533 533
534 /* 534 /*
535 * O_EXCL: optimize away the lookup, but don't hash the dentry. Let 535 * O_EXCL: optimize away the lookup, but don't hash the dentry. Let
536 * the VFS handle the create. 536 * the VFS handle the create.
537 */ 537 */
538 if (nd && (nd->flags & LOOKUP_EXCL)) { 538 if (nd && (nd->flags & LOOKUP_EXCL)) {
539 d_instantiate(direntry, NULL); 539 d_instantiate(direntry, NULL);
540 rc = 0; 540 rc = 0;
541 goto lookup_out; 541 goto lookup_out;
542 } 542 }
543 543
544 /* can not grab the rename sem here since it would 544 /* can not grab the rename sem here since it would
545 deadlock in the cases (beginning of sys_rename itself) 545 deadlock in the cases (beginning of sys_rename itself)
546 in which we already have the sb rename sem */ 546 in which we already have the sb rename sem */
547 full_path = build_path_from_dentry(direntry); 547 full_path = build_path_from_dentry(direntry);
548 if (full_path == NULL) { 548 if (full_path == NULL) {
549 rc = -ENOMEM; 549 rc = -ENOMEM;
550 goto lookup_out; 550 goto lookup_out;
551 } 551 }
552 552
553 if (direntry->d_inode != NULL) { 553 if (direntry->d_inode != NULL) {
554 cFYI(1, "non-NULL inode in lookup"); 554 cFYI(1, "non-NULL inode in lookup");
555 } else { 555 } else {
556 cFYI(1, "NULL inode in lookup"); 556 cFYI(1, "NULL inode in lookup");
557 } 557 }
558 cFYI(1, "Full path: %s inode = 0x%p", full_path, direntry->d_inode); 558 cFYI(1, "Full path: %s inode = 0x%p", full_path, direntry->d_inode);
559 559
560 /* Posix open is only called (at lookup time) for file create now. 560 /* Posix open is only called (at lookup time) for file create now.
561 * For opens (rather than creates), because we do not know if it 561 * For opens (rather than creates), because we do not know if it
562 * is a file or directory yet, and current Samba no longer allows 562 * is a file or directory yet, and current Samba no longer allows
563 * us to do posix open on dirs, we could end up wasting an open call 563 * us to do posix open on dirs, we could end up wasting an open call
564 * on what turns out to be a dir. For file opens, we wait to call posix 564 * on what turns out to be a dir. For file opens, we wait to call posix
565 * open till cifs_open. It could be added here (lookup) in the future 565 * open till cifs_open. It could be added here (lookup) in the future
566 * but the performance tradeoff of the extra network request when EISDIR 566 * but the performance tradeoff of the extra network request when EISDIR
567 * or EACCES is returned would have to be weighed against the 50% 567 * or EACCES is returned would have to be weighed against the 50%
568 * reduction in network traffic in the other paths. 568 * reduction in network traffic in the other paths.
569 */ 569 */
570 if (pTcon->unix_ext) { 570 if (pTcon->unix_ext) {
571 if (nd && !(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY)) && 571 if (nd && !(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY)) &&
572 (nd->flags & LOOKUP_OPEN) && !pTcon->broken_posix_open && 572 (nd->flags & LOOKUP_OPEN) && !pTcon->broken_posix_open &&
573 (nd->intent.open.file->f_flags & O_CREAT)) { 573 (nd->intent.open.file->f_flags & O_CREAT)) {
574 rc = cifs_posix_open(full_path, &newInode, 574 rc = cifs_posix_open(full_path, &newInode,
575 parent_dir_inode->i_sb, 575 parent_dir_inode->i_sb,
576 nd->intent.open.create_mode, 576 nd->intent.open.create_mode,
577 nd->intent.open.file->f_flags, &oplock, 577 nd->intent.open.file->f_flags, &oplock,
578 &fileHandle, xid); 578 &fileHandle, xid);
579 /* 579 /*
580 * The check below works around a bug in POSIX 580 * The check below works around a bug in POSIX
581 * open in samba versions 3.3.1 and earlier where 581 * open in samba versions 3.3.1 and earlier where
582 * open could incorrectly fail with invalid parameter. 582 * open could incorrectly fail with invalid parameter.
583 * If either that or op not supported returned, follow 583 * If either that or op not supported returned, follow
584 * the normal lookup. 584 * the normal lookup.
585 */ 585 */
586 if ((rc == 0) || (rc == -ENOENT)) 586 if ((rc == 0) || (rc == -ENOENT))
587 posix_open = true; 587 posix_open = true;
588 else if ((rc == -EINVAL) || (rc != -EOPNOTSUPP)) 588 else if ((rc == -EINVAL) || (rc != -EOPNOTSUPP))
589 pTcon->broken_posix_open = true; 589 pTcon->broken_posix_open = true;
590 } 590 }
591 if (!posix_open) 591 if (!posix_open)
592 rc = cifs_get_inode_info_unix(&newInode, full_path, 592 rc = cifs_get_inode_info_unix(&newInode, full_path,
593 parent_dir_inode->i_sb, xid); 593 parent_dir_inode->i_sb, xid);
594 } else 594 } else
595 rc = cifs_get_inode_info(&newInode, full_path, NULL, 595 rc = cifs_get_inode_info(&newInode, full_path, NULL,
596 parent_dir_inode->i_sb, xid, NULL); 596 parent_dir_inode->i_sb, xid, NULL);
597 597
598 if ((rc == 0) && (newInode != NULL)) { 598 if ((rc == 0) && (newInode != NULL)) {
599 d_add(direntry, newInode); 599 d_add(direntry, newInode);
600 if (posix_open) { 600 if (posix_open) {
601 filp = lookup_instantiate_filp(nd, direntry, 601 filp = lookup_instantiate_filp(nd, direntry,
602 generic_file_open); 602 generic_file_open);
603 if (IS_ERR(filp)) { 603 if (IS_ERR(filp)) {
604 rc = PTR_ERR(filp); 604 rc = PTR_ERR(filp);
605 CIFSSMBClose(xid, pTcon, fileHandle); 605 CIFSSMBClose(xid, pTcon, fileHandle);
606 goto lookup_out; 606 goto lookup_out;
607 } 607 }
608 608
609 cfile = cifs_new_fileinfo(fileHandle, filp, tlink, 609 cfile = cifs_new_fileinfo(fileHandle, filp, tlink,
610 oplock); 610 oplock);
611 if (cfile == NULL) { 611 if (cfile == NULL) {
612 fput(filp); 612 fput(filp);
613 CIFSSMBClose(xid, pTcon, fileHandle); 613 CIFSSMBClose(xid, pTcon, fileHandle);
614 rc = -ENOMEM; 614 rc = -ENOMEM;
615 goto lookup_out; 615 goto lookup_out;
616 } 616 }
617 } 617 }
618 /* since paths are not looked up by component - the parent 618 /* since paths are not looked up by component - the parent
619 directories are presumed to be good here */ 619 directories are presumed to be good here */
620 renew_parental_timestamps(direntry); 620 renew_parental_timestamps(direntry);
621 621
622 } else if (rc == -ENOENT) { 622 } else if (rc == -ENOENT) {
623 rc = 0; 623 rc = 0;
624 direntry->d_time = jiffies; 624 direntry->d_time = jiffies;
625 d_add(direntry, NULL); 625 d_add(direntry, NULL);
626 /* if it was once a directory (but how can we tell?) we could do 626 /* if it was once a directory (but how can we tell?) we could do
627 shrink_dcache_parent(direntry); */ 627 shrink_dcache_parent(direntry); */
628 } else if (rc != -EACCES) { 628 } else if (rc != -EACCES) {
629 cERROR(1, "Unexpected lookup error %d", rc); 629 cERROR(1, "Unexpected lookup error %d", rc);
630 /* We special case check for Access Denied - since that 630 /* We special case check for Access Denied - since that
631 is a common return code */ 631 is a common return code */
632 } 632 }
633 633
634 lookup_out: 634 lookup_out:
635 kfree(full_path); 635 kfree(full_path);
636 cifs_put_tlink(tlink); 636 cifs_put_tlink(tlink);
637 FreeXid(xid); 637 FreeXid(xid);
638 return ERR_PTR(rc); 638 return ERR_PTR(rc);
639 } 639 }
640 640
641 static int 641 static int
642 cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd) 642 cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd)
643 { 643 {
644 if (nd->flags & LOOKUP_RCU) 644 if (nd->flags & LOOKUP_RCU)
645 return -ECHILD; 645 return -ECHILD;
646 646
647 if (direntry->d_inode) { 647 if (direntry->d_inode) {
648 if (cifs_revalidate_dentry(direntry)) 648 if (cifs_revalidate_dentry(direntry))
649 return 0; 649 return 0;
650 else 650 else
651 return 1; 651 return 1;
652 } 652 }
653 653
654 /* 654 /*
655 * This may be nfsd (or something), anyway, we can't see the 655 * This may be nfsd (or something), anyway, we can't see the
656 * intent of this. So, since this can be for creation, drop it. 656 * intent of this. So, since this can be for creation, drop it.
657 */ 657 */
658 if (!nd) 658 if (!nd)
659 return 0; 659 return 0;
660 660
661 /* 661 /*
662 * Drop the negative dentry, in order to make sure to use the 662 * Drop the negative dentry, in order to make sure to use the
663 * case sensitive name which is specified by user if this is 663 * case sensitive name which is specified by user if this is
664 * for creation. 664 * for creation.
665 */ 665 */
666 if (!(nd->flags & (LOOKUP_CONTINUE | LOOKUP_PARENT))) { 666 if (nd->flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
667 if (nd->flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 667 return 0;
668 return 0;
669 }
670 668
671 if (time_after(jiffies, direntry->d_time + HZ) || !lookupCacheEnabled) 669 if (time_after(jiffies, direntry->d_time + HZ) || !lookupCacheEnabled)
672 return 0; 670 return 0;
673 671
674 return 1; 672 return 1;
675 } 673 }
676 674
677 /* static int cifs_d_delete(struct dentry *direntry) 675 /* static int cifs_d_delete(struct dentry *direntry)
678 { 676 {
679 int rc = 0; 677 int rc = 0;
680 678
681 cFYI(1, "In cifs d_delete, name = %s", direntry->d_name.name); 679 cFYI(1, "In cifs d_delete, name = %s", direntry->d_name.name);
682 680
683 return rc; 681 return rc;
684 } */ 682 } */
685 683
686 const struct dentry_operations cifs_dentry_ops = { 684 const struct dentry_operations cifs_dentry_ops = {
687 .d_revalidate = cifs_d_revalidate, 685 .d_revalidate = cifs_d_revalidate,
688 .d_automount = cifs_dfs_d_automount, 686 .d_automount = cifs_dfs_d_automount,
689 /* d_delete: cifs_d_delete, */ /* not needed except for debugging */ 687 /* d_delete: cifs_d_delete, */ /* not needed except for debugging */
690 }; 688 };
691 689
692 static int cifs_ci_hash(const struct dentry *dentry, const struct inode *inode, 690 static int cifs_ci_hash(const struct dentry *dentry, const struct inode *inode,
693 struct qstr *q) 691 struct qstr *q)
694 { 692 {
695 struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls; 693 struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
696 unsigned long hash; 694 unsigned long hash;
697 int i; 695 int i;
698 696
699 hash = init_name_hash(); 697 hash = init_name_hash();
700 for (i = 0; i < q->len; i++) 698 for (i = 0; i < q->len; i++)
701 hash = partial_name_hash(nls_tolower(codepage, q->name[i]), 699 hash = partial_name_hash(nls_tolower(codepage, q->name[i]),
702 hash); 700 hash);
703 q->hash = end_name_hash(hash); 701 q->hash = end_name_hash(hash);
704 702
705 return 0; 703 return 0;
706 } 704 }
707 705
708 static int cifs_ci_compare(const struct dentry *parent, 706 static int cifs_ci_compare(const struct dentry *parent,
709 const struct inode *pinode, 707 const struct inode *pinode,
710 const struct dentry *dentry, const struct inode *inode, 708 const struct dentry *dentry, const struct inode *inode,
711 unsigned int len, const char *str, const struct qstr *name) 709 unsigned int len, const char *str, const struct qstr *name)
712 { 710 {
713 struct nls_table *codepage = CIFS_SB(pinode->i_sb)->local_nls; 711 struct nls_table *codepage = CIFS_SB(pinode->i_sb)->local_nls;
714 712
715 if ((name->len == len) && 713 if ((name->len == len) &&
716 (nls_strnicmp(codepage, name->name, str, len) == 0)) 714 (nls_strnicmp(codepage, name->name, str, len) == 0))
717 return 0; 715 return 0;
718 return 1; 716 return 1;
719 } 717 }
720 718
721 const struct dentry_operations cifs_ci_dentry_ops = { 719 const struct dentry_operations cifs_ci_dentry_ops = {
722 .d_revalidate = cifs_d_revalidate, 720 .d_revalidate = cifs_d_revalidate,
723 .d_hash = cifs_ci_hash, 721 .d_hash = cifs_ci_hash,
724 .d_compare = cifs_ci_compare, 722 .d_compare = cifs_ci_compare,
725 .d_automount = cifs_dfs_d_automount, 723 .d_automount = cifs_dfs_d_automount,
726 }; 724 };
727 725
1 /* 1 /*
2 * linux/fs/vfat/namei.c 2 * linux/fs/vfat/namei.c
3 * 3 *
4 * Written 1992,1993 by Werner Almesberger 4 * Written 1992,1993 by Werner Almesberger
5 * 5 *
6 * Windows95/Windows NT compatible extended MSDOS filesystem 6 * Windows95/Windows NT compatible extended MSDOS filesystem
7 * by Gordon Chaffee Copyright (C) 1995. Send bug reports for the 7 * by Gordon Chaffee Copyright (C) 1995. Send bug reports for the
8 * VFAT filesystem to <chaffee@cs.berkeley.edu>. Specify 8 * VFAT filesystem to <chaffee@cs.berkeley.edu>. Specify
9 * what file operation caused you trouble and if you can duplicate 9 * what file operation caused you trouble and if you can duplicate
10 * the problem, send a script that demonstrates it. 10 * the problem, send a script that demonstrates it.
11 * 11 *
12 * Short name translation 1999, 2001 by Wolfram Pienkoss <wp@bszh.de> 12 * Short name translation 1999, 2001 by Wolfram Pienkoss <wp@bszh.de>
13 * 13 *
14 * Support Multibyte characters and cleanup by 14 * Support Multibyte characters and cleanup by
15 * OGAWA Hirofumi <hirofumi@mail.parknet.co.jp> 15 * OGAWA Hirofumi <hirofumi@mail.parknet.co.jp>
16 */ 16 */
17 17
18 #include <linux/module.h> 18 #include <linux/module.h>
19 #include <linux/jiffies.h> 19 #include <linux/jiffies.h>
20 #include <linux/ctype.h> 20 #include <linux/ctype.h>
21 #include <linux/slab.h> 21 #include <linux/slab.h>
22 #include <linux/buffer_head.h> 22 #include <linux/buffer_head.h>
23 #include <linux/namei.h> 23 #include <linux/namei.h>
24 #include "fat.h" 24 #include "fat.h"
25 25
26 /* 26 /*
27 * If new entry was created in the parent, it could create the 8.3 27 * If new entry was created in the parent, it could create the 8.3
28 * alias (the shortname of logname). So, the parent may have the 28 * alias (the shortname of logname). So, the parent may have the
29 * negative-dentry which matches the created 8.3 alias. 29 * negative-dentry which matches the created 8.3 alias.
30 * 30 *
31 * If it happened, the negative dentry isn't actually negative 31 * If it happened, the negative dentry isn't actually negative
32 * anymore. So, drop it. 32 * anymore. So, drop it.
33 */ 33 */
34 static int vfat_revalidate_shortname(struct dentry *dentry) 34 static int vfat_revalidate_shortname(struct dentry *dentry)
35 { 35 {
36 int ret = 1; 36 int ret = 1;
37 spin_lock(&dentry->d_lock); 37 spin_lock(&dentry->d_lock);
38 if (dentry->d_time != dentry->d_parent->d_inode->i_version) 38 if (dentry->d_time != dentry->d_parent->d_inode->i_version)
39 ret = 0; 39 ret = 0;
40 spin_unlock(&dentry->d_lock); 40 spin_unlock(&dentry->d_lock);
41 return ret; 41 return ret;
42 } 42 }
43 43
44 static int vfat_revalidate(struct dentry *dentry, struct nameidata *nd) 44 static int vfat_revalidate(struct dentry *dentry, struct nameidata *nd)
45 { 45 {
46 if (nd && nd->flags & LOOKUP_RCU) 46 if (nd && nd->flags & LOOKUP_RCU)
47 return -ECHILD; 47 return -ECHILD;
48 48
49 /* This is not negative dentry. Always valid. */ 49 /* This is not negative dentry. Always valid. */
50 if (dentry->d_inode) 50 if (dentry->d_inode)
51 return 1; 51 return 1;
52 return vfat_revalidate_shortname(dentry); 52 return vfat_revalidate_shortname(dentry);
53 } 53 }
54 54
55 static int vfat_revalidate_ci(struct dentry *dentry, struct nameidata *nd) 55 static int vfat_revalidate_ci(struct dentry *dentry, struct nameidata *nd)
56 { 56 {
57 if (nd && nd->flags & LOOKUP_RCU) 57 if (nd && nd->flags & LOOKUP_RCU)
58 return -ECHILD; 58 return -ECHILD;
59 59
60 /* 60 /*
61 * This is not negative dentry. Always valid. 61 * This is not negative dentry. Always valid.
62 * 62 *
63 * Note, rename() to existing directory entry will have ->d_inode, 63 * Note, rename() to existing directory entry will have ->d_inode,
64 * and will use existing name which isn't specified name by user. 64 * and will use existing name which isn't specified name by user.
65 * 65 *
66 * We may be able to drop this positive dentry here. But dropping 66 * We may be able to drop this positive dentry here. But dropping
67 * positive dentry isn't good idea. So it's unsupported like 67 * positive dentry isn't good idea. So it's unsupported like
68 * rename("filename", "FILENAME") for now. 68 * rename("filename", "FILENAME") for now.
69 */ 69 */
70 if (dentry->d_inode) 70 if (dentry->d_inode)
71 return 1; 71 return 1;
72 72
73 /* 73 /*
74 * This may be nfsd (or something), anyway, we can't see the 74 * This may be nfsd (or something), anyway, we can't see the
75 * intent of this. So, since this can be for creation, drop it. 75 * intent of this. So, since this can be for creation, drop it.
76 */ 76 */
77 if (!nd) 77 if (!nd)
78 return 0; 78 return 0;
79 79
80 /* 80 /*
81 * Drop the negative dentry, in order to make sure to use the 81 * Drop the negative dentry, in order to make sure to use the
82 * case sensitive name which is specified by user if this is 82 * case sensitive name which is specified by user if this is
83 * for creation. 83 * for creation.
84 */ 84 */
85 if (!(nd->flags & (LOOKUP_CONTINUE | LOOKUP_PARENT))) { 85 if (nd->flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
86 if (nd->flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 86 return 0;
87 return 0;
88 }
89 87
90 return vfat_revalidate_shortname(dentry); 88 return vfat_revalidate_shortname(dentry);
91 } 89 }
92 90
93 /* returns the length of a struct qstr, ignoring trailing dots */ 91 /* returns the length of a struct qstr, ignoring trailing dots */
94 static unsigned int __vfat_striptail_len(unsigned int len, const char *name) 92 static unsigned int __vfat_striptail_len(unsigned int len, const char *name)
95 { 93 {
96 while (len && name[len - 1] == '.') 94 while (len && name[len - 1] == '.')
97 len--; 95 len--;
98 return len; 96 return len;
99 } 97 }
100 98
101 static unsigned int vfat_striptail_len(const struct qstr *qstr) 99 static unsigned int vfat_striptail_len(const struct qstr *qstr)
102 { 100 {
103 return __vfat_striptail_len(qstr->len, qstr->name); 101 return __vfat_striptail_len(qstr->len, qstr->name);
104 } 102 }
105 103
106 /* 104 /*
107 * Compute the hash for the vfat name corresponding to the dentry. 105 * Compute the hash for the vfat name corresponding to the dentry.
108 * Note: if the name is invalid, we leave the hash code unchanged so 106 * Note: if the name is invalid, we leave the hash code unchanged so
109 * that the existing dentry can be used. The vfat fs routines will 107 * that the existing dentry can be used. The vfat fs routines will
110 * return ENOENT or EINVAL as appropriate. 108 * return ENOENT or EINVAL as appropriate.
111 */ 109 */
112 static int vfat_hash(const struct dentry *dentry, const struct inode *inode, 110 static int vfat_hash(const struct dentry *dentry, const struct inode *inode,
113 struct qstr *qstr) 111 struct qstr *qstr)
114 { 112 {
115 qstr->hash = full_name_hash(qstr->name, vfat_striptail_len(qstr)); 113 qstr->hash = full_name_hash(qstr->name, vfat_striptail_len(qstr));
116 return 0; 114 return 0;
117 } 115 }
118 116
119 /* 117 /*
120 * Compute the hash for the vfat name corresponding to the dentry. 118 * Compute the hash for the vfat name corresponding to the dentry.
121 * Note: if the name is invalid, we leave the hash code unchanged so 119 * Note: if the name is invalid, we leave the hash code unchanged so
122 * that the existing dentry can be used. The vfat fs routines will 120 * that the existing dentry can be used. The vfat fs routines will
123 * return ENOENT or EINVAL as appropriate. 121 * return ENOENT or EINVAL as appropriate.
124 */ 122 */
125 static int vfat_hashi(const struct dentry *dentry, const struct inode *inode, 123 static int vfat_hashi(const struct dentry *dentry, const struct inode *inode,
126 struct qstr *qstr) 124 struct qstr *qstr)
127 { 125 {
128 struct nls_table *t = MSDOS_SB(dentry->d_sb)->nls_io; 126 struct nls_table *t = MSDOS_SB(dentry->d_sb)->nls_io;
129 const unsigned char *name; 127 const unsigned char *name;
130 unsigned int len; 128 unsigned int len;
131 unsigned long hash; 129 unsigned long hash;
132 130
133 name = qstr->name; 131 name = qstr->name;
134 len = vfat_striptail_len(qstr); 132 len = vfat_striptail_len(qstr);
135 133
136 hash = init_name_hash(); 134 hash = init_name_hash();
137 while (len--) 135 while (len--)
138 hash = partial_name_hash(nls_tolower(t, *name++), hash); 136 hash = partial_name_hash(nls_tolower(t, *name++), hash);
139 qstr->hash = end_name_hash(hash); 137 qstr->hash = end_name_hash(hash);
140 138
141 return 0; 139 return 0;
142 } 140 }
143 141
144 /* 142 /*
145 * Case insensitive compare of two vfat names. 143 * Case insensitive compare of two vfat names.
146 */ 144 */
147 static int vfat_cmpi(const struct dentry *parent, const struct inode *pinode, 145 static int vfat_cmpi(const struct dentry *parent, const struct inode *pinode,
148 const struct dentry *dentry, const struct inode *inode, 146 const struct dentry *dentry, const struct inode *inode,
149 unsigned int len, const char *str, const struct qstr *name) 147 unsigned int len, const char *str, const struct qstr *name)
150 { 148 {
151 struct nls_table *t = MSDOS_SB(parent->d_sb)->nls_io; 149 struct nls_table *t = MSDOS_SB(parent->d_sb)->nls_io;
152 unsigned int alen, blen; 150 unsigned int alen, blen;
153 151
154 /* A filename cannot end in '.' or we treat it like it has none */ 152 /* A filename cannot end in '.' or we treat it like it has none */
155 alen = vfat_striptail_len(name); 153 alen = vfat_striptail_len(name);
156 blen = __vfat_striptail_len(len, str); 154 blen = __vfat_striptail_len(len, str);
157 if (alen == blen) { 155 if (alen == blen) {
158 if (nls_strnicmp(t, name->name, str, alen) == 0) 156 if (nls_strnicmp(t, name->name, str, alen) == 0)
159 return 0; 157 return 0;
160 } 158 }
161 return 1; 159 return 1;
162 } 160 }
163 161
164 /* 162 /*
165 * Case sensitive compare of two vfat names. 163 * Case sensitive compare of two vfat names.
166 */ 164 */
167 static int vfat_cmp(const struct dentry *parent, const struct inode *pinode, 165 static int vfat_cmp(const struct dentry *parent, const struct inode *pinode,
168 const struct dentry *dentry, const struct inode *inode, 166 const struct dentry *dentry, const struct inode *inode,
169 unsigned int len, const char *str, const struct qstr *name) 167 unsigned int len, const char *str, const struct qstr *name)
170 { 168 {
171 unsigned int alen, blen; 169 unsigned int alen, blen;
172 170
173 /* A filename cannot end in '.' or we treat it like it has none */ 171 /* A filename cannot end in '.' or we treat it like it has none */
174 alen = vfat_striptail_len(name); 172 alen = vfat_striptail_len(name);
175 blen = __vfat_striptail_len(len, str); 173 blen = __vfat_striptail_len(len, str);
176 if (alen == blen) { 174 if (alen == blen) {
177 if (strncmp(name->name, str, alen) == 0) 175 if (strncmp(name->name, str, alen) == 0)
178 return 0; 176 return 0;
179 } 177 }
180 return 1; 178 return 1;
181 } 179 }
182 180
183 static const struct dentry_operations vfat_ci_dentry_ops = { 181 static const struct dentry_operations vfat_ci_dentry_ops = {
184 .d_revalidate = vfat_revalidate_ci, 182 .d_revalidate = vfat_revalidate_ci,
185 .d_hash = vfat_hashi, 183 .d_hash = vfat_hashi,
186 .d_compare = vfat_cmpi, 184 .d_compare = vfat_cmpi,
187 }; 185 };
188 186
189 static const struct dentry_operations vfat_dentry_ops = { 187 static const struct dentry_operations vfat_dentry_ops = {
190 .d_revalidate = vfat_revalidate, 188 .d_revalidate = vfat_revalidate,
191 .d_hash = vfat_hash, 189 .d_hash = vfat_hash,
192 .d_compare = vfat_cmp, 190 .d_compare = vfat_cmp,
193 }; 191 };
194 192
195 /* Characters that are undesirable in an MS-DOS file name */ 193 /* Characters that are undesirable in an MS-DOS file name */
196 194
197 static inline wchar_t vfat_bad_char(wchar_t w) 195 static inline wchar_t vfat_bad_char(wchar_t w)
198 { 196 {
199 return (w < 0x0020) 197 return (w < 0x0020)
200 || (w == '*') || (w == '?') || (w == '<') || (w == '>') 198 || (w == '*') || (w == '?') || (w == '<') || (w == '>')
201 || (w == '|') || (w == '"') || (w == ':') || (w == '/') 199 || (w == '|') || (w == '"') || (w == ':') || (w == '/')
202 || (w == '\\'); 200 || (w == '\\');
203 } 201 }
204 202
205 static inline wchar_t vfat_replace_char(wchar_t w) 203 static inline wchar_t vfat_replace_char(wchar_t w)
206 { 204 {
207 return (w == '[') || (w == ']') || (w == ';') || (w == ',') 205 return (w == '[') || (w == ']') || (w == ';') || (w == ',')
208 || (w == '+') || (w == '='); 206 || (w == '+') || (w == '=');
209 } 207 }
210 208
211 static wchar_t vfat_skip_char(wchar_t w) 209 static wchar_t vfat_skip_char(wchar_t w)
212 { 210 {
213 return (w == '.') || (w == ' '); 211 return (w == '.') || (w == ' ');
214 } 212 }
215 213
216 static inline int vfat_is_used_badchars(const wchar_t *s, int len) 214 static inline int vfat_is_used_badchars(const wchar_t *s, int len)
217 { 215 {
218 int i; 216 int i;
219 217
220 for (i = 0; i < len; i++) 218 for (i = 0; i < len; i++)
221 if (vfat_bad_char(s[i])) 219 if (vfat_bad_char(s[i]))
222 return -EINVAL; 220 return -EINVAL;
223 221
224 if (s[i - 1] == ' ') /* last character cannot be space */ 222 if (s[i - 1] == ' ') /* last character cannot be space */
225 return -EINVAL; 223 return -EINVAL;
226 224
227 return 0; 225 return 0;
228 } 226 }
229 227
230 static int vfat_find_form(struct inode *dir, unsigned char *name) 228 static int vfat_find_form(struct inode *dir, unsigned char *name)
231 { 229 {
232 struct fat_slot_info sinfo; 230 struct fat_slot_info sinfo;
233 int err = fat_scan(dir, name, &sinfo); 231 int err = fat_scan(dir, name, &sinfo);
234 if (err) 232 if (err)
235 return -ENOENT; 233 return -ENOENT;
236 brelse(sinfo.bh); 234 brelse(sinfo.bh);
237 return 0; 235 return 0;
238 } 236 }
239 237
240 /* 238 /*
241 * 1) Valid characters for the 8.3 format alias are any combination of 239 * 1) Valid characters for the 8.3 format alias are any combination of
242 * letters, uppercase alphabets, digits, any of the 240 * letters, uppercase alphabets, digits, any of the
243 * following special characters: 241 * following special characters:
244 * $ % ' ` - @ { } ~ ! # ( ) & _ ^ 242 * $ % ' ` - @ { } ~ ! # ( ) & _ ^
245 * In this case Longfilename is not stored in disk. 243 * In this case Longfilename is not stored in disk.
246 * 244 *
247 * WinNT's Extension: 245 * WinNT's Extension:
248 * File name and extension name is contain uppercase/lowercase 246 * File name and extension name is contain uppercase/lowercase
249 * only. And it is expressed by CASE_LOWER_BASE and CASE_LOWER_EXT. 247 * only. And it is expressed by CASE_LOWER_BASE and CASE_LOWER_EXT.
250 * 248 *
251 * 2) File name is 8.3 format, but it contain the uppercase and 249 * 2) File name is 8.3 format, but it contain the uppercase and
252 * lowercase char, muliti bytes char, etc. In this case numtail is not 250 * lowercase char, muliti bytes char, etc. In this case numtail is not
253 * added, but Longfilename is stored. 251 * added, but Longfilename is stored.
254 * 252 *
255 * 3) When the one except for the above, or the following special 253 * 3) When the one except for the above, or the following special
256 * character are contained: 254 * character are contained:
257 * . [ ] ; , + = 255 * . [ ] ; , + =
258 * numtail is added, and Longfilename must be stored in disk . 256 * numtail is added, and Longfilename must be stored in disk .
259 */ 257 */
260 struct shortname_info { 258 struct shortname_info {
261 unsigned char lower:1, 259 unsigned char lower:1,
262 upper:1, 260 upper:1,
263 valid:1; 261 valid:1;
264 }; 262 };
265 #define INIT_SHORTNAME_INFO(x) do { \ 263 #define INIT_SHORTNAME_INFO(x) do { \
266 (x)->lower = 1; \ 264 (x)->lower = 1; \
267 (x)->upper = 1; \ 265 (x)->upper = 1; \
268 (x)->valid = 1; \ 266 (x)->valid = 1; \
269 } while (0) 267 } while (0)
270 268
271 static inline int to_shortname_char(struct nls_table *nls, 269 static inline int to_shortname_char(struct nls_table *nls,
272 unsigned char *buf, int buf_size, 270 unsigned char *buf, int buf_size,
273 wchar_t *src, struct shortname_info *info) 271 wchar_t *src, struct shortname_info *info)
274 { 272 {
275 int len; 273 int len;
276 274
277 if (vfat_skip_char(*src)) { 275 if (vfat_skip_char(*src)) {
278 info->valid = 0; 276 info->valid = 0;
279 return 0; 277 return 0;
280 } 278 }
281 if (vfat_replace_char(*src)) { 279 if (vfat_replace_char(*src)) {
282 info->valid = 0; 280 info->valid = 0;
283 buf[0] = '_'; 281 buf[0] = '_';
284 return 1; 282 return 1;
285 } 283 }
286 284
287 len = nls->uni2char(*src, buf, buf_size); 285 len = nls->uni2char(*src, buf, buf_size);
288 if (len <= 0) { 286 if (len <= 0) {
289 info->valid = 0; 287 info->valid = 0;
290 buf[0] = '_'; 288 buf[0] = '_';
291 len = 1; 289 len = 1;
292 } else if (len == 1) { 290 } else if (len == 1) {
293 unsigned char prev = buf[0]; 291 unsigned char prev = buf[0];
294 292
295 if (buf[0] >= 0x7F) { 293 if (buf[0] >= 0x7F) {
296 info->lower = 0; 294 info->lower = 0;
297 info->upper = 0; 295 info->upper = 0;
298 } 296 }
299 297
300 buf[0] = nls_toupper(nls, buf[0]); 298 buf[0] = nls_toupper(nls, buf[0]);
301 if (isalpha(buf[0])) { 299 if (isalpha(buf[0])) {
302 if (buf[0] == prev) 300 if (buf[0] == prev)
303 info->lower = 0; 301 info->lower = 0;
304 else 302 else
305 info->upper = 0; 303 info->upper = 0;
306 } 304 }
307 } else { 305 } else {
308 info->lower = 0; 306 info->lower = 0;
309 info->upper = 0; 307 info->upper = 0;
310 } 308 }
311 309
312 return len; 310 return len;
313 } 311 }
314 312
315 /* 313 /*
316 * Given a valid longname, create a unique shortname. Make sure the 314 * Given a valid longname, create a unique shortname. Make sure the
317 * shortname does not exist 315 * shortname does not exist
318 * Returns negative number on error, 0 for a normal 316 * Returns negative number on error, 0 for a normal
319 * return, and 1 for valid shortname 317 * return, and 1 for valid shortname
320 */ 318 */
321 static int vfat_create_shortname(struct inode *dir, struct nls_table *nls, 319 static int vfat_create_shortname(struct inode *dir, struct nls_table *nls,
322 wchar_t *uname, int ulen, 320 wchar_t *uname, int ulen,
323 unsigned char *name_res, unsigned char *lcase) 321 unsigned char *name_res, unsigned char *lcase)
324 { 322 {
325 struct fat_mount_options *opts = &MSDOS_SB(dir->i_sb)->options; 323 struct fat_mount_options *opts = &MSDOS_SB(dir->i_sb)->options;
326 wchar_t *ip, *ext_start, *end, *name_start; 324 wchar_t *ip, *ext_start, *end, *name_start;
327 unsigned char base[9], ext[4], buf[5], *p; 325 unsigned char base[9], ext[4], buf[5], *p;
328 unsigned char charbuf[NLS_MAX_CHARSET_SIZE]; 326 unsigned char charbuf[NLS_MAX_CHARSET_SIZE];
329 int chl, chi; 327 int chl, chi;
330 int sz = 0, extlen, baselen, i, numtail_baselen, numtail2_baselen; 328 int sz = 0, extlen, baselen, i, numtail_baselen, numtail2_baselen;
331 int is_shortname; 329 int is_shortname;
332 struct shortname_info base_info, ext_info; 330 struct shortname_info base_info, ext_info;
333 331
334 is_shortname = 1; 332 is_shortname = 1;
335 INIT_SHORTNAME_INFO(&base_info); 333 INIT_SHORTNAME_INFO(&base_info);
336 INIT_SHORTNAME_INFO(&ext_info); 334 INIT_SHORTNAME_INFO(&ext_info);
337 335
338 /* Now, we need to create a shortname from the long name */ 336 /* Now, we need to create a shortname from the long name */
339 ext_start = end = &uname[ulen]; 337 ext_start = end = &uname[ulen];
340 while (--ext_start >= uname) { 338 while (--ext_start >= uname) {
341 if (*ext_start == 0x002E) { /* is `.' */ 339 if (*ext_start == 0x002E) { /* is `.' */
342 if (ext_start == end - 1) { 340 if (ext_start == end - 1) {
343 sz = ulen; 341 sz = ulen;
344 ext_start = NULL; 342 ext_start = NULL;
345 } 343 }
346 break; 344 break;
347 } 345 }
348 } 346 }
349 347
350 if (ext_start == uname - 1) { 348 if (ext_start == uname - 1) {
351 sz = ulen; 349 sz = ulen;
352 ext_start = NULL; 350 ext_start = NULL;
353 } else if (ext_start) { 351 } else if (ext_start) {
354 /* 352 /*
355 * Names which start with a dot could be just 353 * Names which start with a dot could be just
356 * an extension eg. "...test". In this case Win95 354 * an extension eg. "...test". In this case Win95
357 * uses the extension as the name and sets no extension. 355 * uses the extension as the name and sets no extension.
358 */ 356 */
359 name_start = &uname[0]; 357 name_start = &uname[0];
360 while (name_start < ext_start) { 358 while (name_start < ext_start) {
361 if (!vfat_skip_char(*name_start)) 359 if (!vfat_skip_char(*name_start))
362 break; 360 break;
363 name_start++; 361 name_start++;
364 } 362 }
365 if (name_start != ext_start) { 363 if (name_start != ext_start) {
366 sz = ext_start - uname; 364 sz = ext_start - uname;
367 ext_start++; 365 ext_start++;
368 } else { 366 } else {
369 sz = ulen; 367 sz = ulen;
370 ext_start = NULL; 368 ext_start = NULL;
371 } 369 }
372 } 370 }
373 371
374 numtail_baselen = 6; 372 numtail_baselen = 6;
375 numtail2_baselen = 2; 373 numtail2_baselen = 2;
376 for (baselen = i = 0, p = base, ip = uname; i < sz; i++, ip++) { 374 for (baselen = i = 0, p = base, ip = uname; i < sz; i++, ip++) {
377 chl = to_shortname_char(nls, charbuf, sizeof(charbuf), 375 chl = to_shortname_char(nls, charbuf, sizeof(charbuf),
378 ip, &base_info); 376 ip, &base_info);
379 if (chl == 0) 377 if (chl == 0)
380 continue; 378 continue;
381 379
382 if (baselen < 2 && (baselen + chl) > 2) 380 if (baselen < 2 && (baselen + chl) > 2)
383 numtail2_baselen = baselen; 381 numtail2_baselen = baselen;
384 if (baselen < 6 && (baselen + chl) > 6) 382 if (baselen < 6 && (baselen + chl) > 6)
385 numtail_baselen = baselen; 383 numtail_baselen = baselen;
386 for (chi = 0; chi < chl; chi++) { 384 for (chi = 0; chi < chl; chi++) {
387 *p++ = charbuf[chi]; 385 *p++ = charbuf[chi];
388 baselen++; 386 baselen++;
389 if (baselen >= 8) 387 if (baselen >= 8)
390 break; 388 break;
391 } 389 }
392 if (baselen >= 8) { 390 if (baselen >= 8) {
393 if ((chi < chl - 1) || (ip + 1) - uname < sz) 391 if ((chi < chl - 1) || (ip + 1) - uname < sz)
394 is_shortname = 0; 392 is_shortname = 0;
395 break; 393 break;
396 } 394 }
397 } 395 }
398 if (baselen == 0) { 396 if (baselen == 0) {
399 return -EINVAL; 397 return -EINVAL;
400 } 398 }
401 399
402 extlen = 0; 400 extlen = 0;
403 if (ext_start) { 401 if (ext_start) {
404 for (p = ext, ip = ext_start; extlen < 3 && ip < end; ip++) { 402 for (p = ext, ip = ext_start; extlen < 3 && ip < end; ip++) {
405 chl = to_shortname_char(nls, charbuf, sizeof(charbuf), 403 chl = to_shortname_char(nls, charbuf, sizeof(charbuf),
406 ip, &ext_info); 404 ip, &ext_info);
407 if (chl == 0) 405 if (chl == 0)
408 continue; 406 continue;
409 407
410 if ((extlen + chl) > 3) { 408 if ((extlen + chl) > 3) {
411 is_shortname = 0; 409 is_shortname = 0;
412 break; 410 break;
413 } 411 }
414 for (chi = 0; chi < chl; chi++) { 412 for (chi = 0; chi < chl; chi++) {
415 *p++ = charbuf[chi]; 413 *p++ = charbuf[chi];
416 extlen++; 414 extlen++;
417 } 415 }
418 if (extlen >= 3) { 416 if (extlen >= 3) {
419 if (ip + 1 != end) 417 if (ip + 1 != end)
420 is_shortname = 0; 418 is_shortname = 0;
421 break; 419 break;
422 } 420 }
423 } 421 }
424 } 422 }
425 ext[extlen] = '\0'; 423 ext[extlen] = '\0';
426 base[baselen] = '\0'; 424 base[baselen] = '\0';
427 425
428 /* Yes, it can happen. ".\xe5" would do it. */ 426 /* Yes, it can happen. ".\xe5" would do it. */
429 if (base[0] == DELETED_FLAG) 427 if (base[0] == DELETED_FLAG)
430 base[0] = 0x05; 428 base[0] = 0x05;
431 429
432 /* OK, at this point we know that base is not longer than 8 symbols, 430 /* OK, at this point we know that base is not longer than 8 symbols,
433 * ext is not longer than 3, base is nonempty, both don't contain 431 * ext is not longer than 3, base is nonempty, both don't contain
434 * any bad symbols (lowercase transformed to uppercase). 432 * any bad symbols (lowercase transformed to uppercase).
435 */ 433 */
436 434
437 memset(name_res, ' ', MSDOS_NAME); 435 memset(name_res, ' ', MSDOS_NAME);
438 memcpy(name_res, base, baselen); 436 memcpy(name_res, base, baselen);
439 memcpy(name_res + 8, ext, extlen); 437 memcpy(name_res + 8, ext, extlen);
440 *lcase = 0; 438 *lcase = 0;
441 if (is_shortname && base_info.valid && ext_info.valid) { 439 if (is_shortname && base_info.valid && ext_info.valid) {
442 if (vfat_find_form(dir, name_res) == 0) 440 if (vfat_find_form(dir, name_res) == 0)
443 return -EEXIST; 441 return -EEXIST;
444 442
445 if (opts->shortname & VFAT_SFN_CREATE_WIN95) { 443 if (opts->shortname & VFAT_SFN_CREATE_WIN95) {
446 return (base_info.upper && ext_info.upper); 444 return (base_info.upper && ext_info.upper);
447 } else if (opts->shortname & VFAT_SFN_CREATE_WINNT) { 445 } else if (opts->shortname & VFAT_SFN_CREATE_WINNT) {
448 if ((base_info.upper || base_info.lower) && 446 if ((base_info.upper || base_info.lower) &&
449 (ext_info.upper || ext_info.lower)) { 447 (ext_info.upper || ext_info.lower)) {
450 if (!base_info.upper && base_info.lower) 448 if (!base_info.upper && base_info.lower)
451 *lcase |= CASE_LOWER_BASE; 449 *lcase |= CASE_LOWER_BASE;
452 if (!ext_info.upper && ext_info.lower) 450 if (!ext_info.upper && ext_info.lower)
453 *lcase |= CASE_LOWER_EXT; 451 *lcase |= CASE_LOWER_EXT;
454 return 1; 452 return 1;
455 } 453 }
456 return 0; 454 return 0;
457 } else { 455 } else {
458 BUG(); 456 BUG();
459 } 457 }
460 } 458 }
461 459
462 if (opts->numtail == 0) 460 if (opts->numtail == 0)
463 if (vfat_find_form(dir, name_res) < 0) 461 if (vfat_find_form(dir, name_res) < 0)
464 return 0; 462 return 0;
465 463
466 /* 464 /*
467 * Try to find a unique extension. This used to 465 * Try to find a unique extension. This used to
468 * iterate through all possibilities sequentially, 466 * iterate through all possibilities sequentially,
469 * but that gave extremely bad performance. Windows 467 * but that gave extremely bad performance. Windows
470 * only tries a few cases before using random 468 * only tries a few cases before using random
471 * values for part of the base. 469 * values for part of the base.
472 */ 470 */
473 471
474 if (baselen > 6) { 472 if (baselen > 6) {
475 baselen = numtail_baselen; 473 baselen = numtail_baselen;
476 name_res[7] = ' '; 474 name_res[7] = ' ';
477 } 475 }
478 name_res[baselen] = '~'; 476 name_res[baselen] = '~';
479 for (i = 1; i < 10; i++) { 477 for (i = 1; i < 10; i++) {
480 name_res[baselen + 1] = i + '0'; 478 name_res[baselen + 1] = i + '0';
481 if (vfat_find_form(dir, name_res) < 0) 479 if (vfat_find_form(dir, name_res) < 0)
482 return 0; 480 return 0;
483 } 481 }
484 482
485 i = jiffies; 483 i = jiffies;
486 sz = (jiffies >> 16) & 0x7; 484 sz = (jiffies >> 16) & 0x7;
487 if (baselen > 2) { 485 if (baselen > 2) {
488 baselen = numtail2_baselen; 486 baselen = numtail2_baselen;
489 name_res[7] = ' '; 487 name_res[7] = ' ';
490 } 488 }
491 name_res[baselen + 4] = '~'; 489 name_res[baselen + 4] = '~';
492 name_res[baselen + 5] = '1' + sz; 490 name_res[baselen + 5] = '1' + sz;
493 while (1) { 491 while (1) {
494 snprintf(buf, sizeof(buf), "%04X", i & 0xffff); 492 snprintf(buf, sizeof(buf), "%04X", i & 0xffff);
495 memcpy(&name_res[baselen], buf, 4); 493 memcpy(&name_res[baselen], buf, 4);
496 if (vfat_find_form(dir, name_res) < 0) 494 if (vfat_find_form(dir, name_res) < 0)
497 break; 495 break;
498 i -= 11; 496 i -= 11;
499 } 497 }
500 return 0; 498 return 0;
501 } 499 }
502 500
503 /* Translate a string, including coded sequences into Unicode */ 501 /* Translate a string, including coded sequences into Unicode */
504 static int 502 static int
505 xlate_to_uni(const unsigned char *name, int len, unsigned char *outname, 503 xlate_to_uni(const unsigned char *name, int len, unsigned char *outname,
506 int *longlen, int *outlen, int escape, int utf8, 504 int *longlen, int *outlen, int escape, int utf8,
507 struct nls_table *nls) 505 struct nls_table *nls)
508 { 506 {
509 const unsigned char *ip; 507 const unsigned char *ip;
510 unsigned char nc; 508 unsigned char nc;
511 unsigned char *op; 509 unsigned char *op;
512 unsigned int ec; 510 unsigned int ec;
513 int i, k, fill; 511 int i, k, fill;
514 int charlen; 512 int charlen;
515 513
516 if (utf8) { 514 if (utf8) {
517 *outlen = utf8s_to_utf16s(name, len, (wchar_t *)outname); 515 *outlen = utf8s_to_utf16s(name, len, (wchar_t *)outname);
518 if (*outlen < 0) 516 if (*outlen < 0)
519 return *outlen; 517 return *outlen;
520 else if (*outlen > FAT_LFN_LEN) 518 else if (*outlen > FAT_LFN_LEN)
521 return -ENAMETOOLONG; 519 return -ENAMETOOLONG;
522 520
523 op = &outname[*outlen * sizeof(wchar_t)]; 521 op = &outname[*outlen * sizeof(wchar_t)];
524 } else { 522 } else {
525 if (nls) { 523 if (nls) {
526 for (i = 0, ip = name, op = outname, *outlen = 0; 524 for (i = 0, ip = name, op = outname, *outlen = 0;
527 i < len && *outlen <= FAT_LFN_LEN; 525 i < len && *outlen <= FAT_LFN_LEN;
528 *outlen += 1) 526 *outlen += 1)
529 { 527 {
530 if (escape && (*ip == ':')) { 528 if (escape && (*ip == ':')) {
531 if (i > len - 5) 529 if (i > len - 5)
532 return -EINVAL; 530 return -EINVAL;
533 ec = 0; 531 ec = 0;
534 for (k = 1; k < 5; k++) { 532 for (k = 1; k < 5; k++) {
535 nc = ip[k]; 533 nc = ip[k];
536 ec <<= 4; 534 ec <<= 4;
537 if (nc >= '0' && nc <= '9') { 535 if (nc >= '0' && nc <= '9') {
538 ec |= nc - '0'; 536 ec |= nc - '0';
539 continue; 537 continue;
540 } 538 }
541 if (nc >= 'a' && nc <= 'f') { 539 if (nc >= 'a' && nc <= 'f') {
542 ec |= nc - ('a' - 10); 540 ec |= nc - ('a' - 10);
543 continue; 541 continue;
544 } 542 }
545 if (nc >= 'A' && nc <= 'F') { 543 if (nc >= 'A' && nc <= 'F') {
546 ec |= nc - ('A' - 10); 544 ec |= nc - ('A' - 10);
547 continue; 545 continue;
548 } 546 }
549 return -EINVAL; 547 return -EINVAL;
550 } 548 }
551 *op++ = ec & 0xFF; 549 *op++ = ec & 0xFF;
552 *op++ = ec >> 8; 550 *op++ = ec >> 8;
553 ip += 5; 551 ip += 5;
554 i += 5; 552 i += 5;
555 } else { 553 } else {
556 if ((charlen = nls->char2uni(ip, len - i, (wchar_t *)op)) < 0) 554 if ((charlen = nls->char2uni(ip, len - i, (wchar_t *)op)) < 0)
557 return -EINVAL; 555 return -EINVAL;
558 ip += charlen; 556 ip += charlen;
559 i += charlen; 557 i += charlen;
560 op += 2; 558 op += 2;
561 } 559 }
562 } 560 }
563 if (i < len) 561 if (i < len)
564 return -ENAMETOOLONG; 562 return -ENAMETOOLONG;
565 } else { 563 } else {
566 for (i = 0, ip = name, op = outname, *outlen = 0; 564 for (i = 0, ip = name, op = outname, *outlen = 0;
567 i < len && *outlen <= FAT_LFN_LEN; 565 i < len && *outlen <= FAT_LFN_LEN;
568 i++, *outlen += 1) 566 i++, *outlen += 1)
569 { 567 {
570 *op++ = *ip++; 568 *op++ = *ip++;
571 *op++ = 0; 569 *op++ = 0;
572 } 570 }
573 if (i < len) 571 if (i < len)
574 return -ENAMETOOLONG; 572 return -ENAMETOOLONG;
575 } 573 }
576 } 574 }
577 575
578 *longlen = *outlen; 576 *longlen = *outlen;
579 if (*outlen % 13) { 577 if (*outlen % 13) {
580 *op++ = 0; 578 *op++ = 0;
581 *op++ = 0; 579 *op++ = 0;
582 *outlen += 1; 580 *outlen += 1;
583 if (*outlen % 13) { 581 if (*outlen % 13) {
584 fill = 13 - (*outlen % 13); 582 fill = 13 - (*outlen % 13);
585 for (i = 0; i < fill; i++) { 583 for (i = 0; i < fill; i++) {
586 *op++ = 0xff; 584 *op++ = 0xff;
587 *op++ = 0xff; 585 *op++ = 0xff;
588 } 586 }
589 *outlen += fill; 587 *outlen += fill;
590 } 588 }
591 } 589 }
592 590
593 return 0; 591 return 0;
594 } 592 }
595 593
596 static int vfat_build_slots(struct inode *dir, const unsigned char *name, 594 static int vfat_build_slots(struct inode *dir, const unsigned char *name,
597 int len, int is_dir, int cluster, 595 int len, int is_dir, int cluster,
598 struct timespec *ts, 596 struct timespec *ts,
599 struct msdos_dir_slot *slots, int *nr_slots) 597 struct msdos_dir_slot *slots, int *nr_slots)
600 { 598 {
601 struct msdos_sb_info *sbi = MSDOS_SB(dir->i_sb); 599 struct msdos_sb_info *sbi = MSDOS_SB(dir->i_sb);
602 struct fat_mount_options *opts = &sbi->options; 600 struct fat_mount_options *opts = &sbi->options;
603 struct msdos_dir_slot *ps; 601 struct msdos_dir_slot *ps;
604 struct msdos_dir_entry *de; 602 struct msdos_dir_entry *de;
605 unsigned char cksum, lcase; 603 unsigned char cksum, lcase;
606 unsigned char msdos_name[MSDOS_NAME]; 604 unsigned char msdos_name[MSDOS_NAME];
607 wchar_t *uname; 605 wchar_t *uname;
608 __le16 time, date; 606 __le16 time, date;
609 u8 time_cs; 607 u8 time_cs;
610 int err, ulen, usize, i; 608 int err, ulen, usize, i;
611 loff_t offset; 609 loff_t offset;
612 610
613 *nr_slots = 0; 611 *nr_slots = 0;
614 612
615 uname = __getname(); 613 uname = __getname();
616 if (!uname) 614 if (!uname)
617 return -ENOMEM; 615 return -ENOMEM;
618 616
619 err = xlate_to_uni(name, len, (unsigned char *)uname, &ulen, &usize, 617 err = xlate_to_uni(name, len, (unsigned char *)uname, &ulen, &usize,
620 opts->unicode_xlate, opts->utf8, sbi->nls_io); 618 opts->unicode_xlate, opts->utf8, sbi->nls_io);
621 if (err) 619 if (err)
622 goto out_free; 620 goto out_free;
623 621
624 err = vfat_is_used_badchars(uname, ulen); 622 err = vfat_is_used_badchars(uname, ulen);
625 if (err) 623 if (err)
626 goto out_free; 624 goto out_free;
627 625
628 err = vfat_create_shortname(dir, sbi->nls_disk, uname, ulen, 626 err = vfat_create_shortname(dir, sbi->nls_disk, uname, ulen,
629 msdos_name, &lcase); 627 msdos_name, &lcase);
630 if (err < 0) 628 if (err < 0)
631 goto out_free; 629 goto out_free;
632 else if (err == 1) { 630 else if (err == 1) {
633 de = (struct msdos_dir_entry *)slots; 631 de = (struct msdos_dir_entry *)slots;
634 err = 0; 632 err = 0;
635 goto shortname; 633 goto shortname;
636 } 634 }
637 635
638 /* build the entry of long file name */ 636 /* build the entry of long file name */
639 cksum = fat_checksum(msdos_name); 637 cksum = fat_checksum(msdos_name);
640 638
641 *nr_slots = usize / 13; 639 *nr_slots = usize / 13;
642 for (ps = slots, i = *nr_slots; i > 0; i--, ps++) { 640 for (ps = slots, i = *nr_slots; i > 0; i--, ps++) {
643 ps->id = i; 641 ps->id = i;
644 ps->attr = ATTR_EXT; 642 ps->attr = ATTR_EXT;
645 ps->reserved = 0; 643 ps->reserved = 0;
646 ps->alias_checksum = cksum; 644 ps->alias_checksum = cksum;
647 ps->start = 0; 645 ps->start = 0;
648 offset = (i - 1) * 13; 646 offset = (i - 1) * 13;
649 fatwchar_to16(ps->name0_4, uname + offset, 5); 647 fatwchar_to16(ps->name0_4, uname + offset, 5);
650 fatwchar_to16(ps->name5_10, uname + offset + 5, 6); 648 fatwchar_to16(ps->name5_10, uname + offset + 5, 6);
651 fatwchar_to16(ps->name11_12, uname + offset + 11, 2); 649 fatwchar_to16(ps->name11_12, uname + offset + 11, 2);
652 } 650 }
653 slots[0].id |= 0x40; 651 slots[0].id |= 0x40;
654 de = (struct msdos_dir_entry *)ps; 652 de = (struct msdos_dir_entry *)ps;
655 653
656 shortname: 654 shortname:
657 /* build the entry of 8.3 alias name */ 655 /* build the entry of 8.3 alias name */
658 (*nr_slots)++; 656 (*nr_slots)++;
659 memcpy(de->name, msdos_name, MSDOS_NAME); 657 memcpy(de->name, msdos_name, MSDOS_NAME);
660 de->attr = is_dir ? ATTR_DIR : ATTR_ARCH; 658 de->attr = is_dir ? ATTR_DIR : ATTR_ARCH;
661 de->lcase = lcase; 659 de->lcase = lcase;
662 fat_time_unix2fat(sbi, ts, &time, &date, &time_cs); 660 fat_time_unix2fat(sbi, ts, &time, &date, &time_cs);
663 de->time = de->ctime = time; 661 de->time = de->ctime = time;
664 de->date = de->cdate = de->adate = date; 662 de->date = de->cdate = de->adate = date;
665 de->ctime_cs = time_cs; 663 de->ctime_cs = time_cs;
666 de->start = cpu_to_le16(cluster); 664 de->start = cpu_to_le16(cluster);
667 de->starthi = cpu_to_le16(cluster >> 16); 665 de->starthi = cpu_to_le16(cluster >> 16);
668 de->size = 0; 666 de->size = 0;
669 out_free: 667 out_free:
670 __putname(uname); 668 __putname(uname);
671 return err; 669 return err;
672 } 670 }
673 671
674 static int vfat_add_entry(struct inode *dir, struct qstr *qname, int is_dir, 672 static int vfat_add_entry(struct inode *dir, struct qstr *qname, int is_dir,
675 int cluster, struct timespec *ts, 673 int cluster, struct timespec *ts,
676 struct fat_slot_info *sinfo) 674 struct fat_slot_info *sinfo)
677 { 675 {
678 struct msdos_dir_slot *slots; 676 struct msdos_dir_slot *slots;
679 unsigned int len; 677 unsigned int len;
680 int err, nr_slots; 678 int err, nr_slots;
681 679
682 len = vfat_striptail_len(qname); 680 len = vfat_striptail_len(qname);
683 if (len == 0) 681 if (len == 0)
684 return -ENOENT; 682 return -ENOENT;
685 683
686 slots = kmalloc(sizeof(*slots) * MSDOS_SLOTS, GFP_NOFS); 684 slots = kmalloc(sizeof(*slots) * MSDOS_SLOTS, GFP_NOFS);
687 if (slots == NULL) 685 if (slots == NULL)
688 return -ENOMEM; 686 return -ENOMEM;
689 687
690 err = vfat_build_slots(dir, qname->name, len, is_dir, cluster, ts, 688 err = vfat_build_slots(dir, qname->name, len, is_dir, cluster, ts,
691 slots, &nr_slots); 689 slots, &nr_slots);
692 if (err) 690 if (err)
693 goto cleanup; 691 goto cleanup;
694 692
695 err = fat_add_entries(dir, slots, nr_slots, sinfo); 693 err = fat_add_entries(dir, slots, nr_slots, sinfo);
696 if (err) 694 if (err)
697 goto cleanup; 695 goto cleanup;
698 696
699 /* update timestamp */ 697 /* update timestamp */
700 dir->i_ctime = dir->i_mtime = dir->i_atime = *ts; 698 dir->i_ctime = dir->i_mtime = dir->i_atime = *ts;
701 if (IS_DIRSYNC(dir)) 699 if (IS_DIRSYNC(dir))
702 (void)fat_sync_inode(dir); 700 (void)fat_sync_inode(dir);
703 else 701 else
704 mark_inode_dirty(dir); 702 mark_inode_dirty(dir);
705 cleanup: 703 cleanup:
706 kfree(slots); 704 kfree(slots);
707 return err; 705 return err;
708 } 706 }
709 707
710 static int vfat_find(struct inode *dir, struct qstr *qname, 708 static int vfat_find(struct inode *dir, struct qstr *qname,
711 struct fat_slot_info *sinfo) 709 struct fat_slot_info *sinfo)
712 { 710 {
713 unsigned int len = vfat_striptail_len(qname); 711 unsigned int len = vfat_striptail_len(qname);
714 if (len == 0) 712 if (len == 0)
715 return -ENOENT; 713 return -ENOENT;
716 return fat_search_long(dir, qname->name, len, sinfo); 714 return fat_search_long(dir, qname->name, len, sinfo);
717 } 715 }
718 716
719 /* 717 /*
720 * (nfsd's) anonymous disconnected dentry? 718 * (nfsd's) anonymous disconnected dentry?
721 * NOTE: !IS_ROOT() is not anonymous (I.e. d_splice_alias() did the job). 719 * NOTE: !IS_ROOT() is not anonymous (I.e. d_splice_alias() did the job).
722 */ 720 */
723 static int vfat_d_anon_disconn(struct dentry *dentry) 721 static int vfat_d_anon_disconn(struct dentry *dentry)
724 { 722 {
725 return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED); 723 return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
726 } 724 }
727 725
728 static struct dentry *vfat_lookup(struct inode *dir, struct dentry *dentry, 726 static struct dentry *vfat_lookup(struct inode *dir, struct dentry *dentry,
729 struct nameidata *nd) 727 struct nameidata *nd)
730 { 728 {
731 struct super_block *sb = dir->i_sb; 729 struct super_block *sb = dir->i_sb;
732 struct fat_slot_info sinfo; 730 struct fat_slot_info sinfo;
733 struct inode *inode; 731 struct inode *inode;
734 struct dentry *alias; 732 struct dentry *alias;
735 int err; 733 int err;
736 734
737 lock_super(sb); 735 lock_super(sb);
738 736
739 err = vfat_find(dir, &dentry->d_name, &sinfo); 737 err = vfat_find(dir, &dentry->d_name, &sinfo);
740 if (err) { 738 if (err) {
741 if (err == -ENOENT) { 739 if (err == -ENOENT) {
742 inode = NULL; 740 inode = NULL;
743 goto out; 741 goto out;
744 } 742 }
745 goto error; 743 goto error;
746 } 744 }
747 745
748 inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos); 746 inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
749 brelse(sinfo.bh); 747 brelse(sinfo.bh);
750 if (IS_ERR(inode)) { 748 if (IS_ERR(inode)) {
751 err = PTR_ERR(inode); 749 err = PTR_ERR(inode);
752 goto error; 750 goto error;
753 } 751 }
754 752
755 alias = d_find_alias(inode); 753 alias = d_find_alias(inode);
756 if (alias && !vfat_d_anon_disconn(alias)) { 754 if (alias && !vfat_d_anon_disconn(alias)) {
757 /* 755 /*
758 * This inode has non anonymous-DCACHE_DISCONNECTED 756 * This inode has non anonymous-DCACHE_DISCONNECTED
759 * dentry. This means, the user did ->lookup() by an 757 * dentry. This means, the user did ->lookup() by an
760 * another name (longname vs 8.3 alias of it) in past. 758 * another name (longname vs 8.3 alias of it) in past.
761 * 759 *
762 * Switch to new one for reason of locality if possible. 760 * Switch to new one for reason of locality if possible.
763 */ 761 */
764 BUG_ON(d_unhashed(alias)); 762 BUG_ON(d_unhashed(alias));
765 if (!S_ISDIR(inode->i_mode)) 763 if (!S_ISDIR(inode->i_mode))
766 d_move(alias, dentry); 764 d_move(alias, dentry);
767 iput(inode); 765 iput(inode);
768 unlock_super(sb); 766 unlock_super(sb);
769 return alias; 767 return alias;
770 } else 768 } else
771 dput(alias); 769 dput(alias);
772 770
773 out: 771 out:
774 unlock_super(sb); 772 unlock_super(sb);
775 dentry->d_time = dentry->d_parent->d_inode->i_version; 773 dentry->d_time = dentry->d_parent->d_inode->i_version;
776 dentry = d_splice_alias(inode, dentry); 774 dentry = d_splice_alias(inode, dentry);
777 if (dentry) 775 if (dentry)
778 dentry->d_time = dentry->d_parent->d_inode->i_version; 776 dentry->d_time = dentry->d_parent->d_inode->i_version;
779 return dentry; 777 return dentry;
780 778
781 error: 779 error:
782 unlock_super(sb); 780 unlock_super(sb);
783 return ERR_PTR(err); 781 return ERR_PTR(err);
784 } 782 }
785 783
786 static int vfat_create(struct inode *dir, struct dentry *dentry, int mode, 784 static int vfat_create(struct inode *dir, struct dentry *dentry, int mode,
787 struct nameidata *nd) 785 struct nameidata *nd)
788 { 786 {
789 struct super_block *sb = dir->i_sb; 787 struct super_block *sb = dir->i_sb;
790 struct inode *inode; 788 struct inode *inode;
791 struct fat_slot_info sinfo; 789 struct fat_slot_info sinfo;
792 struct timespec ts; 790 struct timespec ts;
793 int err; 791 int err;
794 792
795 lock_super(sb); 793 lock_super(sb);
796 794
797 ts = CURRENT_TIME_SEC; 795 ts = CURRENT_TIME_SEC;
798 err = vfat_add_entry(dir, &dentry->d_name, 0, 0, &ts, &sinfo); 796 err = vfat_add_entry(dir, &dentry->d_name, 0, 0, &ts, &sinfo);
799 if (err) 797 if (err)
800 goto out; 798 goto out;
801 dir->i_version++; 799 dir->i_version++;
802 800
803 inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos); 801 inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
804 brelse(sinfo.bh); 802 brelse(sinfo.bh);
805 if (IS_ERR(inode)) { 803 if (IS_ERR(inode)) {
806 err = PTR_ERR(inode); 804 err = PTR_ERR(inode);
807 goto out; 805 goto out;
808 } 806 }
809 inode->i_version++; 807 inode->i_version++;
810 inode->i_mtime = inode->i_atime = inode->i_ctime = ts; 808 inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
811 /* timestamp is already written, so mark_inode_dirty() is unneeded. */ 809 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
812 810
813 dentry->d_time = dentry->d_parent->d_inode->i_version; 811 dentry->d_time = dentry->d_parent->d_inode->i_version;
814 d_instantiate(dentry, inode); 812 d_instantiate(dentry, inode);
815 out: 813 out:
816 unlock_super(sb); 814 unlock_super(sb);
817 return err; 815 return err;
818 } 816 }
819 817
820 static int vfat_rmdir(struct inode *dir, struct dentry *dentry) 818 static int vfat_rmdir(struct inode *dir, struct dentry *dentry)
821 { 819 {
822 struct inode *inode = dentry->d_inode; 820 struct inode *inode = dentry->d_inode;
823 struct super_block *sb = dir->i_sb; 821 struct super_block *sb = dir->i_sb;
824 struct fat_slot_info sinfo; 822 struct fat_slot_info sinfo;
825 int err; 823 int err;
826 824
827 lock_super(sb); 825 lock_super(sb);
828 826
829 err = fat_dir_empty(inode); 827 err = fat_dir_empty(inode);
830 if (err) 828 if (err)
831 goto out; 829 goto out;
832 err = vfat_find(dir, &dentry->d_name, &sinfo); 830 err = vfat_find(dir, &dentry->d_name, &sinfo);
833 if (err) 831 if (err)
834 goto out; 832 goto out;
835 833
836 err = fat_remove_entries(dir, &sinfo); /* and releases bh */ 834 err = fat_remove_entries(dir, &sinfo); /* and releases bh */
837 if (err) 835 if (err)
838 goto out; 836 goto out;
839 drop_nlink(dir); 837 drop_nlink(dir);
840 838
841 clear_nlink(inode); 839 clear_nlink(inode);
842 inode->i_mtime = inode->i_atime = CURRENT_TIME_SEC; 840 inode->i_mtime = inode->i_atime = CURRENT_TIME_SEC;
843 fat_detach(inode); 841 fat_detach(inode);
844 out: 842 out:
845 unlock_super(sb); 843 unlock_super(sb);
846 844
847 return err; 845 return err;
848 } 846 }
849 847
850 static int vfat_unlink(struct inode *dir, struct dentry *dentry) 848 static int vfat_unlink(struct inode *dir, struct dentry *dentry)
851 { 849 {
852 struct inode *inode = dentry->d_inode; 850 struct inode *inode = dentry->d_inode;
853 struct super_block *sb = dir->i_sb; 851 struct super_block *sb = dir->i_sb;
854 struct fat_slot_info sinfo; 852 struct fat_slot_info sinfo;
855 int err; 853 int err;
856 854
857 lock_super(sb); 855 lock_super(sb);
858 856
859 err = vfat_find(dir, &dentry->d_name, &sinfo); 857 err = vfat_find(dir, &dentry->d_name, &sinfo);
860 if (err) 858 if (err)
861 goto out; 859 goto out;
862 860
863 err = fat_remove_entries(dir, &sinfo); /* and releases bh */ 861 err = fat_remove_entries(dir, &sinfo); /* and releases bh */
864 if (err) 862 if (err)
865 goto out; 863 goto out;
866 clear_nlink(inode); 864 clear_nlink(inode);
867 inode->i_mtime = inode->i_atime = CURRENT_TIME_SEC; 865 inode->i_mtime = inode->i_atime = CURRENT_TIME_SEC;
868 fat_detach(inode); 866 fat_detach(inode);
869 out: 867 out:
870 unlock_super(sb); 868 unlock_super(sb);
871 869
872 return err; 870 return err;
873 } 871 }
874 872
875 static int vfat_mkdir(struct inode *dir, struct dentry *dentry, int mode) 873 static int vfat_mkdir(struct inode *dir, struct dentry *dentry, int mode)
876 { 874 {
877 struct super_block *sb = dir->i_sb; 875 struct super_block *sb = dir->i_sb;
878 struct inode *inode; 876 struct inode *inode;
879 struct fat_slot_info sinfo; 877 struct fat_slot_info sinfo;
880 struct timespec ts; 878 struct timespec ts;
881 int err, cluster; 879 int err, cluster;
882 880
883 lock_super(sb); 881 lock_super(sb);
884 882
885 ts = CURRENT_TIME_SEC; 883 ts = CURRENT_TIME_SEC;
886 cluster = fat_alloc_new_dir(dir, &ts); 884 cluster = fat_alloc_new_dir(dir, &ts);
887 if (cluster < 0) { 885 if (cluster < 0) {
888 err = cluster; 886 err = cluster;
889 goto out; 887 goto out;
890 } 888 }
891 err = vfat_add_entry(dir, &dentry->d_name, 1, cluster, &ts, &sinfo); 889 err = vfat_add_entry(dir, &dentry->d_name, 1, cluster, &ts, &sinfo);
892 if (err) 890 if (err)
893 goto out_free; 891 goto out_free;
894 dir->i_version++; 892 dir->i_version++;
895 inc_nlink(dir); 893 inc_nlink(dir);
896 894
897 inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos); 895 inode = fat_build_inode(sb, sinfo.de, sinfo.i_pos);
898 brelse(sinfo.bh); 896 brelse(sinfo.bh);
899 if (IS_ERR(inode)) { 897 if (IS_ERR(inode)) {
900 err = PTR_ERR(inode); 898 err = PTR_ERR(inode);
901 /* the directory was completed, just return a error */ 899 /* the directory was completed, just return a error */
902 goto out; 900 goto out;
903 } 901 }
904 inode->i_version++; 902 inode->i_version++;
905 inode->i_nlink = 2; 903 inode->i_nlink = 2;
906 inode->i_mtime = inode->i_atime = inode->i_ctime = ts; 904 inode->i_mtime = inode->i_atime = inode->i_ctime = ts;
907 /* timestamp is already written, so mark_inode_dirty() is unneeded. */ 905 /* timestamp is already written, so mark_inode_dirty() is unneeded. */
908 906
909 dentry->d_time = dentry->d_parent->d_inode->i_version; 907 dentry->d_time = dentry->d_parent->d_inode->i_version;
910 d_instantiate(dentry, inode); 908 d_instantiate(dentry, inode);
911 909
912 unlock_super(sb); 910 unlock_super(sb);
913 return 0; 911 return 0;
914 912
915 out_free: 913 out_free:
916 fat_free_clusters(dir, cluster); 914 fat_free_clusters(dir, cluster);
917 out: 915 out:
918 unlock_super(sb); 916 unlock_super(sb);
919 return err; 917 return err;
920 } 918 }
921 919
922 static int vfat_rename(struct inode *old_dir, struct dentry *old_dentry, 920 static int vfat_rename(struct inode *old_dir, struct dentry *old_dentry,
923 struct inode *new_dir, struct dentry *new_dentry) 921 struct inode *new_dir, struct dentry *new_dentry)
924 { 922 {
925 struct buffer_head *dotdot_bh; 923 struct buffer_head *dotdot_bh;
926 struct msdos_dir_entry *dotdot_de; 924 struct msdos_dir_entry *dotdot_de;
927 struct inode *old_inode, *new_inode; 925 struct inode *old_inode, *new_inode;
928 struct fat_slot_info old_sinfo, sinfo; 926 struct fat_slot_info old_sinfo, sinfo;
929 struct timespec ts; 927 struct timespec ts;
930 loff_t dotdot_i_pos, new_i_pos; 928 loff_t dotdot_i_pos, new_i_pos;
931 int err, is_dir, update_dotdot, corrupt = 0; 929 int err, is_dir, update_dotdot, corrupt = 0;
932 struct super_block *sb = old_dir->i_sb; 930 struct super_block *sb = old_dir->i_sb;
933 931
934 old_sinfo.bh = sinfo.bh = dotdot_bh = NULL; 932 old_sinfo.bh = sinfo.bh = dotdot_bh = NULL;
935 old_inode = old_dentry->d_inode; 933 old_inode = old_dentry->d_inode;
936 new_inode = new_dentry->d_inode; 934 new_inode = new_dentry->d_inode;
937 lock_super(sb); 935 lock_super(sb);
938 err = vfat_find(old_dir, &old_dentry->d_name, &old_sinfo); 936 err = vfat_find(old_dir, &old_dentry->d_name, &old_sinfo);
939 if (err) 937 if (err)
940 goto out; 938 goto out;
941 939
942 is_dir = S_ISDIR(old_inode->i_mode); 940 is_dir = S_ISDIR(old_inode->i_mode);
943 update_dotdot = (is_dir && old_dir != new_dir); 941 update_dotdot = (is_dir && old_dir != new_dir);
944 if (update_dotdot) { 942 if (update_dotdot) {
945 if (fat_get_dotdot_entry(old_inode, &dotdot_bh, &dotdot_de, 943 if (fat_get_dotdot_entry(old_inode, &dotdot_bh, &dotdot_de,
946 &dotdot_i_pos) < 0) { 944 &dotdot_i_pos) < 0) {
947 err = -EIO; 945 err = -EIO;
948 goto out; 946 goto out;
949 } 947 }
950 } 948 }
951 949
952 ts = CURRENT_TIME_SEC; 950 ts = CURRENT_TIME_SEC;
953 if (new_inode) { 951 if (new_inode) {
954 if (is_dir) { 952 if (is_dir) {
955 err = fat_dir_empty(new_inode); 953 err = fat_dir_empty(new_inode);
956 if (err) 954 if (err)
957 goto out; 955 goto out;
958 } 956 }
959 new_i_pos = MSDOS_I(new_inode)->i_pos; 957 new_i_pos = MSDOS_I(new_inode)->i_pos;
960 fat_detach(new_inode); 958 fat_detach(new_inode);
961 } else { 959 } else {
962 err = vfat_add_entry(new_dir, &new_dentry->d_name, is_dir, 0, 960 err = vfat_add_entry(new_dir, &new_dentry->d_name, is_dir, 0,
963 &ts, &sinfo); 961 &ts, &sinfo);
964 if (err) 962 if (err)
965 goto out; 963 goto out;
966 new_i_pos = sinfo.i_pos; 964 new_i_pos = sinfo.i_pos;
967 } 965 }
968 new_dir->i_version++; 966 new_dir->i_version++;
969 967
970 fat_detach(old_inode); 968 fat_detach(old_inode);
971 fat_attach(old_inode, new_i_pos); 969 fat_attach(old_inode, new_i_pos);
972 if (IS_DIRSYNC(new_dir)) { 970 if (IS_DIRSYNC(new_dir)) {
973 err = fat_sync_inode(old_inode); 971 err = fat_sync_inode(old_inode);
974 if (err) 972 if (err)
975 goto error_inode; 973 goto error_inode;
976 } else 974 } else
977 mark_inode_dirty(old_inode); 975 mark_inode_dirty(old_inode);
978 976
979 if (update_dotdot) { 977 if (update_dotdot) {
980 int start = MSDOS_I(new_dir)->i_logstart; 978 int start = MSDOS_I(new_dir)->i_logstart;
981 dotdot_de->start = cpu_to_le16(start); 979 dotdot_de->start = cpu_to_le16(start);
982 dotdot_de->starthi = cpu_to_le16(start >> 16); 980 dotdot_de->starthi = cpu_to_le16(start >> 16);
983 mark_buffer_dirty_inode(dotdot_bh, old_inode); 981 mark_buffer_dirty_inode(dotdot_bh, old_inode);
984 if (IS_DIRSYNC(new_dir)) { 982 if (IS_DIRSYNC(new_dir)) {
985 err = sync_dirty_buffer(dotdot_bh); 983 err = sync_dirty_buffer(dotdot_bh);
986 if (err) 984 if (err)
987 goto error_dotdot; 985 goto error_dotdot;
988 } 986 }
989 drop_nlink(old_dir); 987 drop_nlink(old_dir);
990 if (!new_inode) 988 if (!new_inode)
991 inc_nlink(new_dir); 989 inc_nlink(new_dir);
992 } 990 }
993 991
994 err = fat_remove_entries(old_dir, &old_sinfo); /* and releases bh */ 992 err = fat_remove_entries(old_dir, &old_sinfo); /* and releases bh */
995 old_sinfo.bh = NULL; 993 old_sinfo.bh = NULL;
996 if (err) 994 if (err)
997 goto error_dotdot; 995 goto error_dotdot;
998 old_dir->i_version++; 996 old_dir->i_version++;
999 old_dir->i_ctime = old_dir->i_mtime = ts; 997 old_dir->i_ctime = old_dir->i_mtime = ts;
1000 if (IS_DIRSYNC(old_dir)) 998 if (IS_DIRSYNC(old_dir))
1001 (void)fat_sync_inode(old_dir); 999 (void)fat_sync_inode(old_dir);
1002 else 1000 else
1003 mark_inode_dirty(old_dir); 1001 mark_inode_dirty(old_dir);
1004 1002
1005 if (new_inode) { 1003 if (new_inode) {
1006 drop_nlink(new_inode); 1004 drop_nlink(new_inode);
1007 if (is_dir) 1005 if (is_dir)
1008 drop_nlink(new_inode); 1006 drop_nlink(new_inode);
1009 new_inode->i_ctime = ts; 1007 new_inode->i_ctime = ts;
1010 } 1008 }
1011 out: 1009 out:
1012 brelse(sinfo.bh); 1010 brelse(sinfo.bh);
1013 brelse(dotdot_bh); 1011 brelse(dotdot_bh);
1014 brelse(old_sinfo.bh); 1012 brelse(old_sinfo.bh);
1015 unlock_super(sb); 1013 unlock_super(sb);
1016 1014
1017 return err; 1015 return err;
1018 1016
1019 error_dotdot: 1017 error_dotdot:
1020 /* data cluster is shared, serious corruption */ 1018 /* data cluster is shared, serious corruption */
1021 corrupt = 1; 1019 corrupt = 1;
1022 1020
1023 if (update_dotdot) { 1021 if (update_dotdot) {
1024 int start = MSDOS_I(old_dir)->i_logstart; 1022 int start = MSDOS_I(old_dir)->i_logstart;
1025 dotdot_de->start = cpu_to_le16(start); 1023 dotdot_de->start = cpu_to_le16(start);
1026 dotdot_de->starthi = cpu_to_le16(start >> 16); 1024 dotdot_de->starthi = cpu_to_le16(start >> 16);
1027 mark_buffer_dirty_inode(dotdot_bh, old_inode); 1025 mark_buffer_dirty_inode(dotdot_bh, old_inode);
1028 corrupt |= sync_dirty_buffer(dotdot_bh); 1026 corrupt |= sync_dirty_buffer(dotdot_bh);
1029 } 1027 }
1030 error_inode: 1028 error_inode:
1031 fat_detach(old_inode); 1029 fat_detach(old_inode);
1032 fat_attach(old_inode, old_sinfo.i_pos); 1030 fat_attach(old_inode, old_sinfo.i_pos);
1033 if (new_inode) { 1031 if (new_inode) {
1034 fat_attach(new_inode, new_i_pos); 1032 fat_attach(new_inode, new_i_pos);
1035 if (corrupt) 1033 if (corrupt)
1036 corrupt |= fat_sync_inode(new_inode); 1034 corrupt |= fat_sync_inode(new_inode);
1037 } else { 1035 } else {
1038 /* 1036 /*
1039 * If new entry was not sharing the data cluster, it 1037 * If new entry was not sharing the data cluster, it
1040 * shouldn't be serious corruption. 1038 * shouldn't be serious corruption.
1041 */ 1039 */
1042 int err2 = fat_remove_entries(new_dir, &sinfo); 1040 int err2 = fat_remove_entries(new_dir, &sinfo);
1043 if (corrupt) 1041 if (corrupt)
1044 corrupt |= err2; 1042 corrupt |= err2;
1045 sinfo.bh = NULL; 1043 sinfo.bh = NULL;
1046 } 1044 }
1047 if (corrupt < 0) { 1045 if (corrupt < 0) {
1048 fat_fs_error(new_dir->i_sb, 1046 fat_fs_error(new_dir->i_sb,
1049 "%s: Filesystem corrupted (i_pos %lld)", 1047 "%s: Filesystem corrupted (i_pos %lld)",
1050 __func__, sinfo.i_pos); 1048 __func__, sinfo.i_pos);
1051 } 1049 }
1052 goto out; 1050 goto out;
1053 } 1051 }
1054 1052
1055 static const struct inode_operations vfat_dir_inode_operations = { 1053 static const struct inode_operations vfat_dir_inode_operations = {
1056 .create = vfat_create, 1054 .create = vfat_create,
1057 .lookup = vfat_lookup, 1055 .lookup = vfat_lookup,
1058 .unlink = vfat_unlink, 1056 .unlink = vfat_unlink,
1059 .mkdir = vfat_mkdir, 1057 .mkdir = vfat_mkdir,
1060 .rmdir = vfat_rmdir, 1058 .rmdir = vfat_rmdir,
1061 .rename = vfat_rename, 1059 .rename = vfat_rename,
1062 .setattr = fat_setattr, 1060 .setattr = fat_setattr,
1063 .getattr = fat_getattr, 1061 .getattr = fat_getattr,
1064 }; 1062 };
1065 1063
1066 static void setup(struct super_block *sb) 1064 static void setup(struct super_block *sb)
1067 { 1065 {
1068 MSDOS_SB(sb)->dir_ops = &vfat_dir_inode_operations; 1066 MSDOS_SB(sb)->dir_ops = &vfat_dir_inode_operations;
1069 if (MSDOS_SB(sb)->options.name_check != 's') 1067 if (MSDOS_SB(sb)->options.name_check != 's')
1070 sb->s_d_op = &vfat_ci_dentry_ops; 1068 sb->s_d_op = &vfat_ci_dentry_ops;
1071 else 1069 else
1072 sb->s_d_op = &vfat_dentry_ops; 1070 sb->s_d_op = &vfat_dentry_ops;
1073 } 1071 }
1074 1072
1075 static int vfat_fill_super(struct super_block *sb, void *data, int silent) 1073 static int vfat_fill_super(struct super_block *sb, void *data, int silent)
1076 { 1074 {
1077 return fat_fill_super(sb, data, silent, 1, setup); 1075 return fat_fill_super(sb, data, silent, 1, setup);
1078 } 1076 }
1079 1077
1080 static struct dentry *vfat_mount(struct file_system_type *fs_type, 1078 static struct dentry *vfat_mount(struct file_system_type *fs_type,
1081 int flags, const char *dev_name, 1079 int flags, const char *dev_name,
1082 void *data) 1080 void *data)
1083 { 1081 {
1084 return mount_bdev(fs_type, flags, dev_name, data, vfat_fill_super); 1082 return mount_bdev(fs_type, flags, dev_name, data, vfat_fill_super);
1085 } 1083 }
1086 1084
1087 static struct file_system_type vfat_fs_type = { 1085 static struct file_system_type vfat_fs_type = {
1088 .owner = THIS_MODULE, 1086 .owner = THIS_MODULE,
1089 .name = "vfat", 1087 .name = "vfat",
1090 .mount = vfat_mount, 1088 .mount = vfat_mount,
1091 .kill_sb = kill_block_super, 1089 .kill_sb = kill_block_super,
1092 .fs_flags = FS_REQUIRES_DEV, 1090 .fs_flags = FS_REQUIRES_DEV,
1093 }; 1091 };
1094 1092
1095 static int __init init_vfat_fs(void) 1093 static int __init init_vfat_fs(void)
1096 { 1094 {
1097 return register_filesystem(&vfat_fs_type); 1095 return register_filesystem(&vfat_fs_type);
1098 } 1096 }
1099 1097
1100 static void __exit exit_vfat_fs(void) 1098 static void __exit exit_vfat_fs(void)
1101 { 1099 {
1102 unregister_filesystem(&vfat_fs_type); 1100 unregister_filesystem(&vfat_fs_type);
1103 } 1101 }
1104 1102
1105 MODULE_LICENSE("GPL"); 1103 MODULE_LICENSE("GPL");
1106 MODULE_DESCRIPTION("VFAT filesystem support"); 1104 MODULE_DESCRIPTION("VFAT filesystem support");
1107 MODULE_AUTHOR("Gordon Chaffee"); 1105 MODULE_AUTHOR("Gordon Chaffee");
1108 1106
1109 module_init(init_vfat_fs) 1107 module_init(init_vfat_fs)
1110 module_exit(exit_vfat_fs) 1108 module_exit(exit_vfat_fs)
1111 1109
1 /* 1 /*
2 * Copyright (C) International Business Machines Corp., 2000-2004 2 * Copyright (C) International Business Machines Corp., 2000-2004
3 * Portions Copyright (C) Christoph Hellwig, 2001-2002 3 * Portions Copyright (C) Christoph Hellwig, 2001-2002
4 * 4 *
5 * This program is free software; you can redistribute it and/or modify 5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by 6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or 7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version. 8 * (at your option) any later version.
9 * 9 *
10 * This program is distributed in the hope that it will be useful, 10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details. 13 * the GNU General Public License for more details.
14 * 14 *
15 * You should have received a copy of the GNU General Public License 15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software 16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */ 18 */
19 19
20 #include <linux/fs.h> 20 #include <linux/fs.h>
21 #include <linux/namei.h> 21 #include <linux/namei.h>
22 #include <linux/ctype.h> 22 #include <linux/ctype.h>
23 #include <linux/quotaops.h> 23 #include <linux/quotaops.h>
24 #include <linux/exportfs.h> 24 #include <linux/exportfs.h>
25 #include "jfs_incore.h" 25 #include "jfs_incore.h"
26 #include "jfs_superblock.h" 26 #include "jfs_superblock.h"
27 #include "jfs_inode.h" 27 #include "jfs_inode.h"
28 #include "jfs_dinode.h" 28 #include "jfs_dinode.h"
29 #include "jfs_dmap.h" 29 #include "jfs_dmap.h"
30 #include "jfs_unicode.h" 30 #include "jfs_unicode.h"
31 #include "jfs_metapage.h" 31 #include "jfs_metapage.h"
32 #include "jfs_xattr.h" 32 #include "jfs_xattr.h"
33 #include "jfs_acl.h" 33 #include "jfs_acl.h"
34 #include "jfs_debug.h" 34 #include "jfs_debug.h"
35 35
36 /* 36 /*
37 * forward references 37 * forward references
38 */ 38 */
39 const struct dentry_operations jfs_ci_dentry_operations; 39 const struct dentry_operations jfs_ci_dentry_operations;
40 40
41 static s64 commitZeroLink(tid_t, struct inode *); 41 static s64 commitZeroLink(tid_t, struct inode *);
42 42
43 /* 43 /*
44 * NAME: free_ea_wmap(inode) 44 * NAME: free_ea_wmap(inode)
45 * 45 *
46 * FUNCTION: free uncommitted extended attributes from working map 46 * FUNCTION: free uncommitted extended attributes from working map
47 * 47 *
48 */ 48 */
49 static inline void free_ea_wmap(struct inode *inode) 49 static inline void free_ea_wmap(struct inode *inode)
50 { 50 {
51 dxd_t *ea = &JFS_IP(inode)->ea; 51 dxd_t *ea = &JFS_IP(inode)->ea;
52 52
53 if (ea->flag & DXD_EXTENT) { 53 if (ea->flag & DXD_EXTENT) {
54 /* free EA pages from cache */ 54 /* free EA pages from cache */
55 invalidate_dxd_metapages(inode, *ea); 55 invalidate_dxd_metapages(inode, *ea);
56 dbFree(inode, addressDXD(ea), lengthDXD(ea)); 56 dbFree(inode, addressDXD(ea), lengthDXD(ea));
57 } 57 }
58 ea->flag = 0; 58 ea->flag = 0;
59 } 59 }
60 60
61 /* 61 /*
62 * NAME: jfs_create(dip, dentry, mode) 62 * NAME: jfs_create(dip, dentry, mode)
63 * 63 *
64 * FUNCTION: create a regular file in the parent directory <dip> 64 * FUNCTION: create a regular file in the parent directory <dip>
65 * with name = <from dentry> and mode = <mode> 65 * with name = <from dentry> and mode = <mode>
66 * 66 *
67 * PARAMETER: dip - parent directory vnode 67 * PARAMETER: dip - parent directory vnode
68 * dentry - dentry of new file 68 * dentry - dentry of new file
69 * mode - create mode (rwxrwxrwx). 69 * mode - create mode (rwxrwxrwx).
70 * nd- nd struct 70 * nd- nd struct
71 * 71 *
72 * RETURN: Errors from subroutines 72 * RETURN: Errors from subroutines
73 * 73 *
74 */ 74 */
75 static int jfs_create(struct inode *dip, struct dentry *dentry, int mode, 75 static int jfs_create(struct inode *dip, struct dentry *dentry, int mode,
76 struct nameidata *nd) 76 struct nameidata *nd)
77 { 77 {
78 int rc = 0; 78 int rc = 0;
79 tid_t tid; /* transaction id */ 79 tid_t tid; /* transaction id */
80 struct inode *ip = NULL; /* child directory inode */ 80 struct inode *ip = NULL; /* child directory inode */
81 ino_t ino; 81 ino_t ino;
82 struct component_name dname; /* child directory name */ 82 struct component_name dname; /* child directory name */
83 struct btstack btstack; 83 struct btstack btstack;
84 struct inode *iplist[2]; 84 struct inode *iplist[2];
85 struct tblock *tblk; 85 struct tblock *tblk;
86 86
87 jfs_info("jfs_create: dip:0x%p name:%s", dip, dentry->d_name.name); 87 jfs_info("jfs_create: dip:0x%p name:%s", dip, dentry->d_name.name);
88 88
89 dquot_initialize(dip); 89 dquot_initialize(dip);
90 90
91 /* 91 /*
92 * search parent directory for entry/freespace 92 * search parent directory for entry/freespace
93 * (dtSearch() returns parent directory page pinned) 93 * (dtSearch() returns parent directory page pinned)
94 */ 94 */
95 if ((rc = get_UCSname(&dname, dentry))) 95 if ((rc = get_UCSname(&dname, dentry)))
96 goto out1; 96 goto out1;
97 97
98 /* 98 /*
99 * Either iAlloc() or txBegin() may block. Deadlock can occur if we 99 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
100 * block there while holding dtree page, so we allocate the inode & 100 * block there while holding dtree page, so we allocate the inode &
101 * begin the transaction before we search the directory. 101 * begin the transaction before we search the directory.
102 */ 102 */
103 ip = ialloc(dip, mode); 103 ip = ialloc(dip, mode);
104 if (IS_ERR(ip)) { 104 if (IS_ERR(ip)) {
105 rc = PTR_ERR(ip); 105 rc = PTR_ERR(ip);
106 goto out2; 106 goto out2;
107 } 107 }
108 108
109 tid = txBegin(dip->i_sb, 0); 109 tid = txBegin(dip->i_sb, 0);
110 110
111 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT); 111 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
112 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD); 112 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
113 113
114 rc = jfs_init_acl(tid, ip, dip); 114 rc = jfs_init_acl(tid, ip, dip);
115 if (rc) 115 if (rc)
116 goto out3; 116 goto out3;
117 117
118 rc = jfs_init_security(tid, ip, dip, &dentry->d_name); 118 rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
119 if (rc) { 119 if (rc) {
120 txAbort(tid, 0); 120 txAbort(tid, 0);
121 goto out3; 121 goto out3;
122 } 122 }
123 123
124 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) { 124 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
125 jfs_err("jfs_create: dtSearch returned %d", rc); 125 jfs_err("jfs_create: dtSearch returned %d", rc);
126 txAbort(tid, 0); 126 txAbort(tid, 0);
127 goto out3; 127 goto out3;
128 } 128 }
129 129
130 tblk = tid_to_tblock(tid); 130 tblk = tid_to_tblock(tid);
131 tblk->xflag |= COMMIT_CREATE; 131 tblk->xflag |= COMMIT_CREATE;
132 tblk->ino = ip->i_ino; 132 tblk->ino = ip->i_ino;
133 tblk->u.ixpxd = JFS_IP(ip)->ixpxd; 133 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
134 134
135 iplist[0] = dip; 135 iplist[0] = dip;
136 iplist[1] = ip; 136 iplist[1] = ip;
137 137
138 /* 138 /*
139 * initialize the child XAD tree root in-line in inode 139 * initialize the child XAD tree root in-line in inode
140 */ 140 */
141 xtInitRoot(tid, ip); 141 xtInitRoot(tid, ip);
142 142
143 /* 143 /*
144 * create entry in parent directory for child directory 144 * create entry in parent directory for child directory
145 * (dtInsert() releases parent directory page) 145 * (dtInsert() releases parent directory page)
146 */ 146 */
147 ino = ip->i_ino; 147 ino = ip->i_ino;
148 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) { 148 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
149 if (rc == -EIO) { 149 if (rc == -EIO) {
150 jfs_err("jfs_create: dtInsert returned -EIO"); 150 jfs_err("jfs_create: dtInsert returned -EIO");
151 txAbort(tid, 1); /* Marks Filesystem dirty */ 151 txAbort(tid, 1); /* Marks Filesystem dirty */
152 } else 152 } else
153 txAbort(tid, 0); /* Filesystem full */ 153 txAbort(tid, 0); /* Filesystem full */
154 goto out3; 154 goto out3;
155 } 155 }
156 156
157 ip->i_op = &jfs_file_inode_operations; 157 ip->i_op = &jfs_file_inode_operations;
158 ip->i_fop = &jfs_file_operations; 158 ip->i_fop = &jfs_file_operations;
159 ip->i_mapping->a_ops = &jfs_aops; 159 ip->i_mapping->a_ops = &jfs_aops;
160 160
161 mark_inode_dirty(ip); 161 mark_inode_dirty(ip);
162 162
163 dip->i_ctime = dip->i_mtime = CURRENT_TIME; 163 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
164 164
165 mark_inode_dirty(dip); 165 mark_inode_dirty(dip);
166 166
167 rc = txCommit(tid, 2, &iplist[0], 0); 167 rc = txCommit(tid, 2, &iplist[0], 0);
168 168
169 out3: 169 out3:
170 txEnd(tid); 170 txEnd(tid);
171 mutex_unlock(&JFS_IP(ip)->commit_mutex); 171 mutex_unlock(&JFS_IP(ip)->commit_mutex);
172 mutex_unlock(&JFS_IP(dip)->commit_mutex); 172 mutex_unlock(&JFS_IP(dip)->commit_mutex);
173 if (rc) { 173 if (rc) {
174 free_ea_wmap(ip); 174 free_ea_wmap(ip);
175 ip->i_nlink = 0; 175 ip->i_nlink = 0;
176 unlock_new_inode(ip); 176 unlock_new_inode(ip);
177 iput(ip); 177 iput(ip);
178 } else { 178 } else {
179 d_instantiate(dentry, ip); 179 d_instantiate(dentry, ip);
180 unlock_new_inode(ip); 180 unlock_new_inode(ip);
181 } 181 }
182 182
183 out2: 183 out2:
184 free_UCSname(&dname); 184 free_UCSname(&dname);
185 185
186 out1: 186 out1:
187 187
188 jfs_info("jfs_create: rc:%d", rc); 188 jfs_info("jfs_create: rc:%d", rc);
189 return rc; 189 return rc;
190 } 190 }
191 191
192 192
193 /* 193 /*
194 * NAME: jfs_mkdir(dip, dentry, mode) 194 * NAME: jfs_mkdir(dip, dentry, mode)
195 * 195 *
196 * FUNCTION: create a child directory in the parent directory <dip> 196 * FUNCTION: create a child directory in the parent directory <dip>
197 * with name = <from dentry> and mode = <mode> 197 * with name = <from dentry> and mode = <mode>
198 * 198 *
199 * PARAMETER: dip - parent directory vnode 199 * PARAMETER: dip - parent directory vnode
200 * dentry - dentry of child directory 200 * dentry - dentry of child directory
201 * mode - create mode (rwxrwxrwx). 201 * mode - create mode (rwxrwxrwx).
202 * 202 *
203 * RETURN: Errors from subroutines 203 * RETURN: Errors from subroutines
204 * 204 *
205 * note: 205 * note:
206 * EACCESS: user needs search+write permission on the parent directory 206 * EACCESS: user needs search+write permission on the parent directory
207 */ 207 */
208 static int jfs_mkdir(struct inode *dip, struct dentry *dentry, int mode) 208 static int jfs_mkdir(struct inode *dip, struct dentry *dentry, int mode)
209 { 209 {
210 int rc = 0; 210 int rc = 0;
211 tid_t tid; /* transaction id */ 211 tid_t tid; /* transaction id */
212 struct inode *ip = NULL; /* child directory inode */ 212 struct inode *ip = NULL; /* child directory inode */
213 ino_t ino; 213 ino_t ino;
214 struct component_name dname; /* child directory name */ 214 struct component_name dname; /* child directory name */
215 struct btstack btstack; 215 struct btstack btstack;
216 struct inode *iplist[2]; 216 struct inode *iplist[2];
217 struct tblock *tblk; 217 struct tblock *tblk;
218 218
219 jfs_info("jfs_mkdir: dip:0x%p name:%s", dip, dentry->d_name.name); 219 jfs_info("jfs_mkdir: dip:0x%p name:%s", dip, dentry->d_name.name);
220 220
221 dquot_initialize(dip); 221 dquot_initialize(dip);
222 222
223 /* link count overflow on parent directory ? */ 223 /* link count overflow on parent directory ? */
224 if (dip->i_nlink == JFS_LINK_MAX) { 224 if (dip->i_nlink == JFS_LINK_MAX) {
225 rc = -EMLINK; 225 rc = -EMLINK;
226 goto out1; 226 goto out1;
227 } 227 }
228 228
229 /* 229 /*
230 * search parent directory for entry/freespace 230 * search parent directory for entry/freespace
231 * (dtSearch() returns parent directory page pinned) 231 * (dtSearch() returns parent directory page pinned)
232 */ 232 */
233 if ((rc = get_UCSname(&dname, dentry))) 233 if ((rc = get_UCSname(&dname, dentry)))
234 goto out1; 234 goto out1;
235 235
236 /* 236 /*
237 * Either iAlloc() or txBegin() may block. Deadlock can occur if we 237 * Either iAlloc() or txBegin() may block. Deadlock can occur if we
238 * block there while holding dtree page, so we allocate the inode & 238 * block there while holding dtree page, so we allocate the inode &
239 * begin the transaction before we search the directory. 239 * begin the transaction before we search the directory.
240 */ 240 */
241 ip = ialloc(dip, S_IFDIR | mode); 241 ip = ialloc(dip, S_IFDIR | mode);
242 if (IS_ERR(ip)) { 242 if (IS_ERR(ip)) {
243 rc = PTR_ERR(ip); 243 rc = PTR_ERR(ip);
244 goto out2; 244 goto out2;
245 } 245 }
246 246
247 tid = txBegin(dip->i_sb, 0); 247 tid = txBegin(dip->i_sb, 0);
248 248
249 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT); 249 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
250 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD); 250 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
251 251
252 rc = jfs_init_acl(tid, ip, dip); 252 rc = jfs_init_acl(tid, ip, dip);
253 if (rc) 253 if (rc)
254 goto out3; 254 goto out3;
255 255
256 rc = jfs_init_security(tid, ip, dip, &dentry->d_name); 256 rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
257 if (rc) { 257 if (rc) {
258 txAbort(tid, 0); 258 txAbort(tid, 0);
259 goto out3; 259 goto out3;
260 } 260 }
261 261
262 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) { 262 if ((rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE))) {
263 jfs_err("jfs_mkdir: dtSearch returned %d", rc); 263 jfs_err("jfs_mkdir: dtSearch returned %d", rc);
264 txAbort(tid, 0); 264 txAbort(tid, 0);
265 goto out3; 265 goto out3;
266 } 266 }
267 267
268 tblk = tid_to_tblock(tid); 268 tblk = tid_to_tblock(tid);
269 tblk->xflag |= COMMIT_CREATE; 269 tblk->xflag |= COMMIT_CREATE;
270 tblk->ino = ip->i_ino; 270 tblk->ino = ip->i_ino;
271 tblk->u.ixpxd = JFS_IP(ip)->ixpxd; 271 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
272 272
273 iplist[0] = dip; 273 iplist[0] = dip;
274 iplist[1] = ip; 274 iplist[1] = ip;
275 275
276 /* 276 /*
277 * initialize the child directory in-line in inode 277 * initialize the child directory in-line in inode
278 */ 278 */
279 dtInitRoot(tid, ip, dip->i_ino); 279 dtInitRoot(tid, ip, dip->i_ino);
280 280
281 /* 281 /*
282 * create entry in parent directory for child directory 282 * create entry in parent directory for child directory
283 * (dtInsert() releases parent directory page) 283 * (dtInsert() releases parent directory page)
284 */ 284 */
285 ino = ip->i_ino; 285 ino = ip->i_ino;
286 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) { 286 if ((rc = dtInsert(tid, dip, &dname, &ino, &btstack))) {
287 if (rc == -EIO) { 287 if (rc == -EIO) {
288 jfs_err("jfs_mkdir: dtInsert returned -EIO"); 288 jfs_err("jfs_mkdir: dtInsert returned -EIO");
289 txAbort(tid, 1); /* Marks Filesystem dirty */ 289 txAbort(tid, 1); /* Marks Filesystem dirty */
290 } else 290 } else
291 txAbort(tid, 0); /* Filesystem full */ 291 txAbort(tid, 0); /* Filesystem full */
292 goto out3; 292 goto out3;
293 } 293 }
294 294
295 ip->i_nlink = 2; /* for '.' */ 295 ip->i_nlink = 2; /* for '.' */
296 ip->i_op = &jfs_dir_inode_operations; 296 ip->i_op = &jfs_dir_inode_operations;
297 ip->i_fop = &jfs_dir_operations; 297 ip->i_fop = &jfs_dir_operations;
298 298
299 mark_inode_dirty(ip); 299 mark_inode_dirty(ip);
300 300
301 /* update parent directory inode */ 301 /* update parent directory inode */
302 inc_nlink(dip); /* for '..' from child directory */ 302 inc_nlink(dip); /* for '..' from child directory */
303 dip->i_ctime = dip->i_mtime = CURRENT_TIME; 303 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
304 mark_inode_dirty(dip); 304 mark_inode_dirty(dip);
305 305
306 rc = txCommit(tid, 2, &iplist[0], 0); 306 rc = txCommit(tid, 2, &iplist[0], 0);
307 307
308 out3: 308 out3:
309 txEnd(tid); 309 txEnd(tid);
310 mutex_unlock(&JFS_IP(ip)->commit_mutex); 310 mutex_unlock(&JFS_IP(ip)->commit_mutex);
311 mutex_unlock(&JFS_IP(dip)->commit_mutex); 311 mutex_unlock(&JFS_IP(dip)->commit_mutex);
312 if (rc) { 312 if (rc) {
313 free_ea_wmap(ip); 313 free_ea_wmap(ip);
314 ip->i_nlink = 0; 314 ip->i_nlink = 0;
315 unlock_new_inode(ip); 315 unlock_new_inode(ip);
316 iput(ip); 316 iput(ip);
317 } else { 317 } else {
318 d_instantiate(dentry, ip); 318 d_instantiate(dentry, ip);
319 unlock_new_inode(ip); 319 unlock_new_inode(ip);
320 } 320 }
321 321
322 out2: 322 out2:
323 free_UCSname(&dname); 323 free_UCSname(&dname);
324 324
325 325
326 out1: 326 out1:
327 327
328 jfs_info("jfs_mkdir: rc:%d", rc); 328 jfs_info("jfs_mkdir: rc:%d", rc);
329 return rc; 329 return rc;
330 } 330 }
331 331
332 /* 332 /*
333 * NAME: jfs_rmdir(dip, dentry) 333 * NAME: jfs_rmdir(dip, dentry)
334 * 334 *
335 * FUNCTION: remove a link to child directory 335 * FUNCTION: remove a link to child directory
336 * 336 *
337 * PARAMETER: dip - parent inode 337 * PARAMETER: dip - parent inode
338 * dentry - child directory dentry 338 * dentry - child directory dentry
339 * 339 *
340 * RETURN: -EINVAL - if name is . or .. 340 * RETURN: -EINVAL - if name is . or ..
341 * -EINVAL - if . or .. exist but are invalid. 341 * -EINVAL - if . or .. exist but are invalid.
342 * errors from subroutines 342 * errors from subroutines
343 * 343 *
344 * note: 344 * note:
345 * if other threads have the directory open when the last link 345 * if other threads have the directory open when the last link
346 * is removed, the "." and ".." entries, if present, are removed before 346 * is removed, the "." and ".." entries, if present, are removed before
347 * rmdir() returns and no new entries may be created in the directory, 347 * rmdir() returns and no new entries may be created in the directory,
348 * but the directory is not removed until the last reference to 348 * but the directory is not removed until the last reference to
349 * the directory is released (cf.unlink() of regular file). 349 * the directory is released (cf.unlink() of regular file).
350 */ 350 */
351 static int jfs_rmdir(struct inode *dip, struct dentry *dentry) 351 static int jfs_rmdir(struct inode *dip, struct dentry *dentry)
352 { 352 {
353 int rc; 353 int rc;
354 tid_t tid; /* transaction id */ 354 tid_t tid; /* transaction id */
355 struct inode *ip = dentry->d_inode; 355 struct inode *ip = dentry->d_inode;
356 ino_t ino; 356 ino_t ino;
357 struct component_name dname; 357 struct component_name dname;
358 struct inode *iplist[2]; 358 struct inode *iplist[2];
359 struct tblock *tblk; 359 struct tblock *tblk;
360 360
361 jfs_info("jfs_rmdir: dip:0x%p name:%s", dip, dentry->d_name.name); 361 jfs_info("jfs_rmdir: dip:0x%p name:%s", dip, dentry->d_name.name);
362 362
363 /* Init inode for quota operations. */ 363 /* Init inode for quota operations. */
364 dquot_initialize(dip); 364 dquot_initialize(dip);
365 dquot_initialize(ip); 365 dquot_initialize(ip);
366 366
367 /* directory must be empty to be removed */ 367 /* directory must be empty to be removed */
368 if (!dtEmpty(ip)) { 368 if (!dtEmpty(ip)) {
369 rc = -ENOTEMPTY; 369 rc = -ENOTEMPTY;
370 goto out; 370 goto out;
371 } 371 }
372 372
373 if ((rc = get_UCSname(&dname, dentry))) { 373 if ((rc = get_UCSname(&dname, dentry))) {
374 goto out; 374 goto out;
375 } 375 }
376 376
377 tid = txBegin(dip->i_sb, 0); 377 tid = txBegin(dip->i_sb, 0);
378 378
379 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT); 379 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
380 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD); 380 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
381 381
382 iplist[0] = dip; 382 iplist[0] = dip;
383 iplist[1] = ip; 383 iplist[1] = ip;
384 384
385 tblk = tid_to_tblock(tid); 385 tblk = tid_to_tblock(tid);
386 tblk->xflag |= COMMIT_DELETE; 386 tblk->xflag |= COMMIT_DELETE;
387 tblk->u.ip = ip; 387 tblk->u.ip = ip;
388 388
389 /* 389 /*
390 * delete the entry of target directory from parent directory 390 * delete the entry of target directory from parent directory
391 */ 391 */
392 ino = ip->i_ino; 392 ino = ip->i_ino;
393 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) { 393 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
394 jfs_err("jfs_rmdir: dtDelete returned %d", rc); 394 jfs_err("jfs_rmdir: dtDelete returned %d", rc);
395 if (rc == -EIO) 395 if (rc == -EIO)
396 txAbort(tid, 1); 396 txAbort(tid, 1);
397 txEnd(tid); 397 txEnd(tid);
398 mutex_unlock(&JFS_IP(ip)->commit_mutex); 398 mutex_unlock(&JFS_IP(ip)->commit_mutex);
399 mutex_unlock(&JFS_IP(dip)->commit_mutex); 399 mutex_unlock(&JFS_IP(dip)->commit_mutex);
400 400
401 goto out2; 401 goto out2;
402 } 402 }
403 403
404 /* update parent directory's link count corresponding 404 /* update parent directory's link count corresponding
405 * to ".." entry of the target directory deleted 405 * to ".." entry of the target directory deleted
406 */ 406 */
407 dip->i_ctime = dip->i_mtime = CURRENT_TIME; 407 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
408 inode_dec_link_count(dip); 408 inode_dec_link_count(dip);
409 409
410 /* 410 /*
411 * OS/2 could have created EA and/or ACL 411 * OS/2 could have created EA and/or ACL
412 */ 412 */
413 /* free EA from both persistent and working map */ 413 /* free EA from both persistent and working map */
414 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) { 414 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
415 /* free EA pages */ 415 /* free EA pages */
416 txEA(tid, ip, &JFS_IP(ip)->ea, NULL); 416 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
417 } 417 }
418 JFS_IP(ip)->ea.flag = 0; 418 JFS_IP(ip)->ea.flag = 0;
419 419
420 /* free ACL from both persistent and working map */ 420 /* free ACL from both persistent and working map */
421 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) { 421 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
422 /* free ACL pages */ 422 /* free ACL pages */
423 txEA(tid, ip, &JFS_IP(ip)->acl, NULL); 423 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
424 } 424 }
425 JFS_IP(ip)->acl.flag = 0; 425 JFS_IP(ip)->acl.flag = 0;
426 426
427 /* mark the target directory as deleted */ 427 /* mark the target directory as deleted */
428 clear_nlink(ip); 428 clear_nlink(ip);
429 mark_inode_dirty(ip); 429 mark_inode_dirty(ip);
430 430
431 rc = txCommit(tid, 2, &iplist[0], 0); 431 rc = txCommit(tid, 2, &iplist[0], 0);
432 432
433 txEnd(tid); 433 txEnd(tid);
434 434
435 mutex_unlock(&JFS_IP(ip)->commit_mutex); 435 mutex_unlock(&JFS_IP(ip)->commit_mutex);
436 mutex_unlock(&JFS_IP(dip)->commit_mutex); 436 mutex_unlock(&JFS_IP(dip)->commit_mutex);
437 437
438 /* 438 /*
439 * Truncating the directory index table is not guaranteed. It 439 * Truncating the directory index table is not guaranteed. It
440 * may need to be done iteratively 440 * may need to be done iteratively
441 */ 441 */
442 if (test_cflag(COMMIT_Stale, dip)) { 442 if (test_cflag(COMMIT_Stale, dip)) {
443 if (dip->i_size > 1) 443 if (dip->i_size > 1)
444 jfs_truncate_nolock(dip, 0); 444 jfs_truncate_nolock(dip, 0);
445 445
446 clear_cflag(COMMIT_Stale, dip); 446 clear_cflag(COMMIT_Stale, dip);
447 } 447 }
448 448
449 out2: 449 out2:
450 free_UCSname(&dname); 450 free_UCSname(&dname);
451 451
452 out: 452 out:
453 jfs_info("jfs_rmdir: rc:%d", rc); 453 jfs_info("jfs_rmdir: rc:%d", rc);
454 return rc; 454 return rc;
455 } 455 }
456 456
457 /* 457 /*
458 * NAME: jfs_unlink(dip, dentry) 458 * NAME: jfs_unlink(dip, dentry)
459 * 459 *
460 * FUNCTION: remove a link to object <vp> named by <name> 460 * FUNCTION: remove a link to object <vp> named by <name>
461 * from parent directory <dvp> 461 * from parent directory <dvp>
462 * 462 *
463 * PARAMETER: dip - inode of parent directory 463 * PARAMETER: dip - inode of parent directory
464 * dentry - dentry of object to be removed 464 * dentry - dentry of object to be removed
465 * 465 *
466 * RETURN: errors from subroutines 466 * RETURN: errors from subroutines
467 * 467 *
468 * note: 468 * note:
469 * temporary file: if one or more processes have the file open 469 * temporary file: if one or more processes have the file open
470 * when the last link is removed, the link will be removed before 470 * when the last link is removed, the link will be removed before
471 * unlink() returns, but the removal of the file contents will be 471 * unlink() returns, but the removal of the file contents will be
472 * postponed until all references to the files are closed. 472 * postponed until all references to the files are closed.
473 * 473 *
474 * JFS does NOT support unlink() on directories. 474 * JFS does NOT support unlink() on directories.
475 * 475 *
476 */ 476 */
477 static int jfs_unlink(struct inode *dip, struct dentry *dentry) 477 static int jfs_unlink(struct inode *dip, struct dentry *dentry)
478 { 478 {
479 int rc; 479 int rc;
480 tid_t tid; /* transaction id */ 480 tid_t tid; /* transaction id */
481 struct inode *ip = dentry->d_inode; 481 struct inode *ip = dentry->d_inode;
482 ino_t ino; 482 ino_t ino;
483 struct component_name dname; /* object name */ 483 struct component_name dname; /* object name */
484 struct inode *iplist[2]; 484 struct inode *iplist[2];
485 struct tblock *tblk; 485 struct tblock *tblk;
486 s64 new_size = 0; 486 s64 new_size = 0;
487 int commit_flag; 487 int commit_flag;
488 488
489 jfs_info("jfs_unlink: dip:0x%p name:%s", dip, dentry->d_name.name); 489 jfs_info("jfs_unlink: dip:0x%p name:%s", dip, dentry->d_name.name);
490 490
491 /* Init inode for quota operations. */ 491 /* Init inode for quota operations. */
492 dquot_initialize(dip); 492 dquot_initialize(dip);
493 dquot_initialize(ip); 493 dquot_initialize(ip);
494 494
495 if ((rc = get_UCSname(&dname, dentry))) 495 if ((rc = get_UCSname(&dname, dentry)))
496 goto out; 496 goto out;
497 497
498 IWRITE_LOCK(ip, RDWRLOCK_NORMAL); 498 IWRITE_LOCK(ip, RDWRLOCK_NORMAL);
499 499
500 tid = txBegin(dip->i_sb, 0); 500 tid = txBegin(dip->i_sb, 0);
501 501
502 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT); 502 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
503 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD); 503 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
504 504
505 iplist[0] = dip; 505 iplist[0] = dip;
506 iplist[1] = ip; 506 iplist[1] = ip;
507 507
508 /* 508 /*
509 * delete the entry of target file from parent directory 509 * delete the entry of target file from parent directory
510 */ 510 */
511 ino = ip->i_ino; 511 ino = ip->i_ino;
512 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) { 512 if ((rc = dtDelete(tid, dip, &dname, &ino, JFS_REMOVE))) {
513 jfs_err("jfs_unlink: dtDelete returned %d", rc); 513 jfs_err("jfs_unlink: dtDelete returned %d", rc);
514 if (rc == -EIO) 514 if (rc == -EIO)
515 txAbort(tid, 1); /* Marks FS Dirty */ 515 txAbort(tid, 1); /* Marks FS Dirty */
516 txEnd(tid); 516 txEnd(tid);
517 mutex_unlock(&JFS_IP(ip)->commit_mutex); 517 mutex_unlock(&JFS_IP(ip)->commit_mutex);
518 mutex_unlock(&JFS_IP(dip)->commit_mutex); 518 mutex_unlock(&JFS_IP(dip)->commit_mutex);
519 IWRITE_UNLOCK(ip); 519 IWRITE_UNLOCK(ip);
520 goto out1; 520 goto out1;
521 } 521 }
522 522
523 ASSERT(ip->i_nlink); 523 ASSERT(ip->i_nlink);
524 524
525 ip->i_ctime = dip->i_ctime = dip->i_mtime = CURRENT_TIME; 525 ip->i_ctime = dip->i_ctime = dip->i_mtime = CURRENT_TIME;
526 mark_inode_dirty(dip); 526 mark_inode_dirty(dip);
527 527
528 /* update target's inode */ 528 /* update target's inode */
529 inode_dec_link_count(ip); 529 inode_dec_link_count(ip);
530 530
531 /* 531 /*
532 * commit zero link count object 532 * commit zero link count object
533 */ 533 */
534 if (ip->i_nlink == 0) { 534 if (ip->i_nlink == 0) {
535 assert(!test_cflag(COMMIT_Nolink, ip)); 535 assert(!test_cflag(COMMIT_Nolink, ip));
536 /* free block resources */ 536 /* free block resources */
537 if ((new_size = commitZeroLink(tid, ip)) < 0) { 537 if ((new_size = commitZeroLink(tid, ip)) < 0) {
538 txAbort(tid, 1); /* Marks FS Dirty */ 538 txAbort(tid, 1); /* Marks FS Dirty */
539 txEnd(tid); 539 txEnd(tid);
540 mutex_unlock(&JFS_IP(ip)->commit_mutex); 540 mutex_unlock(&JFS_IP(ip)->commit_mutex);
541 mutex_unlock(&JFS_IP(dip)->commit_mutex); 541 mutex_unlock(&JFS_IP(dip)->commit_mutex);
542 IWRITE_UNLOCK(ip); 542 IWRITE_UNLOCK(ip);
543 rc = new_size; 543 rc = new_size;
544 goto out1; 544 goto out1;
545 } 545 }
546 tblk = tid_to_tblock(tid); 546 tblk = tid_to_tblock(tid);
547 tblk->xflag |= COMMIT_DELETE; 547 tblk->xflag |= COMMIT_DELETE;
548 tblk->u.ip = ip; 548 tblk->u.ip = ip;
549 } 549 }
550 550
551 /* 551 /*
552 * Incomplete truncate of file data can 552 * Incomplete truncate of file data can
553 * result in timing problems unless we synchronously commit the 553 * result in timing problems unless we synchronously commit the
554 * transaction. 554 * transaction.
555 */ 555 */
556 if (new_size) 556 if (new_size)
557 commit_flag = COMMIT_SYNC; 557 commit_flag = COMMIT_SYNC;
558 else 558 else
559 commit_flag = 0; 559 commit_flag = 0;
560 560
561 /* 561 /*
562 * If xtTruncate was incomplete, commit synchronously to avoid 562 * If xtTruncate was incomplete, commit synchronously to avoid
563 * timing complications 563 * timing complications
564 */ 564 */
565 rc = txCommit(tid, 2, &iplist[0], commit_flag); 565 rc = txCommit(tid, 2, &iplist[0], commit_flag);
566 566
567 txEnd(tid); 567 txEnd(tid);
568 568
569 mutex_unlock(&JFS_IP(ip)->commit_mutex); 569 mutex_unlock(&JFS_IP(ip)->commit_mutex);
570 mutex_unlock(&JFS_IP(dip)->commit_mutex); 570 mutex_unlock(&JFS_IP(dip)->commit_mutex);
571 571
572 while (new_size && (rc == 0)) { 572 while (new_size && (rc == 0)) {
573 tid = txBegin(dip->i_sb, 0); 573 tid = txBegin(dip->i_sb, 0);
574 mutex_lock(&JFS_IP(ip)->commit_mutex); 574 mutex_lock(&JFS_IP(ip)->commit_mutex);
575 new_size = xtTruncate_pmap(tid, ip, new_size); 575 new_size = xtTruncate_pmap(tid, ip, new_size);
576 if (new_size < 0) { 576 if (new_size < 0) {
577 txAbort(tid, 1); /* Marks FS Dirty */ 577 txAbort(tid, 1); /* Marks FS Dirty */
578 rc = new_size; 578 rc = new_size;
579 } else 579 } else
580 rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC); 580 rc = txCommit(tid, 2, &iplist[0], COMMIT_SYNC);
581 txEnd(tid); 581 txEnd(tid);
582 mutex_unlock(&JFS_IP(ip)->commit_mutex); 582 mutex_unlock(&JFS_IP(ip)->commit_mutex);
583 } 583 }
584 584
585 if (ip->i_nlink == 0) 585 if (ip->i_nlink == 0)
586 set_cflag(COMMIT_Nolink, ip); 586 set_cflag(COMMIT_Nolink, ip);
587 587
588 IWRITE_UNLOCK(ip); 588 IWRITE_UNLOCK(ip);
589 589
590 /* 590 /*
591 * Truncating the directory index table is not guaranteed. It 591 * Truncating the directory index table is not guaranteed. It
592 * may need to be done iteratively 592 * may need to be done iteratively
593 */ 593 */
594 if (test_cflag(COMMIT_Stale, dip)) { 594 if (test_cflag(COMMIT_Stale, dip)) {
595 if (dip->i_size > 1) 595 if (dip->i_size > 1)
596 jfs_truncate_nolock(dip, 0); 596 jfs_truncate_nolock(dip, 0);
597 597
598 clear_cflag(COMMIT_Stale, dip); 598 clear_cflag(COMMIT_Stale, dip);
599 } 599 }
600 600
601 out1: 601 out1:
602 free_UCSname(&dname); 602 free_UCSname(&dname);
603 out: 603 out:
604 jfs_info("jfs_unlink: rc:%d", rc); 604 jfs_info("jfs_unlink: rc:%d", rc);
605 return rc; 605 return rc;
606 } 606 }
607 607
608 /* 608 /*
609 * NAME: commitZeroLink() 609 * NAME: commitZeroLink()
610 * 610 *
611 * FUNCTION: for non-directory, called by jfs_remove(), 611 * FUNCTION: for non-directory, called by jfs_remove(),
612 * truncate a regular file, directory or symbolic 612 * truncate a regular file, directory or symbolic
613 * link to zero length. return 0 if type is not 613 * link to zero length. return 0 if type is not
614 * one of these. 614 * one of these.
615 * 615 *
616 * if the file is currently associated with a VM segment 616 * if the file is currently associated with a VM segment
617 * only permanent disk and inode map resources are freed, 617 * only permanent disk and inode map resources are freed,
618 * and neither the inode nor indirect blocks are modified 618 * and neither the inode nor indirect blocks are modified
619 * so that the resources can be later freed in the work 619 * so that the resources can be later freed in the work
620 * map by ctrunc1. 620 * map by ctrunc1.
621 * if there is no VM segment on entry, the resources are 621 * if there is no VM segment on entry, the resources are
622 * freed in both work and permanent map. 622 * freed in both work and permanent map.
623 * (? for temporary file - memory object is cached even 623 * (? for temporary file - memory object is cached even
624 * after no reference: 624 * after no reference:
625 * reference count > 0 - ) 625 * reference count > 0 - )
626 * 626 *
627 * PARAMETERS: cd - pointer to commit data structure. 627 * PARAMETERS: cd - pointer to commit data structure.
628 * current inode is the one to truncate. 628 * current inode is the one to truncate.
629 * 629 *
630 * RETURN: Errors from subroutines 630 * RETURN: Errors from subroutines
631 */ 631 */
632 static s64 commitZeroLink(tid_t tid, struct inode *ip) 632 static s64 commitZeroLink(tid_t tid, struct inode *ip)
633 { 633 {
634 int filetype; 634 int filetype;
635 struct tblock *tblk; 635 struct tblock *tblk;
636 636
637 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip); 637 jfs_info("commitZeroLink: tid = %d, ip = 0x%p", tid, ip);
638 638
639 filetype = ip->i_mode & S_IFMT; 639 filetype = ip->i_mode & S_IFMT;
640 switch (filetype) { 640 switch (filetype) {
641 case S_IFREG: 641 case S_IFREG:
642 break; 642 break;
643 case S_IFLNK: 643 case S_IFLNK:
644 /* fast symbolic link */ 644 /* fast symbolic link */
645 if (ip->i_size < IDATASIZE) { 645 if (ip->i_size < IDATASIZE) {
646 ip->i_size = 0; 646 ip->i_size = 0;
647 return 0; 647 return 0;
648 } 648 }
649 break; 649 break;
650 default: 650 default:
651 assert(filetype != S_IFDIR); 651 assert(filetype != S_IFDIR);
652 return 0; 652 return 0;
653 } 653 }
654 654
655 set_cflag(COMMIT_Freewmap, ip); 655 set_cflag(COMMIT_Freewmap, ip);
656 656
657 /* mark transaction of block map update type */ 657 /* mark transaction of block map update type */
658 tblk = tid_to_tblock(tid); 658 tblk = tid_to_tblock(tid);
659 tblk->xflag |= COMMIT_PMAP; 659 tblk->xflag |= COMMIT_PMAP;
660 660
661 /* 661 /*
662 * free EA 662 * free EA
663 */ 663 */
664 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) 664 if (JFS_IP(ip)->ea.flag & DXD_EXTENT)
665 /* acquire maplock on EA to be freed from block map */ 665 /* acquire maplock on EA to be freed from block map */
666 txEA(tid, ip, &JFS_IP(ip)->ea, NULL); 666 txEA(tid, ip, &JFS_IP(ip)->ea, NULL);
667 667
668 /* 668 /*
669 * free ACL 669 * free ACL
670 */ 670 */
671 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) 671 if (JFS_IP(ip)->acl.flag & DXD_EXTENT)
672 /* acquire maplock on EA to be freed from block map */ 672 /* acquire maplock on EA to be freed from block map */
673 txEA(tid, ip, &JFS_IP(ip)->acl, NULL); 673 txEA(tid, ip, &JFS_IP(ip)->acl, NULL);
674 674
675 /* 675 /*
676 * free xtree/data (truncate to zero length): 676 * free xtree/data (truncate to zero length):
677 * free xtree/data pages from cache if COMMIT_PWMAP, 677 * free xtree/data pages from cache if COMMIT_PWMAP,
678 * free xtree/data blocks from persistent block map, and 678 * free xtree/data blocks from persistent block map, and
679 * free xtree/data blocks from working block map if COMMIT_PWMAP; 679 * free xtree/data blocks from working block map if COMMIT_PWMAP;
680 */ 680 */
681 if (ip->i_size) 681 if (ip->i_size)
682 return xtTruncate_pmap(tid, ip, 0); 682 return xtTruncate_pmap(tid, ip, 0);
683 683
684 return 0; 684 return 0;
685 } 685 }
686 686
687 687
688 /* 688 /*
689 * NAME: jfs_free_zero_link() 689 * NAME: jfs_free_zero_link()
690 * 690 *
691 * FUNCTION: for non-directory, called by iClose(), 691 * FUNCTION: for non-directory, called by iClose(),
692 * free resources of a file from cache and WORKING map 692 * free resources of a file from cache and WORKING map
693 * for a file previously committed with zero link count 693 * for a file previously committed with zero link count
694 * while associated with a pager object, 694 * while associated with a pager object,
695 * 695 *
696 * PARAMETER: ip - pointer to inode of file. 696 * PARAMETER: ip - pointer to inode of file.
697 */ 697 */
698 void jfs_free_zero_link(struct inode *ip) 698 void jfs_free_zero_link(struct inode *ip)
699 { 699 {
700 int type; 700 int type;
701 701
702 jfs_info("jfs_free_zero_link: ip = 0x%p", ip); 702 jfs_info("jfs_free_zero_link: ip = 0x%p", ip);
703 703
704 /* return if not reg or symbolic link or if size is 704 /* return if not reg or symbolic link or if size is
705 * already ok. 705 * already ok.
706 */ 706 */
707 type = ip->i_mode & S_IFMT; 707 type = ip->i_mode & S_IFMT;
708 708
709 switch (type) { 709 switch (type) {
710 case S_IFREG: 710 case S_IFREG:
711 break; 711 break;
712 case S_IFLNK: 712 case S_IFLNK:
713 /* if its contained in inode nothing to do */ 713 /* if its contained in inode nothing to do */
714 if (ip->i_size < IDATASIZE) 714 if (ip->i_size < IDATASIZE)
715 return; 715 return;
716 break; 716 break;
717 default: 717 default:
718 return; 718 return;
719 } 719 }
720 720
721 /* 721 /*
722 * free EA 722 * free EA
723 */ 723 */
724 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) { 724 if (JFS_IP(ip)->ea.flag & DXD_EXTENT) {
725 s64 xaddr = addressDXD(&JFS_IP(ip)->ea); 725 s64 xaddr = addressDXD(&JFS_IP(ip)->ea);
726 int xlen = lengthDXD(&JFS_IP(ip)->ea); 726 int xlen = lengthDXD(&JFS_IP(ip)->ea);
727 struct maplock maplock; /* maplock for COMMIT_WMAP */ 727 struct maplock maplock; /* maplock for COMMIT_WMAP */
728 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */ 728 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
729 729
730 /* free EA pages from cache */ 730 /* free EA pages from cache */
731 invalidate_dxd_metapages(ip, JFS_IP(ip)->ea); 731 invalidate_dxd_metapages(ip, JFS_IP(ip)->ea);
732 732
733 /* free EA extent from working block map */ 733 /* free EA extent from working block map */
734 maplock.index = 1; 734 maplock.index = 1;
735 pxdlock = (struct pxd_lock *) & maplock; 735 pxdlock = (struct pxd_lock *) & maplock;
736 pxdlock->flag = mlckFREEPXD; 736 pxdlock->flag = mlckFREEPXD;
737 PXDaddress(&pxdlock->pxd, xaddr); 737 PXDaddress(&pxdlock->pxd, xaddr);
738 PXDlength(&pxdlock->pxd, xlen); 738 PXDlength(&pxdlock->pxd, xlen);
739 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP); 739 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
740 } 740 }
741 741
742 /* 742 /*
743 * free ACL 743 * free ACL
744 */ 744 */
745 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) { 745 if (JFS_IP(ip)->acl.flag & DXD_EXTENT) {
746 s64 xaddr = addressDXD(&JFS_IP(ip)->acl); 746 s64 xaddr = addressDXD(&JFS_IP(ip)->acl);
747 int xlen = lengthDXD(&JFS_IP(ip)->acl); 747 int xlen = lengthDXD(&JFS_IP(ip)->acl);
748 struct maplock maplock; /* maplock for COMMIT_WMAP */ 748 struct maplock maplock; /* maplock for COMMIT_WMAP */
749 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */ 749 struct pxd_lock *pxdlock; /* maplock for COMMIT_WMAP */
750 750
751 invalidate_dxd_metapages(ip, JFS_IP(ip)->acl); 751 invalidate_dxd_metapages(ip, JFS_IP(ip)->acl);
752 752
753 /* free ACL extent from working block map */ 753 /* free ACL extent from working block map */
754 maplock.index = 1; 754 maplock.index = 1;
755 pxdlock = (struct pxd_lock *) & maplock; 755 pxdlock = (struct pxd_lock *) & maplock;
756 pxdlock->flag = mlckFREEPXD; 756 pxdlock->flag = mlckFREEPXD;
757 PXDaddress(&pxdlock->pxd, xaddr); 757 PXDaddress(&pxdlock->pxd, xaddr);
758 PXDlength(&pxdlock->pxd, xlen); 758 PXDlength(&pxdlock->pxd, xlen);
759 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP); 759 txFreeMap(ip, pxdlock, NULL, COMMIT_WMAP);
760 } 760 }
761 761
762 /* 762 /*
763 * free xtree/data (truncate to zero length): 763 * free xtree/data (truncate to zero length):
764 * free xtree/data pages from cache, and 764 * free xtree/data pages from cache, and
765 * free xtree/data blocks from working block map; 765 * free xtree/data blocks from working block map;
766 */ 766 */
767 if (ip->i_size) 767 if (ip->i_size)
768 xtTruncate(0, ip, 0, COMMIT_WMAP); 768 xtTruncate(0, ip, 0, COMMIT_WMAP);
769 } 769 }
770 770
771 /* 771 /*
772 * NAME: jfs_link(vp, dvp, name, crp) 772 * NAME: jfs_link(vp, dvp, name, crp)
773 * 773 *
774 * FUNCTION: create a link to <vp> by the name = <name> 774 * FUNCTION: create a link to <vp> by the name = <name>
775 * in the parent directory <dvp> 775 * in the parent directory <dvp>
776 * 776 *
777 * PARAMETER: vp - target object 777 * PARAMETER: vp - target object
778 * dvp - parent directory of new link 778 * dvp - parent directory of new link
779 * name - name of new link to target object 779 * name - name of new link to target object
780 * crp - credential 780 * crp - credential
781 * 781 *
782 * RETURN: Errors from subroutines 782 * RETURN: Errors from subroutines
783 * 783 *
784 * note: 784 * note:
785 * JFS does NOT support link() on directories (to prevent circular 785 * JFS does NOT support link() on directories (to prevent circular
786 * path in the directory hierarchy); 786 * path in the directory hierarchy);
787 * EPERM: the target object is a directory, and either the caller 787 * EPERM: the target object is a directory, and either the caller
788 * does not have appropriate privileges or the implementation prohibits 788 * does not have appropriate privileges or the implementation prohibits
789 * using link() on directories [XPG4.2]. 789 * using link() on directories [XPG4.2].
790 * 790 *
791 * JFS does NOT support links between file systems: 791 * JFS does NOT support links between file systems:
792 * EXDEV: target object and new link are on different file systems and 792 * EXDEV: target object and new link are on different file systems and
793 * implementation does not support links between file systems [XPG4.2]. 793 * implementation does not support links between file systems [XPG4.2].
794 */ 794 */
795 static int jfs_link(struct dentry *old_dentry, 795 static int jfs_link(struct dentry *old_dentry,
796 struct inode *dir, struct dentry *dentry) 796 struct inode *dir, struct dentry *dentry)
797 { 797 {
798 int rc; 798 int rc;
799 tid_t tid; 799 tid_t tid;
800 struct inode *ip = old_dentry->d_inode; 800 struct inode *ip = old_dentry->d_inode;
801 ino_t ino; 801 ino_t ino;
802 struct component_name dname; 802 struct component_name dname;
803 struct btstack btstack; 803 struct btstack btstack;
804 struct inode *iplist[2]; 804 struct inode *iplist[2];
805 805
806 jfs_info("jfs_link: %s %s", old_dentry->d_name.name, 806 jfs_info("jfs_link: %s %s", old_dentry->d_name.name,
807 dentry->d_name.name); 807 dentry->d_name.name);
808 808
809 if (ip->i_nlink == JFS_LINK_MAX) 809 if (ip->i_nlink == JFS_LINK_MAX)
810 return -EMLINK; 810 return -EMLINK;
811 811
812 dquot_initialize(dir); 812 dquot_initialize(dir);
813 813
814 tid = txBegin(ip->i_sb, 0); 814 tid = txBegin(ip->i_sb, 0);
815 815
816 mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT); 816 mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
817 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD); 817 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
818 818
819 /* 819 /*
820 * scan parent directory for entry/freespace 820 * scan parent directory for entry/freespace
821 */ 821 */
822 if ((rc = get_UCSname(&dname, dentry))) 822 if ((rc = get_UCSname(&dname, dentry)))
823 goto out; 823 goto out;
824 824
825 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) 825 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE)))
826 goto free_dname; 826 goto free_dname;
827 827
828 /* 828 /*
829 * create entry for new link in parent directory 829 * create entry for new link in parent directory
830 */ 830 */
831 ino = ip->i_ino; 831 ino = ip->i_ino;
832 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) 832 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack)))
833 goto free_dname; 833 goto free_dname;
834 834
835 /* update object inode */ 835 /* update object inode */
836 inc_nlink(ip); /* for new link */ 836 inc_nlink(ip); /* for new link */
837 ip->i_ctime = CURRENT_TIME; 837 ip->i_ctime = CURRENT_TIME;
838 dir->i_ctime = dir->i_mtime = CURRENT_TIME; 838 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
839 mark_inode_dirty(dir); 839 mark_inode_dirty(dir);
840 ihold(ip); 840 ihold(ip);
841 841
842 iplist[0] = ip; 842 iplist[0] = ip;
843 iplist[1] = dir; 843 iplist[1] = dir;
844 rc = txCommit(tid, 2, &iplist[0], 0); 844 rc = txCommit(tid, 2, &iplist[0], 0);
845 845
846 if (rc) { 846 if (rc) {
847 ip->i_nlink--; /* never instantiated */ 847 ip->i_nlink--; /* never instantiated */
848 iput(ip); 848 iput(ip);
849 } else 849 } else
850 d_instantiate(dentry, ip); 850 d_instantiate(dentry, ip);
851 851
852 free_dname: 852 free_dname:
853 free_UCSname(&dname); 853 free_UCSname(&dname);
854 854
855 out: 855 out:
856 txEnd(tid); 856 txEnd(tid);
857 857
858 mutex_unlock(&JFS_IP(ip)->commit_mutex); 858 mutex_unlock(&JFS_IP(ip)->commit_mutex);
859 mutex_unlock(&JFS_IP(dir)->commit_mutex); 859 mutex_unlock(&JFS_IP(dir)->commit_mutex);
860 860
861 jfs_info("jfs_link: rc:%d", rc); 861 jfs_info("jfs_link: rc:%d", rc);
862 return rc; 862 return rc;
863 } 863 }
864 864
865 /* 865 /*
866 * NAME: jfs_symlink(dip, dentry, name) 866 * NAME: jfs_symlink(dip, dentry, name)
867 * 867 *
868 * FUNCTION: creates a symbolic link to <symlink> by name <name> 868 * FUNCTION: creates a symbolic link to <symlink> by name <name>
869 * in directory <dip> 869 * in directory <dip>
870 * 870 *
871 * PARAMETER: dip - parent directory vnode 871 * PARAMETER: dip - parent directory vnode
872 * dentry - dentry of symbolic link 872 * dentry - dentry of symbolic link
873 * name - the path name of the existing object 873 * name - the path name of the existing object
874 * that will be the source of the link 874 * that will be the source of the link
875 * 875 *
876 * RETURN: errors from subroutines 876 * RETURN: errors from subroutines
877 * 877 *
878 * note: 878 * note:
879 * ENAMETOOLONG: pathname resolution of a symbolic link produced 879 * ENAMETOOLONG: pathname resolution of a symbolic link produced
880 * an intermediate result whose length exceeds PATH_MAX [XPG4.2] 880 * an intermediate result whose length exceeds PATH_MAX [XPG4.2]
881 */ 881 */
882 882
883 static int jfs_symlink(struct inode *dip, struct dentry *dentry, 883 static int jfs_symlink(struct inode *dip, struct dentry *dentry,
884 const char *name) 884 const char *name)
885 { 885 {
886 int rc; 886 int rc;
887 tid_t tid; 887 tid_t tid;
888 ino_t ino = 0; 888 ino_t ino = 0;
889 struct component_name dname; 889 struct component_name dname;
890 int ssize; /* source pathname size */ 890 int ssize; /* source pathname size */
891 struct btstack btstack; 891 struct btstack btstack;
892 struct inode *ip = dentry->d_inode; 892 struct inode *ip = dentry->d_inode;
893 unchar *i_fastsymlink; 893 unchar *i_fastsymlink;
894 s64 xlen = 0; 894 s64 xlen = 0;
895 int bmask = 0, xsize; 895 int bmask = 0, xsize;
896 s64 extent = 0, xaddr; 896 s64 extent = 0, xaddr;
897 struct metapage *mp; 897 struct metapage *mp;
898 struct super_block *sb; 898 struct super_block *sb;
899 struct tblock *tblk; 899 struct tblock *tblk;
900 900
901 struct inode *iplist[2]; 901 struct inode *iplist[2];
902 902
903 jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name); 903 jfs_info("jfs_symlink: dip:0x%p name:%s", dip, name);
904 904
905 dquot_initialize(dip); 905 dquot_initialize(dip);
906 906
907 ssize = strlen(name) + 1; 907 ssize = strlen(name) + 1;
908 908
909 /* 909 /*
910 * search parent directory for entry/freespace 910 * search parent directory for entry/freespace
911 * (dtSearch() returns parent directory page pinned) 911 * (dtSearch() returns parent directory page pinned)
912 */ 912 */
913 913
914 if ((rc = get_UCSname(&dname, dentry))) 914 if ((rc = get_UCSname(&dname, dentry)))
915 goto out1; 915 goto out1;
916 916
917 /* 917 /*
918 * allocate on-disk/in-memory inode for symbolic link: 918 * allocate on-disk/in-memory inode for symbolic link:
919 * (iAlloc() returns new, locked inode) 919 * (iAlloc() returns new, locked inode)
920 */ 920 */
921 ip = ialloc(dip, S_IFLNK | 0777); 921 ip = ialloc(dip, S_IFLNK | 0777);
922 if (IS_ERR(ip)) { 922 if (IS_ERR(ip)) {
923 rc = PTR_ERR(ip); 923 rc = PTR_ERR(ip);
924 goto out2; 924 goto out2;
925 } 925 }
926 926
927 tid = txBegin(dip->i_sb, 0); 927 tid = txBegin(dip->i_sb, 0);
928 928
929 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT); 929 mutex_lock_nested(&JFS_IP(dip)->commit_mutex, COMMIT_MUTEX_PARENT);
930 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD); 930 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
931 931
932 rc = jfs_init_security(tid, ip, dip, &dentry->d_name); 932 rc = jfs_init_security(tid, ip, dip, &dentry->d_name);
933 if (rc) 933 if (rc)
934 goto out3; 934 goto out3;
935 935
936 tblk = tid_to_tblock(tid); 936 tblk = tid_to_tblock(tid);
937 tblk->xflag |= COMMIT_CREATE; 937 tblk->xflag |= COMMIT_CREATE;
938 tblk->ino = ip->i_ino; 938 tblk->ino = ip->i_ino;
939 tblk->u.ixpxd = JFS_IP(ip)->ixpxd; 939 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
940 940
941 /* fix symlink access permission 941 /* fix symlink access permission
942 * (dir_create() ANDs in the u.u_cmask, 942 * (dir_create() ANDs in the u.u_cmask,
943 * but symlinks really need to be 777 access) 943 * but symlinks really need to be 777 access)
944 */ 944 */
945 ip->i_mode |= 0777; 945 ip->i_mode |= 0777;
946 946
947 /* 947 /*
948 * write symbolic link target path name 948 * write symbolic link target path name
949 */ 949 */
950 xtInitRoot(tid, ip); 950 xtInitRoot(tid, ip);
951 951
952 /* 952 /*
953 * write source path name inline in on-disk inode (fast symbolic link) 953 * write source path name inline in on-disk inode (fast symbolic link)
954 */ 954 */
955 955
956 if (ssize <= IDATASIZE) { 956 if (ssize <= IDATASIZE) {
957 ip->i_op = &jfs_fast_symlink_inode_operations; 957 ip->i_op = &jfs_fast_symlink_inode_operations;
958 958
959 i_fastsymlink = JFS_IP(ip)->i_inline; 959 i_fastsymlink = JFS_IP(ip)->i_inline;
960 memcpy(i_fastsymlink, name, ssize); 960 memcpy(i_fastsymlink, name, ssize);
961 ip->i_size = ssize - 1; 961 ip->i_size = ssize - 1;
962 962
963 /* 963 /*
964 * if symlink is > 128 bytes, we don't have the space to 964 * if symlink is > 128 bytes, we don't have the space to
965 * store inline extended attributes 965 * store inline extended attributes
966 */ 966 */
967 if (ssize > sizeof (JFS_IP(ip)->i_inline)) 967 if (ssize > sizeof (JFS_IP(ip)->i_inline))
968 JFS_IP(ip)->mode2 &= ~INLINEEA; 968 JFS_IP(ip)->mode2 &= ~INLINEEA;
969 969
970 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ", 970 jfs_info("jfs_symlink: fast symlink added ssize:%d name:%s ",
971 ssize, name); 971 ssize, name);
972 } 972 }
973 /* 973 /*
974 * write source path name in a single extent 974 * write source path name in a single extent
975 */ 975 */
976 else { 976 else {
977 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip); 977 jfs_info("jfs_symlink: allocate extent ip:0x%p", ip);
978 978
979 ip->i_op = &jfs_symlink_inode_operations; 979 ip->i_op = &jfs_symlink_inode_operations;
980 ip->i_mapping->a_ops = &jfs_aops; 980 ip->i_mapping->a_ops = &jfs_aops;
981 981
982 /* 982 /*
983 * even though the data of symlink object (source 983 * even though the data of symlink object (source
984 * path name) is treated as non-journaled user data, 984 * path name) is treated as non-journaled user data,
985 * it is read/written thru buffer cache for performance. 985 * it is read/written thru buffer cache for performance.
986 */ 986 */
987 sb = ip->i_sb; 987 sb = ip->i_sb;
988 bmask = JFS_SBI(sb)->bsize - 1; 988 bmask = JFS_SBI(sb)->bsize - 1;
989 xsize = (ssize + bmask) & ~bmask; 989 xsize = (ssize + bmask) & ~bmask;
990 xaddr = 0; 990 xaddr = 0;
991 xlen = xsize >> JFS_SBI(sb)->l2bsize; 991 xlen = xsize >> JFS_SBI(sb)->l2bsize;
992 if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) { 992 if ((rc = xtInsert(tid, ip, 0, 0, xlen, &xaddr, 0))) {
993 txAbort(tid, 0); 993 txAbort(tid, 0);
994 goto out3; 994 goto out3;
995 } 995 }
996 extent = xaddr; 996 extent = xaddr;
997 ip->i_size = ssize - 1; 997 ip->i_size = ssize - 1;
998 while (ssize) { 998 while (ssize) {
999 /* This is kind of silly since PATH_MAX == 4K */ 999 /* This is kind of silly since PATH_MAX == 4K */
1000 int copy_size = min(ssize, PSIZE); 1000 int copy_size = min(ssize, PSIZE);
1001 1001
1002 mp = get_metapage(ip, xaddr, PSIZE, 1); 1002 mp = get_metapage(ip, xaddr, PSIZE, 1);
1003 1003
1004 if (mp == NULL) { 1004 if (mp == NULL) {
1005 xtTruncate(tid, ip, 0, COMMIT_PWMAP); 1005 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1006 rc = -EIO; 1006 rc = -EIO;
1007 txAbort(tid, 0); 1007 txAbort(tid, 0);
1008 goto out3; 1008 goto out3;
1009 } 1009 }
1010 memcpy(mp->data, name, copy_size); 1010 memcpy(mp->data, name, copy_size);
1011 flush_metapage(mp); 1011 flush_metapage(mp);
1012 ssize -= copy_size; 1012 ssize -= copy_size;
1013 name += copy_size; 1013 name += copy_size;
1014 xaddr += JFS_SBI(sb)->nbperpage; 1014 xaddr += JFS_SBI(sb)->nbperpage;
1015 } 1015 }
1016 } 1016 }
1017 1017
1018 /* 1018 /*
1019 * create entry for symbolic link in parent directory 1019 * create entry for symbolic link in parent directory
1020 */ 1020 */
1021 rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE); 1021 rc = dtSearch(dip, &dname, &ino, &btstack, JFS_CREATE);
1022 if (rc == 0) { 1022 if (rc == 0) {
1023 ino = ip->i_ino; 1023 ino = ip->i_ino;
1024 rc = dtInsert(tid, dip, &dname, &ino, &btstack); 1024 rc = dtInsert(tid, dip, &dname, &ino, &btstack);
1025 } 1025 }
1026 if (rc) { 1026 if (rc) {
1027 if (xlen) 1027 if (xlen)
1028 xtTruncate(tid, ip, 0, COMMIT_PWMAP); 1028 xtTruncate(tid, ip, 0, COMMIT_PWMAP);
1029 txAbort(tid, 0); 1029 txAbort(tid, 0);
1030 /* discard new inode */ 1030 /* discard new inode */
1031 goto out3; 1031 goto out3;
1032 } 1032 }
1033 1033
1034 mark_inode_dirty(ip); 1034 mark_inode_dirty(ip);
1035 1035
1036 dip->i_ctime = dip->i_mtime = CURRENT_TIME; 1036 dip->i_ctime = dip->i_mtime = CURRENT_TIME;
1037 mark_inode_dirty(dip); 1037 mark_inode_dirty(dip);
1038 /* 1038 /*
1039 * commit update of parent directory and link object 1039 * commit update of parent directory and link object
1040 */ 1040 */
1041 1041
1042 iplist[0] = dip; 1042 iplist[0] = dip;
1043 iplist[1] = ip; 1043 iplist[1] = ip;
1044 rc = txCommit(tid, 2, &iplist[0], 0); 1044 rc = txCommit(tid, 2, &iplist[0], 0);
1045 1045
1046 out3: 1046 out3:
1047 txEnd(tid); 1047 txEnd(tid);
1048 mutex_unlock(&JFS_IP(ip)->commit_mutex); 1048 mutex_unlock(&JFS_IP(ip)->commit_mutex);
1049 mutex_unlock(&JFS_IP(dip)->commit_mutex); 1049 mutex_unlock(&JFS_IP(dip)->commit_mutex);
1050 if (rc) { 1050 if (rc) {
1051 free_ea_wmap(ip); 1051 free_ea_wmap(ip);
1052 ip->i_nlink = 0; 1052 ip->i_nlink = 0;
1053 unlock_new_inode(ip); 1053 unlock_new_inode(ip);
1054 iput(ip); 1054 iput(ip);
1055 } else { 1055 } else {
1056 d_instantiate(dentry, ip); 1056 d_instantiate(dentry, ip);
1057 unlock_new_inode(ip); 1057 unlock_new_inode(ip);
1058 } 1058 }
1059 1059
1060 out2: 1060 out2:
1061 free_UCSname(&dname); 1061 free_UCSname(&dname);
1062 1062
1063 out1: 1063 out1:
1064 jfs_info("jfs_symlink: rc:%d", rc); 1064 jfs_info("jfs_symlink: rc:%d", rc);
1065 return rc; 1065 return rc;
1066 } 1066 }
1067 1067
1068 1068
1069 /* 1069 /*
1070 * NAME: jfs_rename 1070 * NAME: jfs_rename
1071 * 1071 *
1072 * FUNCTION: rename a file or directory 1072 * FUNCTION: rename a file or directory
1073 */ 1073 */
1074 static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry, 1074 static int jfs_rename(struct inode *old_dir, struct dentry *old_dentry,
1075 struct inode *new_dir, struct dentry *new_dentry) 1075 struct inode *new_dir, struct dentry *new_dentry)
1076 { 1076 {
1077 struct btstack btstack; 1077 struct btstack btstack;
1078 ino_t ino; 1078 ino_t ino;
1079 struct component_name new_dname; 1079 struct component_name new_dname;
1080 struct inode *new_ip; 1080 struct inode *new_ip;
1081 struct component_name old_dname; 1081 struct component_name old_dname;
1082 struct inode *old_ip; 1082 struct inode *old_ip;
1083 int rc; 1083 int rc;
1084 tid_t tid; 1084 tid_t tid;
1085 struct tlock *tlck; 1085 struct tlock *tlck;
1086 struct dt_lock *dtlck; 1086 struct dt_lock *dtlck;
1087 struct lv *lv; 1087 struct lv *lv;
1088 int ipcount; 1088 int ipcount;
1089 struct inode *iplist[4]; 1089 struct inode *iplist[4];
1090 struct tblock *tblk; 1090 struct tblock *tblk;
1091 s64 new_size = 0; 1091 s64 new_size = 0;
1092 int commit_flag; 1092 int commit_flag;
1093 1093
1094 1094
1095 jfs_info("jfs_rename: %s %s", old_dentry->d_name.name, 1095 jfs_info("jfs_rename: %s %s", old_dentry->d_name.name,
1096 new_dentry->d_name.name); 1096 new_dentry->d_name.name);
1097 1097
1098 dquot_initialize(old_dir); 1098 dquot_initialize(old_dir);
1099 dquot_initialize(new_dir); 1099 dquot_initialize(new_dir);
1100 1100
1101 old_ip = old_dentry->d_inode; 1101 old_ip = old_dentry->d_inode;
1102 new_ip = new_dentry->d_inode; 1102 new_ip = new_dentry->d_inode;
1103 1103
1104 if ((rc = get_UCSname(&old_dname, old_dentry))) 1104 if ((rc = get_UCSname(&old_dname, old_dentry)))
1105 goto out1; 1105 goto out1;
1106 1106
1107 if ((rc = get_UCSname(&new_dname, new_dentry))) 1107 if ((rc = get_UCSname(&new_dname, new_dentry)))
1108 goto out2; 1108 goto out2;
1109 1109
1110 /* 1110 /*
1111 * Make sure source inode number is what we think it is 1111 * Make sure source inode number is what we think it is
1112 */ 1112 */
1113 rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP); 1113 rc = dtSearch(old_dir, &old_dname, &ino, &btstack, JFS_LOOKUP);
1114 if (rc || (ino != old_ip->i_ino)) { 1114 if (rc || (ino != old_ip->i_ino)) {
1115 rc = -ENOENT; 1115 rc = -ENOENT;
1116 goto out3; 1116 goto out3;
1117 } 1117 }
1118 1118
1119 /* 1119 /*
1120 * Make sure dest inode number (if any) is what we think it is 1120 * Make sure dest inode number (if any) is what we think it is
1121 */ 1121 */
1122 rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP); 1122 rc = dtSearch(new_dir, &new_dname, &ino, &btstack, JFS_LOOKUP);
1123 if (!rc) { 1123 if (!rc) {
1124 if ((!new_ip) || (ino != new_ip->i_ino)) { 1124 if ((!new_ip) || (ino != new_ip->i_ino)) {
1125 rc = -ESTALE; 1125 rc = -ESTALE;
1126 goto out3; 1126 goto out3;
1127 } 1127 }
1128 } else if (rc != -ENOENT) 1128 } else if (rc != -ENOENT)
1129 goto out3; 1129 goto out3;
1130 else if (new_ip) { 1130 else if (new_ip) {
1131 /* no entry exists, but one was expected */ 1131 /* no entry exists, but one was expected */
1132 rc = -ESTALE; 1132 rc = -ESTALE;
1133 goto out3; 1133 goto out3;
1134 } 1134 }
1135 1135
1136 if (S_ISDIR(old_ip->i_mode)) { 1136 if (S_ISDIR(old_ip->i_mode)) {
1137 if (new_ip) { 1137 if (new_ip) {
1138 if (!dtEmpty(new_ip)) { 1138 if (!dtEmpty(new_ip)) {
1139 rc = -ENOTEMPTY; 1139 rc = -ENOTEMPTY;
1140 goto out3; 1140 goto out3;
1141 } 1141 }
1142 } else if ((new_dir != old_dir) && 1142 } else if ((new_dir != old_dir) &&
1143 (new_dir->i_nlink == JFS_LINK_MAX)) { 1143 (new_dir->i_nlink == JFS_LINK_MAX)) {
1144 rc = -EMLINK; 1144 rc = -EMLINK;
1145 goto out3; 1145 goto out3;
1146 } 1146 }
1147 } else if (new_ip) { 1147 } else if (new_ip) {
1148 IWRITE_LOCK(new_ip, RDWRLOCK_NORMAL); 1148 IWRITE_LOCK(new_ip, RDWRLOCK_NORMAL);
1149 /* Init inode for quota operations. */ 1149 /* Init inode for quota operations. */
1150 dquot_initialize(new_ip); 1150 dquot_initialize(new_ip);
1151 } 1151 }
1152 1152
1153 /* 1153 /*
1154 * The real work starts here 1154 * The real work starts here
1155 */ 1155 */
1156 tid = txBegin(new_dir->i_sb, 0); 1156 tid = txBegin(new_dir->i_sb, 0);
1157 1157
1158 /* 1158 /*
1159 * How do we know the locking is safe from deadlocks? 1159 * How do we know the locking is safe from deadlocks?
1160 * The vfs does the hard part for us. Any time we are taking nested 1160 * The vfs does the hard part for us. Any time we are taking nested
1161 * commit_mutexes, the vfs already has i_mutex held on the parent. 1161 * commit_mutexes, the vfs already has i_mutex held on the parent.
1162 * Here, the vfs has already taken i_mutex on both old_dir and new_dir. 1162 * Here, the vfs has already taken i_mutex on both old_dir and new_dir.
1163 */ 1163 */
1164 mutex_lock_nested(&JFS_IP(new_dir)->commit_mutex, COMMIT_MUTEX_PARENT); 1164 mutex_lock_nested(&JFS_IP(new_dir)->commit_mutex, COMMIT_MUTEX_PARENT);
1165 mutex_lock_nested(&JFS_IP(old_ip)->commit_mutex, COMMIT_MUTEX_CHILD); 1165 mutex_lock_nested(&JFS_IP(old_ip)->commit_mutex, COMMIT_MUTEX_CHILD);
1166 if (old_dir != new_dir) 1166 if (old_dir != new_dir)
1167 mutex_lock_nested(&JFS_IP(old_dir)->commit_mutex, 1167 mutex_lock_nested(&JFS_IP(old_dir)->commit_mutex,
1168 COMMIT_MUTEX_SECOND_PARENT); 1168 COMMIT_MUTEX_SECOND_PARENT);
1169 1169
1170 if (new_ip) { 1170 if (new_ip) {
1171 mutex_lock_nested(&JFS_IP(new_ip)->commit_mutex, 1171 mutex_lock_nested(&JFS_IP(new_ip)->commit_mutex,
1172 COMMIT_MUTEX_VICTIM); 1172 COMMIT_MUTEX_VICTIM);
1173 /* 1173 /*
1174 * Change existing directory entry to new inode number 1174 * Change existing directory entry to new inode number
1175 */ 1175 */
1176 ino = new_ip->i_ino; 1176 ino = new_ip->i_ino;
1177 rc = dtModify(tid, new_dir, &new_dname, &ino, 1177 rc = dtModify(tid, new_dir, &new_dname, &ino,
1178 old_ip->i_ino, JFS_RENAME); 1178 old_ip->i_ino, JFS_RENAME);
1179 if (rc) 1179 if (rc)
1180 goto out4; 1180 goto out4;
1181 drop_nlink(new_ip); 1181 drop_nlink(new_ip);
1182 if (S_ISDIR(new_ip->i_mode)) { 1182 if (S_ISDIR(new_ip->i_mode)) {
1183 drop_nlink(new_ip); 1183 drop_nlink(new_ip);
1184 if (new_ip->i_nlink) { 1184 if (new_ip->i_nlink) {
1185 mutex_unlock(&JFS_IP(new_ip)->commit_mutex); 1185 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
1186 if (old_dir != new_dir) 1186 if (old_dir != new_dir)
1187 mutex_unlock(&JFS_IP(old_dir)->commit_mutex); 1187 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
1188 mutex_unlock(&JFS_IP(old_ip)->commit_mutex); 1188 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1189 mutex_unlock(&JFS_IP(new_dir)->commit_mutex); 1189 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
1190 if (!S_ISDIR(old_ip->i_mode) && new_ip) 1190 if (!S_ISDIR(old_ip->i_mode) && new_ip)
1191 IWRITE_UNLOCK(new_ip); 1191 IWRITE_UNLOCK(new_ip);
1192 jfs_error(new_ip->i_sb, 1192 jfs_error(new_ip->i_sb,
1193 "jfs_rename: new_ip->i_nlink != 0"); 1193 "jfs_rename: new_ip->i_nlink != 0");
1194 return -EIO; 1194 return -EIO;
1195 } 1195 }
1196 tblk = tid_to_tblock(tid); 1196 tblk = tid_to_tblock(tid);
1197 tblk->xflag |= COMMIT_DELETE; 1197 tblk->xflag |= COMMIT_DELETE;
1198 tblk->u.ip = new_ip; 1198 tblk->u.ip = new_ip;
1199 } else if (new_ip->i_nlink == 0) { 1199 } else if (new_ip->i_nlink == 0) {
1200 assert(!test_cflag(COMMIT_Nolink, new_ip)); 1200 assert(!test_cflag(COMMIT_Nolink, new_ip));
1201 /* free block resources */ 1201 /* free block resources */
1202 if ((new_size = commitZeroLink(tid, new_ip)) < 0) { 1202 if ((new_size = commitZeroLink(tid, new_ip)) < 0) {
1203 txAbort(tid, 1); /* Marks FS Dirty */ 1203 txAbort(tid, 1); /* Marks FS Dirty */
1204 rc = new_size; 1204 rc = new_size;
1205 goto out4; 1205 goto out4;
1206 } 1206 }
1207 tblk = tid_to_tblock(tid); 1207 tblk = tid_to_tblock(tid);
1208 tblk->xflag |= COMMIT_DELETE; 1208 tblk->xflag |= COMMIT_DELETE;
1209 tblk->u.ip = new_ip; 1209 tblk->u.ip = new_ip;
1210 } else { 1210 } else {
1211 new_ip->i_ctime = CURRENT_TIME; 1211 new_ip->i_ctime = CURRENT_TIME;
1212 mark_inode_dirty(new_ip); 1212 mark_inode_dirty(new_ip);
1213 } 1213 }
1214 } else { 1214 } else {
1215 /* 1215 /*
1216 * Add new directory entry 1216 * Add new directory entry
1217 */ 1217 */
1218 rc = dtSearch(new_dir, &new_dname, &ino, &btstack, 1218 rc = dtSearch(new_dir, &new_dname, &ino, &btstack,
1219 JFS_CREATE); 1219 JFS_CREATE);
1220 if (rc) { 1220 if (rc) {
1221 jfs_err("jfs_rename didn't expect dtSearch to fail " 1221 jfs_err("jfs_rename didn't expect dtSearch to fail "
1222 "w/rc = %d", rc); 1222 "w/rc = %d", rc);
1223 goto out4; 1223 goto out4;
1224 } 1224 }
1225 1225
1226 ino = old_ip->i_ino; 1226 ino = old_ip->i_ino;
1227 rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack); 1227 rc = dtInsert(tid, new_dir, &new_dname, &ino, &btstack);
1228 if (rc) { 1228 if (rc) {
1229 if (rc == -EIO) 1229 if (rc == -EIO)
1230 jfs_err("jfs_rename: dtInsert returned -EIO"); 1230 jfs_err("jfs_rename: dtInsert returned -EIO");
1231 goto out4; 1231 goto out4;
1232 } 1232 }
1233 if (S_ISDIR(old_ip->i_mode)) 1233 if (S_ISDIR(old_ip->i_mode))
1234 inc_nlink(new_dir); 1234 inc_nlink(new_dir);
1235 } 1235 }
1236 /* 1236 /*
1237 * Remove old directory entry 1237 * Remove old directory entry
1238 */ 1238 */
1239 1239
1240 ino = old_ip->i_ino; 1240 ino = old_ip->i_ino;
1241 rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE); 1241 rc = dtDelete(tid, old_dir, &old_dname, &ino, JFS_REMOVE);
1242 if (rc) { 1242 if (rc) {
1243 jfs_err("jfs_rename did not expect dtDelete to return rc = %d", 1243 jfs_err("jfs_rename did not expect dtDelete to return rc = %d",
1244 rc); 1244 rc);
1245 txAbort(tid, 1); /* Marks Filesystem dirty */ 1245 txAbort(tid, 1); /* Marks Filesystem dirty */
1246 goto out4; 1246 goto out4;
1247 } 1247 }
1248 if (S_ISDIR(old_ip->i_mode)) { 1248 if (S_ISDIR(old_ip->i_mode)) {
1249 drop_nlink(old_dir); 1249 drop_nlink(old_dir);
1250 if (old_dir != new_dir) { 1250 if (old_dir != new_dir) {
1251 /* 1251 /*
1252 * Change inode number of parent for moved directory 1252 * Change inode number of parent for moved directory
1253 */ 1253 */
1254 1254
1255 JFS_IP(old_ip)->i_dtroot.header.idotdot = 1255 JFS_IP(old_ip)->i_dtroot.header.idotdot =
1256 cpu_to_le32(new_dir->i_ino); 1256 cpu_to_le32(new_dir->i_ino);
1257 1257
1258 /* Linelock header of dtree */ 1258 /* Linelock header of dtree */
1259 tlck = txLock(tid, old_ip, 1259 tlck = txLock(tid, old_ip,
1260 (struct metapage *) &JFS_IP(old_ip)->bxflag, 1260 (struct metapage *) &JFS_IP(old_ip)->bxflag,
1261 tlckDTREE | tlckBTROOT | tlckRELINK); 1261 tlckDTREE | tlckBTROOT | tlckRELINK);
1262 dtlck = (struct dt_lock *) & tlck->lock; 1262 dtlck = (struct dt_lock *) & tlck->lock;
1263 ASSERT(dtlck->index == 0); 1263 ASSERT(dtlck->index == 0);
1264 lv = & dtlck->lv[0]; 1264 lv = & dtlck->lv[0];
1265 lv->offset = 0; 1265 lv->offset = 0;
1266 lv->length = 1; 1266 lv->length = 1;
1267 dtlck->index++; 1267 dtlck->index++;
1268 } 1268 }
1269 } 1269 }
1270 1270
1271 /* 1271 /*
1272 * Update ctime on changed/moved inodes & mark dirty 1272 * Update ctime on changed/moved inodes & mark dirty
1273 */ 1273 */
1274 old_ip->i_ctime = CURRENT_TIME; 1274 old_ip->i_ctime = CURRENT_TIME;
1275 mark_inode_dirty(old_ip); 1275 mark_inode_dirty(old_ip);
1276 1276
1277 new_dir->i_ctime = new_dir->i_mtime = current_fs_time(new_dir->i_sb); 1277 new_dir->i_ctime = new_dir->i_mtime = current_fs_time(new_dir->i_sb);
1278 mark_inode_dirty(new_dir); 1278 mark_inode_dirty(new_dir);
1279 1279
1280 /* Build list of inodes modified by this transaction */ 1280 /* Build list of inodes modified by this transaction */
1281 ipcount = 0; 1281 ipcount = 0;
1282 iplist[ipcount++] = old_ip; 1282 iplist[ipcount++] = old_ip;
1283 if (new_ip) 1283 if (new_ip)
1284 iplist[ipcount++] = new_ip; 1284 iplist[ipcount++] = new_ip;
1285 iplist[ipcount++] = old_dir; 1285 iplist[ipcount++] = old_dir;
1286 1286
1287 if (old_dir != new_dir) { 1287 if (old_dir != new_dir) {
1288 iplist[ipcount++] = new_dir; 1288 iplist[ipcount++] = new_dir;
1289 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME; 1289 old_dir->i_ctime = old_dir->i_mtime = CURRENT_TIME;
1290 mark_inode_dirty(old_dir); 1290 mark_inode_dirty(old_dir);
1291 } 1291 }
1292 1292
1293 /* 1293 /*
1294 * Incomplete truncate of file data can 1294 * Incomplete truncate of file data can
1295 * result in timing problems unless we synchronously commit the 1295 * result in timing problems unless we synchronously commit the
1296 * transaction. 1296 * transaction.
1297 */ 1297 */
1298 if (new_size) 1298 if (new_size)
1299 commit_flag = COMMIT_SYNC; 1299 commit_flag = COMMIT_SYNC;
1300 else 1300 else
1301 commit_flag = 0; 1301 commit_flag = 0;
1302 1302
1303 rc = txCommit(tid, ipcount, iplist, commit_flag); 1303 rc = txCommit(tid, ipcount, iplist, commit_flag);
1304 1304
1305 out4: 1305 out4:
1306 txEnd(tid); 1306 txEnd(tid);
1307 if (new_ip) 1307 if (new_ip)
1308 mutex_unlock(&JFS_IP(new_ip)->commit_mutex); 1308 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
1309 if (old_dir != new_dir) 1309 if (old_dir != new_dir)
1310 mutex_unlock(&JFS_IP(old_dir)->commit_mutex); 1310 mutex_unlock(&JFS_IP(old_dir)->commit_mutex);
1311 mutex_unlock(&JFS_IP(old_ip)->commit_mutex); 1311 mutex_unlock(&JFS_IP(old_ip)->commit_mutex);
1312 mutex_unlock(&JFS_IP(new_dir)->commit_mutex); 1312 mutex_unlock(&JFS_IP(new_dir)->commit_mutex);
1313 1313
1314 while (new_size && (rc == 0)) { 1314 while (new_size && (rc == 0)) {
1315 tid = txBegin(new_ip->i_sb, 0); 1315 tid = txBegin(new_ip->i_sb, 0);
1316 mutex_lock(&JFS_IP(new_ip)->commit_mutex); 1316 mutex_lock(&JFS_IP(new_ip)->commit_mutex);
1317 new_size = xtTruncate_pmap(tid, new_ip, new_size); 1317 new_size = xtTruncate_pmap(tid, new_ip, new_size);
1318 if (new_size < 0) { 1318 if (new_size < 0) {
1319 txAbort(tid, 1); 1319 txAbort(tid, 1);
1320 rc = new_size; 1320 rc = new_size;
1321 } else 1321 } else
1322 rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC); 1322 rc = txCommit(tid, 1, &new_ip, COMMIT_SYNC);
1323 txEnd(tid); 1323 txEnd(tid);
1324 mutex_unlock(&JFS_IP(new_ip)->commit_mutex); 1324 mutex_unlock(&JFS_IP(new_ip)->commit_mutex);
1325 } 1325 }
1326 if (new_ip && (new_ip->i_nlink == 0)) 1326 if (new_ip && (new_ip->i_nlink == 0))
1327 set_cflag(COMMIT_Nolink, new_ip); 1327 set_cflag(COMMIT_Nolink, new_ip);
1328 out3: 1328 out3:
1329 free_UCSname(&new_dname); 1329 free_UCSname(&new_dname);
1330 out2: 1330 out2:
1331 free_UCSname(&old_dname); 1331 free_UCSname(&old_dname);
1332 out1: 1332 out1:
1333 if (new_ip && !S_ISDIR(new_ip->i_mode)) 1333 if (new_ip && !S_ISDIR(new_ip->i_mode))
1334 IWRITE_UNLOCK(new_ip); 1334 IWRITE_UNLOCK(new_ip);
1335 /* 1335 /*
1336 * Truncating the directory index table is not guaranteed. It 1336 * Truncating the directory index table is not guaranteed. It
1337 * may need to be done iteratively 1337 * may need to be done iteratively
1338 */ 1338 */
1339 if (test_cflag(COMMIT_Stale, old_dir)) { 1339 if (test_cflag(COMMIT_Stale, old_dir)) {
1340 if (old_dir->i_size > 1) 1340 if (old_dir->i_size > 1)
1341 jfs_truncate_nolock(old_dir, 0); 1341 jfs_truncate_nolock(old_dir, 0);
1342 1342
1343 clear_cflag(COMMIT_Stale, old_dir); 1343 clear_cflag(COMMIT_Stale, old_dir);
1344 } 1344 }
1345 1345
1346 jfs_info("jfs_rename: returning %d", rc); 1346 jfs_info("jfs_rename: returning %d", rc);
1347 return rc; 1347 return rc;
1348 } 1348 }
1349 1349
1350 1350
1351 /* 1351 /*
1352 * NAME: jfs_mknod 1352 * NAME: jfs_mknod
1353 * 1353 *
1354 * FUNCTION: Create a special file (device) 1354 * FUNCTION: Create a special file (device)
1355 */ 1355 */
1356 static int jfs_mknod(struct inode *dir, struct dentry *dentry, 1356 static int jfs_mknod(struct inode *dir, struct dentry *dentry,
1357 int mode, dev_t rdev) 1357 int mode, dev_t rdev)
1358 { 1358 {
1359 struct jfs_inode_info *jfs_ip; 1359 struct jfs_inode_info *jfs_ip;
1360 struct btstack btstack; 1360 struct btstack btstack;
1361 struct component_name dname; 1361 struct component_name dname;
1362 ino_t ino; 1362 ino_t ino;
1363 struct inode *ip; 1363 struct inode *ip;
1364 struct inode *iplist[2]; 1364 struct inode *iplist[2];
1365 int rc; 1365 int rc;
1366 tid_t tid; 1366 tid_t tid;
1367 struct tblock *tblk; 1367 struct tblock *tblk;
1368 1368
1369 if (!new_valid_dev(rdev)) 1369 if (!new_valid_dev(rdev))
1370 return -EINVAL; 1370 return -EINVAL;
1371 1371
1372 jfs_info("jfs_mknod: %s", dentry->d_name.name); 1372 jfs_info("jfs_mknod: %s", dentry->d_name.name);
1373 1373
1374 dquot_initialize(dir); 1374 dquot_initialize(dir);
1375 1375
1376 if ((rc = get_UCSname(&dname, dentry))) 1376 if ((rc = get_UCSname(&dname, dentry)))
1377 goto out; 1377 goto out;
1378 1378
1379 ip = ialloc(dir, mode); 1379 ip = ialloc(dir, mode);
1380 if (IS_ERR(ip)) { 1380 if (IS_ERR(ip)) {
1381 rc = PTR_ERR(ip); 1381 rc = PTR_ERR(ip);
1382 goto out1; 1382 goto out1;
1383 } 1383 }
1384 jfs_ip = JFS_IP(ip); 1384 jfs_ip = JFS_IP(ip);
1385 1385
1386 tid = txBegin(dir->i_sb, 0); 1386 tid = txBegin(dir->i_sb, 0);
1387 1387
1388 mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT); 1388 mutex_lock_nested(&JFS_IP(dir)->commit_mutex, COMMIT_MUTEX_PARENT);
1389 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD); 1389 mutex_lock_nested(&JFS_IP(ip)->commit_mutex, COMMIT_MUTEX_CHILD);
1390 1390
1391 rc = jfs_init_acl(tid, ip, dir); 1391 rc = jfs_init_acl(tid, ip, dir);
1392 if (rc) 1392 if (rc)
1393 goto out3; 1393 goto out3;
1394 1394
1395 rc = jfs_init_security(tid, ip, dir, &dentry->d_name); 1395 rc = jfs_init_security(tid, ip, dir, &dentry->d_name);
1396 if (rc) { 1396 if (rc) {
1397 txAbort(tid, 0); 1397 txAbort(tid, 0);
1398 goto out3; 1398 goto out3;
1399 } 1399 }
1400 1400
1401 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) { 1401 if ((rc = dtSearch(dir, &dname, &ino, &btstack, JFS_CREATE))) {
1402 txAbort(tid, 0); 1402 txAbort(tid, 0);
1403 goto out3; 1403 goto out3;
1404 } 1404 }
1405 1405
1406 tblk = tid_to_tblock(tid); 1406 tblk = tid_to_tblock(tid);
1407 tblk->xflag |= COMMIT_CREATE; 1407 tblk->xflag |= COMMIT_CREATE;
1408 tblk->ino = ip->i_ino; 1408 tblk->ino = ip->i_ino;
1409 tblk->u.ixpxd = JFS_IP(ip)->ixpxd; 1409 tblk->u.ixpxd = JFS_IP(ip)->ixpxd;
1410 1410
1411 ino = ip->i_ino; 1411 ino = ip->i_ino;
1412 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) { 1412 if ((rc = dtInsert(tid, dir, &dname, &ino, &btstack))) {
1413 txAbort(tid, 0); 1413 txAbort(tid, 0);
1414 goto out3; 1414 goto out3;
1415 } 1415 }
1416 1416
1417 ip->i_op = &jfs_file_inode_operations; 1417 ip->i_op = &jfs_file_inode_operations;
1418 jfs_ip->dev = new_encode_dev(rdev); 1418 jfs_ip->dev = new_encode_dev(rdev);
1419 init_special_inode(ip, ip->i_mode, rdev); 1419 init_special_inode(ip, ip->i_mode, rdev);
1420 1420
1421 mark_inode_dirty(ip); 1421 mark_inode_dirty(ip);
1422 1422
1423 dir->i_ctime = dir->i_mtime = CURRENT_TIME; 1423 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
1424 1424
1425 mark_inode_dirty(dir); 1425 mark_inode_dirty(dir);
1426 1426
1427 iplist[0] = dir; 1427 iplist[0] = dir;
1428 iplist[1] = ip; 1428 iplist[1] = ip;
1429 rc = txCommit(tid, 2, iplist, 0); 1429 rc = txCommit(tid, 2, iplist, 0);
1430 1430
1431 out3: 1431 out3:
1432 txEnd(tid); 1432 txEnd(tid);
1433 mutex_unlock(&JFS_IP(ip)->commit_mutex); 1433 mutex_unlock(&JFS_IP(ip)->commit_mutex);
1434 mutex_unlock(&JFS_IP(dir)->commit_mutex); 1434 mutex_unlock(&JFS_IP(dir)->commit_mutex);
1435 if (rc) { 1435 if (rc) {
1436 free_ea_wmap(ip); 1436 free_ea_wmap(ip);
1437 ip->i_nlink = 0; 1437 ip->i_nlink = 0;
1438 unlock_new_inode(ip); 1438 unlock_new_inode(ip);
1439 iput(ip); 1439 iput(ip);
1440 } else { 1440 } else {
1441 d_instantiate(dentry, ip); 1441 d_instantiate(dentry, ip);
1442 unlock_new_inode(ip); 1442 unlock_new_inode(ip);
1443 } 1443 }
1444 1444
1445 out1: 1445 out1:
1446 free_UCSname(&dname); 1446 free_UCSname(&dname);
1447 1447
1448 out: 1448 out:
1449 jfs_info("jfs_mknod: returning %d", rc); 1449 jfs_info("jfs_mknod: returning %d", rc);
1450 return rc; 1450 return rc;
1451 } 1451 }
1452 1452
1453 static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, struct nameidata *nd) 1453 static struct dentry *jfs_lookup(struct inode *dip, struct dentry *dentry, struct nameidata *nd)
1454 { 1454 {
1455 struct btstack btstack; 1455 struct btstack btstack;
1456 ino_t inum; 1456 ino_t inum;
1457 struct inode *ip; 1457 struct inode *ip;
1458 struct component_name key; 1458 struct component_name key;
1459 const char *name = dentry->d_name.name; 1459 const char *name = dentry->d_name.name;
1460 int len = dentry->d_name.len; 1460 int len = dentry->d_name.len;
1461 int rc; 1461 int rc;
1462 1462
1463 jfs_info("jfs_lookup: name = %s", name); 1463 jfs_info("jfs_lookup: name = %s", name);
1464 1464
1465 if ((name[0] == '.') && (len == 1)) 1465 if ((name[0] == '.') && (len == 1))
1466 inum = dip->i_ino; 1466 inum = dip->i_ino;
1467 else if (strcmp(name, "..") == 0) 1467 else if (strcmp(name, "..") == 0)
1468 inum = PARENT(dip); 1468 inum = PARENT(dip);
1469 else { 1469 else {
1470 if ((rc = get_UCSname(&key, dentry))) 1470 if ((rc = get_UCSname(&key, dentry)))
1471 return ERR_PTR(rc); 1471 return ERR_PTR(rc);
1472 rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP); 1472 rc = dtSearch(dip, &key, &inum, &btstack, JFS_LOOKUP);
1473 free_UCSname(&key); 1473 free_UCSname(&key);
1474 if (rc == -ENOENT) { 1474 if (rc == -ENOENT) {
1475 d_add(dentry, NULL); 1475 d_add(dentry, NULL);
1476 return NULL; 1476 return NULL;
1477 } else if (rc) { 1477 } else if (rc) {
1478 jfs_err("jfs_lookup: dtSearch returned %d", rc); 1478 jfs_err("jfs_lookup: dtSearch returned %d", rc);
1479 return ERR_PTR(rc); 1479 return ERR_PTR(rc);
1480 } 1480 }
1481 } 1481 }
1482 1482
1483 ip = jfs_iget(dip->i_sb, inum); 1483 ip = jfs_iget(dip->i_sb, inum);
1484 if (IS_ERR(ip)) { 1484 if (IS_ERR(ip)) {
1485 jfs_err("jfs_lookup: iget failed on inum %d", (uint) inum); 1485 jfs_err("jfs_lookup: iget failed on inum %d", (uint) inum);
1486 return ERR_CAST(ip); 1486 return ERR_CAST(ip);
1487 } 1487 }
1488 1488
1489 return d_splice_alias(ip, dentry); 1489 return d_splice_alias(ip, dentry);
1490 } 1490 }
1491 1491
1492 static struct inode *jfs_nfs_get_inode(struct super_block *sb, 1492 static struct inode *jfs_nfs_get_inode(struct super_block *sb,
1493 u64 ino, u32 generation) 1493 u64 ino, u32 generation)
1494 { 1494 {
1495 struct inode *inode; 1495 struct inode *inode;
1496 1496
1497 if (ino == 0) 1497 if (ino == 0)
1498 return ERR_PTR(-ESTALE); 1498 return ERR_PTR(-ESTALE);
1499 inode = jfs_iget(sb, ino); 1499 inode = jfs_iget(sb, ino);
1500 if (IS_ERR(inode)) 1500 if (IS_ERR(inode))
1501 return ERR_CAST(inode); 1501 return ERR_CAST(inode);
1502 1502
1503 if (generation && inode->i_generation != generation) { 1503 if (generation && inode->i_generation != generation) {
1504 iput(inode); 1504 iput(inode);
1505 return ERR_PTR(-ESTALE); 1505 return ERR_PTR(-ESTALE);
1506 } 1506 }
1507 1507
1508 return inode; 1508 return inode;
1509 } 1509 }
1510 1510
1511 struct dentry *jfs_fh_to_dentry(struct super_block *sb, struct fid *fid, 1511 struct dentry *jfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
1512 int fh_len, int fh_type) 1512 int fh_len, int fh_type)
1513 { 1513 {
1514 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, 1514 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1515 jfs_nfs_get_inode); 1515 jfs_nfs_get_inode);
1516 } 1516 }
1517 1517
1518 struct dentry *jfs_fh_to_parent(struct super_block *sb, struct fid *fid, 1518 struct dentry *jfs_fh_to_parent(struct super_block *sb, struct fid *fid,
1519 int fh_len, int fh_type) 1519 int fh_len, int fh_type)
1520 { 1520 {
1521 return generic_fh_to_parent(sb, fid, fh_len, fh_type, 1521 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1522 jfs_nfs_get_inode); 1522 jfs_nfs_get_inode);
1523 } 1523 }
1524 1524
1525 struct dentry *jfs_get_parent(struct dentry *dentry) 1525 struct dentry *jfs_get_parent(struct dentry *dentry)
1526 { 1526 {
1527 unsigned long parent_ino; 1527 unsigned long parent_ino;
1528 1528
1529 parent_ino = 1529 parent_ino =
1530 le32_to_cpu(JFS_IP(dentry->d_inode)->i_dtroot.header.idotdot); 1530 le32_to_cpu(JFS_IP(dentry->d_inode)->i_dtroot.header.idotdot);
1531 1531
1532 return d_obtain_alias(jfs_iget(dentry->d_inode->i_sb, parent_ino)); 1532 return d_obtain_alias(jfs_iget(dentry->d_inode->i_sb, parent_ino));
1533 } 1533 }
1534 1534
1535 const struct inode_operations jfs_dir_inode_operations = { 1535 const struct inode_operations jfs_dir_inode_operations = {
1536 .create = jfs_create, 1536 .create = jfs_create,
1537 .lookup = jfs_lookup, 1537 .lookup = jfs_lookup,
1538 .link = jfs_link, 1538 .link = jfs_link,
1539 .unlink = jfs_unlink, 1539 .unlink = jfs_unlink,
1540 .symlink = jfs_symlink, 1540 .symlink = jfs_symlink,
1541 .mkdir = jfs_mkdir, 1541 .mkdir = jfs_mkdir,
1542 .rmdir = jfs_rmdir, 1542 .rmdir = jfs_rmdir,
1543 .mknod = jfs_mknod, 1543 .mknod = jfs_mknod,
1544 .rename = jfs_rename, 1544 .rename = jfs_rename,
1545 .setxattr = jfs_setxattr, 1545 .setxattr = jfs_setxattr,
1546 .getxattr = jfs_getxattr, 1546 .getxattr = jfs_getxattr,
1547 .listxattr = jfs_listxattr, 1547 .listxattr = jfs_listxattr,
1548 .removexattr = jfs_removexattr, 1548 .removexattr = jfs_removexattr,
1549 .setattr = jfs_setattr, 1549 .setattr = jfs_setattr,
1550 #ifdef CONFIG_JFS_POSIX_ACL 1550 #ifdef CONFIG_JFS_POSIX_ACL
1551 .check_acl = jfs_check_acl, 1551 .check_acl = jfs_check_acl,
1552 #endif 1552 #endif
1553 }; 1553 };
1554 1554
1555 const struct file_operations jfs_dir_operations = { 1555 const struct file_operations jfs_dir_operations = {
1556 .read = generic_read_dir, 1556 .read = generic_read_dir,
1557 .readdir = jfs_readdir, 1557 .readdir = jfs_readdir,
1558 .fsync = jfs_fsync, 1558 .fsync = jfs_fsync,
1559 .unlocked_ioctl = jfs_ioctl, 1559 .unlocked_ioctl = jfs_ioctl,
1560 #ifdef CONFIG_COMPAT 1560 #ifdef CONFIG_COMPAT
1561 .compat_ioctl = jfs_compat_ioctl, 1561 .compat_ioctl = jfs_compat_ioctl,
1562 #endif 1562 #endif
1563 .llseek = generic_file_llseek, 1563 .llseek = generic_file_llseek,
1564 }; 1564 };
1565 1565
1566 static int jfs_ci_hash(const struct dentry *dir, const struct inode *inode, 1566 static int jfs_ci_hash(const struct dentry *dir, const struct inode *inode,
1567 struct qstr *this) 1567 struct qstr *this)
1568 { 1568 {
1569 unsigned long hash; 1569 unsigned long hash;
1570 int i; 1570 int i;
1571 1571
1572 hash = init_name_hash(); 1572 hash = init_name_hash();
1573 for (i=0; i < this->len; i++) 1573 for (i=0; i < this->len; i++)
1574 hash = partial_name_hash(tolower(this->name[i]), hash); 1574 hash = partial_name_hash(tolower(this->name[i]), hash);
1575 this->hash = end_name_hash(hash); 1575 this->hash = end_name_hash(hash);
1576 1576
1577 return 0; 1577 return 0;
1578 } 1578 }
1579 1579
1580 static int jfs_ci_compare(const struct dentry *parent, 1580 static int jfs_ci_compare(const struct dentry *parent,
1581 const struct inode *pinode, 1581 const struct inode *pinode,
1582 const struct dentry *dentry, const struct inode *inode, 1582 const struct dentry *dentry, const struct inode *inode,
1583 unsigned int len, const char *str, const struct qstr *name) 1583 unsigned int len, const char *str, const struct qstr *name)
1584 { 1584 {
1585 int i, result = 1; 1585 int i, result = 1;
1586 1586
1587 if (len != name->len) 1587 if (len != name->len)
1588 goto out; 1588 goto out;
1589 for (i=0; i < len; i++) { 1589 for (i=0; i < len; i++) {
1590 if (tolower(str[i]) != tolower(name->name[i])) 1590 if (tolower(str[i]) != tolower(name->name[i]))
1591 goto out; 1591 goto out;
1592 } 1592 }
1593 result = 0; 1593 result = 0;
1594 out: 1594 out:
1595 return result; 1595 return result;
1596 } 1596 }
1597 1597
1598 static int jfs_ci_revalidate(struct dentry *dentry, struct nameidata *nd) 1598 static int jfs_ci_revalidate(struct dentry *dentry, struct nameidata *nd)
1599 { 1599 {
1600 if (nd && nd->flags & LOOKUP_RCU) 1600 if (nd && nd->flags & LOOKUP_RCU)
1601 return -ECHILD; 1601 return -ECHILD;
1602 /* 1602 /*
1603 * This is not negative dentry. Always valid. 1603 * This is not negative dentry. Always valid.
1604 * 1604 *
1605 * Note, rename() to existing directory entry will have ->d_inode, 1605 * Note, rename() to existing directory entry will have ->d_inode,
1606 * and will use existing name which isn't specified name by user. 1606 * and will use existing name which isn't specified name by user.
1607 * 1607 *
1608 * We may be able to drop this positive dentry here. But dropping 1608 * We may be able to drop this positive dentry here. But dropping
1609 * positive dentry isn't good idea. So it's unsupported like 1609 * positive dentry isn't good idea. So it's unsupported like
1610 * rename("filename", "FILENAME") for now. 1610 * rename("filename", "FILENAME") for now.
1611 */ 1611 */
1612 if (dentry->d_inode) 1612 if (dentry->d_inode)
1613 return 1; 1613 return 1;
1614 1614
1615 /* 1615 /*
1616 * This may be nfsd (or something), anyway, we can't see the 1616 * This may be nfsd (or something), anyway, we can't see the
1617 * intent of this. So, since this can be for creation, drop it. 1617 * intent of this. So, since this can be for creation, drop it.
1618 */ 1618 */
1619 if (!nd) 1619 if (!nd)
1620 return 0; 1620 return 0;
1621 1621
1622 /* 1622 /*
1623 * Drop the negative dentry, in order to make sure to use the 1623 * Drop the negative dentry, in order to make sure to use the
1624 * case sensitive name which is specified by user if this is 1624 * case sensitive name which is specified by user if this is
1625 * for creation. 1625 * for creation.
1626 */ 1626 */
1627 if (!(nd->flags & (LOOKUP_CONTINUE | LOOKUP_PARENT))) { 1627 if (nd->flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
1628 if (nd->flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) 1628 return 0;
1629 return 0;
1630 }
1631 return 1; 1629 return 1;
1632 } 1630 }
1633 1631
1634 const struct dentry_operations jfs_ci_dentry_operations = 1632 const struct dentry_operations jfs_ci_dentry_operations =
1635 { 1633 {
1636 .d_hash = jfs_ci_hash, 1634 .d_hash = jfs_ci_hash,
1637 .d_compare = jfs_ci_compare, 1635 .d_compare = jfs_ci_compare,
1638 .d_revalidate = jfs_ci_revalidate, 1636 .d_revalidate = jfs_ci_revalidate,
1639 }; 1637 };
1640 1638