Merge commit 'gcl/next' into next
[deliverable/linux.git] / fs / cifs / dir.c
1 /*
2 * fs/cifs/dir.c
3 *
4 * vfs operations that deal with dentries
5 *
6 * Copyright (C) International Business Machines Corp., 2002,2008
7 * Author(s): Steve French (sfrench@us.ibm.com)
8 *
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
11 * by the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
17 * the GNU Lesser General Public License for more details.
18 *
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
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23 #include <linux/fs.h>
24 #include <linux/stat.h>
25 #include <linux/slab.h>
26 #include <linux/namei.h>
27 #include "cifsfs.h"
28 #include "cifspdu.h"
29 #include "cifsglob.h"
30 #include "cifsproto.h"
31 #include "cifs_debug.h"
32 #include "cifs_fs_sb.h"
33
34 static void
35 renew_parental_timestamps(struct dentry *direntry)
36 {
37 /* BB check if there is a way to get the kernel to do this or if we
38 really need this */
39 do {
40 direntry->d_time = jiffies;
41 direntry = direntry->d_parent;
42 } while (!IS_ROOT(direntry));
43 }
44
45 /* Note: caller must free return buffer */
46 char *
47 build_path_from_dentry(struct dentry *direntry)
48 {
49 struct dentry *temp;
50 int namelen;
51 int pplen;
52 int dfsplen;
53 char *full_path;
54 char dirsep;
55 struct cifs_sb_info *cifs_sb;
56
57 if (direntry == NULL)
58 return NULL; /* not much we can do if dentry is freed and
59 we need to reopen the file after it was closed implicitly
60 when the server crashed */
61
62 cifs_sb = CIFS_SB(direntry->d_sb);
63 dirsep = CIFS_DIR_SEP(cifs_sb);
64 pplen = cifs_sb->prepathlen;
65 if (cifs_sb->tcon && (cifs_sb->tcon->Flags & SMB_SHARE_IS_IN_DFS))
66 dfsplen = strnlen(cifs_sb->tcon->treeName, MAX_TREE_SIZE + 1);
67 else
68 dfsplen = 0;
69 cifs_bp_rename_retry:
70 namelen = pplen + dfsplen;
71 for (temp = direntry; !IS_ROOT(temp);) {
72 namelen += (1 + temp->d_name.len);
73 temp = temp->d_parent;
74 if (temp == NULL) {
75 cERROR(1, ("corrupt dentry"));
76 return NULL;
77 }
78 }
79
80 full_path = kmalloc(namelen+1, GFP_KERNEL);
81 if (full_path == NULL)
82 return full_path;
83 full_path[namelen] = 0; /* trailing null */
84 for (temp = direntry; !IS_ROOT(temp);) {
85 namelen -= 1 + temp->d_name.len;
86 if (namelen < 0) {
87 break;
88 } else {
89 full_path[namelen] = dirsep;
90 strncpy(full_path + namelen + 1, temp->d_name.name,
91 temp->d_name.len);
92 cFYI(0, ("name: %s", full_path + namelen));
93 }
94 temp = temp->d_parent;
95 if (temp == NULL) {
96 cERROR(1, ("corrupt dentry"));
97 kfree(full_path);
98 return NULL;
99 }
100 }
101 if (namelen != pplen + dfsplen) {
102 cERROR(1,
103 ("did not end path lookup where expected namelen is %d",
104 namelen));
105 /* presumably this is only possible if racing with a rename
106 of one of the parent directories (we can not lock the dentries
107 above us to prevent this, but retrying should be harmless) */
108 kfree(full_path);
109 goto cifs_bp_rename_retry;
110 }
111 /* DIR_SEP already set for byte 0 / vs \ but not for
112 subsequent slashes in prepath which currently must
113 be entered the right way - not sure if there is an alternative
114 since the '\' is a valid posix character so we can not switch
115 those safely to '/' if any are found in the middle of the prepath */
116 /* BB test paths to Windows with '/' in the midst of prepath */
117
118 if (dfsplen) {
119 strncpy(full_path, cifs_sb->tcon->treeName, dfsplen);
120 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) {
121 int i;
122 for (i = 0; i < dfsplen; i++) {
123 if (full_path[i] == '\\')
124 full_path[i] = '/';
125 }
126 }
127 }
128 strncpy(full_path + dfsplen, CIFS_SB(direntry->d_sb)->prepath, pplen);
129 return full_path;
130 }
131
132 static void setup_cifs_dentry(struct cifsTconInfo *tcon,
133 struct dentry *direntry,
134 struct inode *newinode)
135 {
136 if (tcon->nocase)
137 direntry->d_op = &cifs_ci_dentry_ops;
138 else
139 direntry->d_op = &cifs_dentry_ops;
140 d_instantiate(direntry, newinode);
141 }
142
143 /* Inode operations in similar order to how they appear in Linux file fs.h */
144
145 int
146 cifs_create(struct inode *inode, struct dentry *direntry, int mode,
147 struct nameidata *nd)
148 {
149 int rc = -ENOENT;
150 int xid;
151 int create_options = CREATE_NOT_DIR;
152 int oplock = 0;
153 /* BB below access is too much for the mknod to request */
154 int desiredAccess = GENERIC_READ | GENERIC_WRITE;
155 __u16 fileHandle;
156 struct cifs_sb_info *cifs_sb;
157 struct cifsTconInfo *tcon;
158 char *full_path = NULL;
159 FILE_ALL_INFO *buf = NULL;
160 struct inode *newinode = NULL;
161 struct cifsInodeInfo *pCifsInode;
162 int disposition = FILE_OVERWRITE_IF;
163 bool write_only = false;
164
165 xid = GetXid();
166
167 cifs_sb = CIFS_SB(inode->i_sb);
168 tcon = cifs_sb->tcon;
169
170 full_path = build_path_from_dentry(direntry);
171 if (full_path == NULL) {
172 FreeXid(xid);
173 return -ENOMEM;
174 }
175
176 mode &= ~current->fs->umask;
177
178 if (nd && (nd->flags & LOOKUP_OPEN)) {
179 int oflags = nd->intent.open.flags;
180
181 desiredAccess = 0;
182 if (oflags & FMODE_READ)
183 desiredAccess |= GENERIC_READ;
184 if (oflags & FMODE_WRITE) {
185 desiredAccess |= GENERIC_WRITE;
186 if (!(oflags & FMODE_READ))
187 write_only = true;
188 }
189
190 if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
191 disposition = FILE_CREATE;
192 else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
193 disposition = FILE_OVERWRITE_IF;
194 else if ((oflags & O_CREAT) == O_CREAT)
195 disposition = FILE_OPEN_IF;
196 else
197 cFYI(1, ("Create flag not set in create function"));
198 }
199
200 /* BB add processing to set equivalent of mode - e.g. via CreateX with
201 ACLs */
202 if (oplockEnabled)
203 oplock = REQ_OPLOCK;
204
205 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
206 if (buf == NULL) {
207 kfree(full_path);
208 FreeXid(xid);
209 return -ENOMEM;
210 }
211
212 /*
213 * if we're not using unix extensions, see if we need to set
214 * ATTR_READONLY on the create call
215 */
216 if (!tcon->unix_ext && (mode & S_IWUGO) == 0)
217 create_options |= CREATE_OPTION_READONLY;
218
219 if (cifs_sb->tcon->ses->capabilities & CAP_NT_SMBS)
220 rc = CIFSSMBOpen(xid, tcon, full_path, disposition,
221 desiredAccess, create_options,
222 &fileHandle, &oplock, buf, cifs_sb->local_nls,
223 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
224 else
225 rc = -EIO; /* no NT SMB support fall into legacy open below */
226
227 if (rc == -EIO) {
228 /* old server, retry the open legacy style */
229 rc = SMBLegacyOpen(xid, tcon, full_path, disposition,
230 desiredAccess, create_options,
231 &fileHandle, &oplock, buf, cifs_sb->local_nls,
232 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
233 }
234 if (rc) {
235 cFYI(1, ("cifs_create returned 0x%x", rc));
236 } else {
237 /* If Open reported that we actually created a file
238 then we now have to set the mode if possible */
239 if ((tcon->unix_ext) && (oplock & CIFS_CREATE_ACTION)) {
240 struct cifs_unix_set_info_args args = {
241 .mode = mode,
242 .ctime = NO_CHANGE_64,
243 .atime = NO_CHANGE_64,
244 .mtime = NO_CHANGE_64,
245 .device = 0,
246 };
247
248 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
249 args.uid = (__u64) current_fsuid();
250 if (inode->i_mode & S_ISGID)
251 args.gid = (__u64) inode->i_gid;
252 else
253 args.gid = (__u64) current_fsgid();
254 } else {
255 args.uid = NO_CHANGE_64;
256 args.gid = NO_CHANGE_64;
257 }
258 CIFSSMBUnixSetInfo(xid, tcon, full_path, &args,
259 cifs_sb->local_nls,
260 cifs_sb->mnt_cifs_flags &
261 CIFS_MOUNT_MAP_SPECIAL_CHR);
262 } else {
263 /* BB implement mode setting via Windows security
264 descriptors e.g. */
265 /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/
266
267 /* Could set r/o dos attribute if mode & 0222 == 0 */
268 }
269
270 /* server might mask mode so we have to query for it */
271 if (tcon->unix_ext)
272 rc = cifs_get_inode_info_unix(&newinode, full_path,
273 inode->i_sb, xid);
274 else {
275 rc = cifs_get_inode_info(&newinode, full_path,
276 buf, inode->i_sb, xid,
277 &fileHandle);
278 if (newinode) {
279 if (cifs_sb->mnt_cifs_flags &
280 CIFS_MOUNT_DYNPERM)
281 newinode->i_mode = mode;
282 if ((oplock & CIFS_CREATE_ACTION) &&
283 (cifs_sb->mnt_cifs_flags &
284 CIFS_MOUNT_SET_UID)) {
285 newinode->i_uid = current_fsuid();
286 if (inode->i_mode & S_ISGID)
287 newinode->i_gid =
288 inode->i_gid;
289 else
290 newinode->i_gid =
291 current_fsgid();
292 }
293 }
294 }
295
296 if (rc != 0) {
297 cFYI(1, ("Create worked, get_inode_info failed rc = %d",
298 rc));
299 } else
300 setup_cifs_dentry(tcon, direntry, newinode);
301
302 if ((nd == NULL /* nfsd case - nfs srv does not set nd */) ||
303 (!(nd->flags & LOOKUP_OPEN))) {
304 /* mknod case - do not leave file open */
305 CIFSSMBClose(xid, tcon, fileHandle);
306 } else if (newinode) {
307 struct cifsFileInfo *pCifsFile =
308 kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL);
309
310 if (pCifsFile == NULL)
311 goto cifs_create_out;
312 pCifsFile->netfid = fileHandle;
313 pCifsFile->pid = current->tgid;
314 pCifsFile->pInode = newinode;
315 pCifsFile->invalidHandle = false;
316 pCifsFile->closePend = false;
317 init_MUTEX(&pCifsFile->fh_sem);
318 mutex_init(&pCifsFile->lock_mutex);
319 INIT_LIST_HEAD(&pCifsFile->llist);
320 atomic_set(&pCifsFile->wrtPending, 0);
321
322 /* set the following in open now
323 pCifsFile->pfile = file; */
324 write_lock(&GlobalSMBSeslock);
325 list_add(&pCifsFile->tlist, &tcon->openFileList);
326 pCifsInode = CIFS_I(newinode);
327 if (pCifsInode) {
328 /* if readable file instance put first in list*/
329 if (write_only) {
330 list_add_tail(&pCifsFile->flist,
331 &pCifsInode->openFileList);
332 } else {
333 list_add(&pCifsFile->flist,
334 &pCifsInode->openFileList);
335 }
336 if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) {
337 pCifsInode->clientCanCacheAll = true;
338 pCifsInode->clientCanCacheRead = true;
339 cFYI(1, ("Exclusive Oplock inode %p",
340 newinode));
341 } else if ((oplock & 0xF) == OPLOCK_READ)
342 pCifsInode->clientCanCacheRead = true;
343 }
344 write_unlock(&GlobalSMBSeslock);
345 }
346 }
347 cifs_create_out:
348 kfree(buf);
349 kfree(full_path);
350 FreeXid(xid);
351 return rc;
352 }
353
354 int cifs_mknod(struct inode *inode, struct dentry *direntry, int mode,
355 dev_t device_number)
356 {
357 int rc = -EPERM;
358 int xid;
359 struct cifs_sb_info *cifs_sb;
360 struct cifsTconInfo *pTcon;
361 char *full_path = NULL;
362 struct inode *newinode = NULL;
363
364 if (!old_valid_dev(device_number))
365 return -EINVAL;
366
367 xid = GetXid();
368
369 cifs_sb = CIFS_SB(inode->i_sb);
370 pTcon = cifs_sb->tcon;
371
372 full_path = build_path_from_dentry(direntry);
373 if (full_path == NULL)
374 rc = -ENOMEM;
375 else if (pTcon->unix_ext) {
376 struct cifs_unix_set_info_args args = {
377 .mode = mode & ~current->fs->umask,
378 .ctime = NO_CHANGE_64,
379 .atime = NO_CHANGE_64,
380 .mtime = NO_CHANGE_64,
381 .device = device_number,
382 };
383 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
384 args.uid = (__u64) current_fsuid();
385 args.gid = (__u64) current_fsgid();
386 } else {
387 args.uid = NO_CHANGE_64;
388 args.gid = NO_CHANGE_64;
389 }
390 rc = CIFSSMBUnixSetInfo(xid, pTcon, full_path,
391 &args, cifs_sb->local_nls,
392 cifs_sb->mnt_cifs_flags &
393 CIFS_MOUNT_MAP_SPECIAL_CHR);
394
395 if (!rc) {
396 rc = cifs_get_inode_info_unix(&newinode, full_path,
397 inode->i_sb, xid);
398 if (pTcon->nocase)
399 direntry->d_op = &cifs_ci_dentry_ops;
400 else
401 direntry->d_op = &cifs_dentry_ops;
402 if (rc == 0)
403 d_instantiate(direntry, newinode);
404 }
405 } else {
406 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
407 int oplock = 0;
408 u16 fileHandle;
409 FILE_ALL_INFO *buf;
410
411 cFYI(1, ("sfu compat create special file"));
412
413 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
414 if (buf == NULL) {
415 kfree(full_path);
416 FreeXid(xid);
417 return -ENOMEM;
418 }
419
420 rc = CIFSSMBOpen(xid, pTcon, full_path,
421 FILE_CREATE, /* fail if exists */
422 GENERIC_WRITE /* BB would
423 WRITE_OWNER | WRITE_DAC be better? */,
424 /* Create a file and set the
425 file attribute to SYSTEM */
426 CREATE_NOT_DIR | CREATE_OPTION_SPECIAL,
427 &fileHandle, &oplock, buf,
428 cifs_sb->local_nls,
429 cifs_sb->mnt_cifs_flags &
430 CIFS_MOUNT_MAP_SPECIAL_CHR);
431
432 /* BB FIXME - add handling for backlevel servers
433 which need legacy open and check for all
434 calls to SMBOpen for fallback to SMBLeagcyOpen */
435 if (!rc) {
436 /* BB Do not bother to decode buf since no
437 local inode yet to put timestamps in,
438 but we can reuse it safely */
439 unsigned int bytes_written;
440 struct win_dev *pdev;
441 pdev = (struct win_dev *)buf;
442 if (S_ISCHR(mode)) {
443 memcpy(pdev->type, "IntxCHR", 8);
444 pdev->major =
445 cpu_to_le64(MAJOR(device_number));
446 pdev->minor =
447 cpu_to_le64(MINOR(device_number));
448 rc = CIFSSMBWrite(xid, pTcon,
449 fileHandle,
450 sizeof(struct win_dev),
451 0, &bytes_written, (char *)pdev,
452 NULL, 0);
453 } else if (S_ISBLK(mode)) {
454 memcpy(pdev->type, "IntxBLK", 8);
455 pdev->major =
456 cpu_to_le64(MAJOR(device_number));
457 pdev->minor =
458 cpu_to_le64(MINOR(device_number));
459 rc = CIFSSMBWrite(xid, pTcon,
460 fileHandle,
461 sizeof(struct win_dev),
462 0, &bytes_written, (char *)pdev,
463 NULL, 0);
464 } /* else if(S_ISFIFO */
465 CIFSSMBClose(xid, pTcon, fileHandle);
466 d_drop(direntry);
467 }
468 kfree(buf);
469 /* add code here to set EAs */
470 }
471 }
472
473 kfree(full_path);
474 FreeXid(xid);
475 return rc;
476 }
477
478
479 struct dentry *
480 cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
481 struct nameidata *nd)
482 {
483 int xid;
484 int rc = 0; /* to get around spurious gcc warning, set to zero here */
485 struct cifs_sb_info *cifs_sb;
486 struct cifsTconInfo *pTcon;
487 struct inode *newInode = NULL;
488 char *full_path = NULL;
489
490 xid = GetXid();
491
492 cFYI(1, ("parent inode = 0x%p name is: %s and dentry = 0x%p",
493 parent_dir_inode, direntry->d_name.name, direntry));
494
495 /* check whether path exists */
496
497 cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
498 pTcon = cifs_sb->tcon;
499
500 /*
501 * Don't allow the separator character in a path component.
502 * The VFS will not allow "/", but "\" is allowed by posix.
503 */
504 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) {
505 int i;
506 for (i = 0; i < direntry->d_name.len; i++)
507 if (direntry->d_name.name[i] == '\\') {
508 cFYI(1, ("Invalid file name"));
509 FreeXid(xid);
510 return ERR_PTR(-EINVAL);
511 }
512 }
513
514 /* can not grab the rename sem here since it would
515 deadlock in the cases (beginning of sys_rename itself)
516 in which we already have the sb rename sem */
517 full_path = build_path_from_dentry(direntry);
518 if (full_path == NULL) {
519 FreeXid(xid);
520 return ERR_PTR(-ENOMEM);
521 }
522
523 if (direntry->d_inode != NULL) {
524 cFYI(1, ("non-NULL inode in lookup"));
525 } else {
526 cFYI(1, ("NULL inode in lookup"));
527 }
528 cFYI(1, ("Full path: %s inode = 0x%p", full_path, direntry->d_inode));
529
530 if (pTcon->unix_ext)
531 rc = cifs_get_inode_info_unix(&newInode, full_path,
532 parent_dir_inode->i_sb, xid);
533 else
534 rc = cifs_get_inode_info(&newInode, full_path, NULL,
535 parent_dir_inode->i_sb, xid, NULL);
536
537 if ((rc == 0) && (newInode != NULL)) {
538 if (pTcon->nocase)
539 direntry->d_op = &cifs_ci_dentry_ops;
540 else
541 direntry->d_op = &cifs_dentry_ops;
542 d_add(direntry, newInode);
543
544 /* since paths are not looked up by component - the parent
545 directories are presumed to be good here */
546 renew_parental_timestamps(direntry);
547
548 } else if (rc == -ENOENT) {
549 rc = 0;
550 direntry->d_time = jiffies;
551 if (pTcon->nocase)
552 direntry->d_op = &cifs_ci_dentry_ops;
553 else
554 direntry->d_op = &cifs_dentry_ops;
555 d_add(direntry, NULL);
556 /* if it was once a directory (but how can we tell?) we could do
557 shrink_dcache_parent(direntry); */
558 } else if (rc != -EACCES) {
559 cERROR(1, ("Unexpected lookup error %d", rc));
560 /* We special case check for Access Denied - since that
561 is a common return code */
562 }
563
564 kfree(full_path);
565 FreeXid(xid);
566 return ERR_PTR(rc);
567 }
568
569 static int
570 cifs_d_revalidate(struct dentry *direntry, struct nameidata *nd)
571 {
572 int isValid = 1;
573
574 if (direntry->d_inode) {
575 if (cifs_revalidate(direntry))
576 return 0;
577 } else {
578 cFYI(1, ("neg dentry 0x%p name = %s",
579 direntry, direntry->d_name.name));
580 if (time_after(jiffies, direntry->d_time + HZ) ||
581 !lookupCacheEnabled) {
582 d_drop(direntry);
583 isValid = 0;
584 }
585 }
586
587 return isValid;
588 }
589
590 /* static int cifs_d_delete(struct dentry *direntry)
591 {
592 int rc = 0;
593
594 cFYI(1, ("In cifs d_delete, name = %s", direntry->d_name.name));
595
596 return rc;
597 } */
598
599 struct dentry_operations cifs_dentry_ops = {
600 .d_revalidate = cifs_d_revalidate,
601 /* d_delete: cifs_d_delete, */ /* not needed except for debugging */
602 };
603
604 static int cifs_ci_hash(struct dentry *dentry, struct qstr *q)
605 {
606 struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
607 unsigned long hash;
608 int i;
609
610 hash = init_name_hash();
611 for (i = 0; i < q->len; i++)
612 hash = partial_name_hash(nls_tolower(codepage, q->name[i]),
613 hash);
614 q->hash = end_name_hash(hash);
615
616 return 0;
617 }
618
619 static int cifs_ci_compare(struct dentry *dentry, struct qstr *a,
620 struct qstr *b)
621 {
622 struct nls_table *codepage = CIFS_SB(dentry->d_inode->i_sb)->local_nls;
623
624 if ((a->len == b->len) &&
625 (nls_strnicmp(codepage, a->name, b->name, a->len) == 0)) {
626 /*
627 * To preserve case, don't let an existing negative dentry's
628 * case take precedence. If a is not a negative dentry, this
629 * should have no side effects
630 */
631 memcpy((void *)a->name, b->name, a->len);
632 return 0;
633 }
634 return 1;
635 }
636
637 struct dentry_operations cifs_ci_dentry_ops = {
638 .d_revalidate = cifs_d_revalidate,
639 .d_hash = cifs_ci_hash,
640 .d_compare = cifs_ci_compare,
641 };
This page took 0.043909 seconds and 5 git commands to generate.