Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: LGPL-2.1
2/*
3 *
4 * vfs operations that deal with dentries
5 *
6 * Copyright (C) International Business Machines Corp., 2002,2009
7 * Author(s): Steve French (sfrench@us.ibm.com)
8 *
9 */
10#include <linux/fs.h>
11#include <linux/stat.h>
12#include <linux/slab.h>
13#include <linux/namei.h>
14#include <linux/mount.h>
15#include <linux/file.h>
16#include "cifsfs.h"
17#include "cifspdu.h"
18#include "cifsglob.h"
19#include "cifsproto.h"
20#include "cifs_debug.h"
21#include "cifs_fs_sb.h"
22#include "cifs_unicode.h"
23#include "fs_context.h"
24#include "cifs_ioctl.h"
25#include "fscache.h"
26
27static void
28renew_parental_timestamps(struct dentry *direntry)
29{
30 /* BB check if there is a way to get the kernel to do this or if we
31 really need this */
32 do {
33 cifs_set_time(direntry, jiffies);
34 direntry = direntry->d_parent;
35 } while (!IS_ROOT(direntry));
36}
37
38char *
39cifs_build_path_to_root(struct smb3_fs_context *ctx, struct cifs_sb_info *cifs_sb,
40 struct cifs_tcon *tcon, int add_treename)
41{
42 int pplen = ctx->prepath ? strlen(ctx->prepath) + 1 : 0;
43 int dfsplen;
44 char *full_path = NULL;
45
46 /* if no prefix path, simply set path to the root of share to "" */
47 if (pplen == 0) {
48 full_path = kzalloc(1, GFP_KERNEL);
49 return full_path;
50 }
51
52 if (add_treename)
53 dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1);
54 else
55 dfsplen = 0;
56
57 full_path = kmalloc(dfsplen + pplen + 1, GFP_KERNEL);
58 if (full_path == NULL)
59 return full_path;
60
61 if (dfsplen)
62 memcpy(full_path, tcon->treeName, dfsplen);
63 full_path[dfsplen] = CIFS_DIR_SEP(cifs_sb);
64 memcpy(full_path + dfsplen + 1, ctx->prepath, pplen);
65 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
66 return full_path;
67}
68
69/* Note: caller must free return buffer */
70const char *
71build_path_from_dentry(struct dentry *direntry, void *page)
72{
73 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
74 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
75 bool prefix = tcon->Flags & SMB_SHARE_IS_IN_DFS;
76
77 return build_path_from_dentry_optional_prefix(direntry, page,
78 prefix);
79}
80
81char *
82build_path_from_dentry_optional_prefix(struct dentry *direntry, void *page,
83 bool prefix)
84{
85 int dfsplen;
86 int pplen = 0;
87 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
88 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
89 char dirsep = CIFS_DIR_SEP(cifs_sb);
90 char *s;
91
92 if (unlikely(!page))
93 return ERR_PTR(-ENOMEM);
94
95 if (prefix)
96 dfsplen = strnlen(tcon->treeName, MAX_TREE_SIZE + 1);
97 else
98 dfsplen = 0;
99
100 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
101 pplen = cifs_sb->prepath ? strlen(cifs_sb->prepath) + 1 : 0;
102
103 s = dentry_path_raw(direntry, page, PATH_MAX);
104 if (IS_ERR(s))
105 return s;
106 if (!s[1]) // for root we want "", not "/"
107 s++;
108 if (s < (char *)page + pplen + dfsplen)
109 return ERR_PTR(-ENAMETOOLONG);
110 if (pplen) {
111 cifs_dbg(FYI, "using cifs_sb prepath <%s>\n", cifs_sb->prepath);
112 s -= pplen;
113 memcpy(s + 1, cifs_sb->prepath, pplen - 1);
114 *s = '/';
115 }
116 if (dirsep != '/') {
117 /* BB test paths to Windows with '/' in the midst of prepath */
118 char *p;
119
120 for (p = s; *p; p++)
121 if (*p == '/')
122 *p = dirsep;
123 }
124 if (dfsplen) {
125 s -= dfsplen;
126 memcpy(s, tcon->treeName, dfsplen);
127 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) {
128 int i;
129 for (i = 0; i < dfsplen; i++) {
130 if (s[i] == '\\')
131 s[i] = '/';
132 }
133 }
134 }
135 return s;
136}
137
138/*
139 * Don't allow path components longer than the server max.
140 * Don't allow the separator character in a path component.
141 * The VFS will not allow "/", but "\" is allowed by posix.
142 */
143static int
144check_name(struct dentry *direntry, struct cifs_tcon *tcon)
145{
146 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
147 int i;
148
149 if (unlikely(tcon->fsAttrInfo.MaxPathNameComponentLength &&
150 direntry->d_name.len >
151 le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength)))
152 return -ENAMETOOLONG;
153
154 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS)) {
155 for (i = 0; i < direntry->d_name.len; i++) {
156 if (direntry->d_name.name[i] == '\\') {
157 cifs_dbg(FYI, "Invalid file name\n");
158 return -EINVAL;
159 }
160 }
161 }
162 return 0;
163}
164
165
166/* Inode operations in similar order to how they appear in Linux file fs.h */
167
168static int
169cifs_do_create(struct inode *inode, struct dentry *direntry, unsigned int xid,
170 struct tcon_link *tlink, unsigned oflags, umode_t mode,
171 __u32 *oplock, struct cifs_fid *fid)
172{
173 int rc = -ENOENT;
174 int create_options = CREATE_NOT_DIR;
175 int desired_access;
176 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
177 struct cifs_tcon *tcon = tlink_tcon(tlink);
178 const char *full_path;
179 void *page = alloc_dentry_path();
180 FILE_ALL_INFO *buf = NULL;
181 struct inode *newinode = NULL;
182 int disposition;
183 struct TCP_Server_Info *server = tcon->ses->server;
184 struct cifs_open_parms oparms;
185
186 *oplock = 0;
187 if (tcon->ses->server->oplocks)
188 *oplock = REQ_OPLOCK;
189
190 full_path = build_path_from_dentry(direntry, page);
191 if (IS_ERR(full_path)) {
192 free_dentry_path(page);
193 return PTR_ERR(full_path);
194 }
195
196#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
197 if (tcon->unix_ext && cap_unix(tcon->ses) && !tcon->broken_posix_open &&
198 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
199 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
200 rc = cifs_posix_open(full_path, &newinode, inode->i_sb, mode,
201 oflags, oplock, &fid->netfid, xid);
202 switch (rc) {
203 case 0:
204 if (newinode == NULL) {
205 /* query inode info */
206 goto cifs_create_get_file_info;
207 }
208
209 if (S_ISDIR(newinode->i_mode)) {
210 CIFSSMBClose(xid, tcon, fid->netfid);
211 iput(newinode);
212 rc = -EISDIR;
213 goto out;
214 }
215
216 if (!S_ISREG(newinode->i_mode)) {
217 /*
218 * The server may allow us to open things like
219 * FIFOs, but the client isn't set up to deal
220 * with that. If it's not a regular file, just
221 * close it and proceed as if it were a normal
222 * lookup.
223 */
224 CIFSSMBClose(xid, tcon, fid->netfid);
225 goto cifs_create_get_file_info;
226 }
227 /* success, no need to query */
228 goto cifs_create_set_dentry;
229
230 case -ENOENT:
231 goto cifs_create_get_file_info;
232
233 case -EIO:
234 case -EINVAL:
235 /*
236 * EIO could indicate that (posix open) operation is not
237 * supported, despite what server claimed in capability
238 * negotiation.
239 *
240 * POSIX open in samba versions 3.3.1 and earlier could
241 * incorrectly fail with invalid parameter.
242 */
243 tcon->broken_posix_open = true;
244 break;
245
246 case -EREMOTE:
247 case -EOPNOTSUPP:
248 /*
249 * EREMOTE indicates DFS junction, which is not handled
250 * in posix open. If either that or op not supported
251 * returned, follow the normal lookup.
252 */
253 break;
254
255 default:
256 goto out;
257 }
258 /*
259 * fallthrough to retry, using older open call, this is case
260 * where server does not support this SMB level, and falsely
261 * claims capability (also get here for DFS case which should be
262 * rare for path not covered on files)
263 */
264 }
265#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
266
267 desired_access = 0;
268 if (OPEN_FMODE(oflags) & FMODE_READ)
269 desired_access |= GENERIC_READ; /* is this too little? */
270 if (OPEN_FMODE(oflags) & FMODE_WRITE)
271 desired_access |= GENERIC_WRITE;
272
273 disposition = FILE_OVERWRITE_IF;
274 if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
275 disposition = FILE_CREATE;
276 else if ((oflags & (O_CREAT | O_TRUNC)) == (O_CREAT | O_TRUNC))
277 disposition = FILE_OVERWRITE_IF;
278 else if ((oflags & O_CREAT) == O_CREAT)
279 disposition = FILE_OPEN_IF;
280 else
281 cifs_dbg(FYI, "Create flag not set in create function\n");
282
283 /*
284 * BB add processing to set equivalent of mode - e.g. via CreateX with
285 * ACLs
286 */
287
288 if (!server->ops->open) {
289 rc = -ENOSYS;
290 goto out;
291 }
292
293 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
294 if (buf == NULL) {
295 rc = -ENOMEM;
296 goto out;
297 }
298
299 /*
300 * if we're not using unix extensions, see if we need to set
301 * ATTR_READONLY on the create call
302 */
303 if (!tcon->unix_ext && (mode & S_IWUGO) == 0)
304 create_options |= CREATE_OPTION_READONLY;
305
306 oparms.tcon = tcon;
307 oparms.cifs_sb = cifs_sb;
308 oparms.desired_access = desired_access;
309 oparms.create_options = cifs_create_options(cifs_sb, create_options);
310 oparms.disposition = disposition;
311 oparms.path = full_path;
312 oparms.fid = fid;
313 oparms.reconnect = false;
314 oparms.mode = mode;
315 rc = server->ops->open(xid, &oparms, oplock, buf);
316 if (rc) {
317 cifs_dbg(FYI, "cifs_create returned 0x%x\n", rc);
318 goto out;
319 }
320
321#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
322 /*
323 * If Open reported that we actually created a file then we now have to
324 * set the mode if possible.
325 */
326 if ((tcon->unix_ext) && (*oplock & CIFS_CREATE_ACTION)) {
327 struct cifs_unix_set_info_args args = {
328 .mode = mode,
329 .ctime = NO_CHANGE_64,
330 .atime = NO_CHANGE_64,
331 .mtime = NO_CHANGE_64,
332 .device = 0,
333 };
334
335 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
336 args.uid = current_fsuid();
337 if (inode->i_mode & S_ISGID)
338 args.gid = inode->i_gid;
339 else
340 args.gid = current_fsgid();
341 } else {
342 args.uid = INVALID_UID; /* no change */
343 args.gid = INVALID_GID; /* no change */
344 }
345 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid->netfid,
346 current->tgid);
347 } else {
348 /*
349 * BB implement mode setting via Windows security
350 * descriptors e.g.
351 */
352 /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/
353
354 /* Could set r/o dos attribute if mode & 0222 == 0 */
355 }
356
357cifs_create_get_file_info:
358 /* server might mask mode so we have to query for it */
359 if (tcon->unix_ext)
360 rc = cifs_get_inode_info_unix(&newinode, full_path, inode->i_sb,
361 xid);
362 else {
363#else
364 {
365#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
366 /* TODO: Add support for calling POSIX query info here, but passing in fid */
367 rc = cifs_get_inode_info(&newinode, full_path, buf, inode->i_sb,
368 xid, fid);
369 if (newinode) {
370 if (server->ops->set_lease_key)
371 server->ops->set_lease_key(newinode, fid);
372 if ((*oplock & CIFS_CREATE_ACTION) && S_ISREG(newinode->i_mode)) {
373 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
374 newinode->i_mode = mode;
375 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
376 newinode->i_uid = current_fsuid();
377 if (inode->i_mode & S_ISGID)
378 newinode->i_gid = inode->i_gid;
379 else
380 newinode->i_gid = current_fsgid();
381 }
382 }
383 }
384 }
385
386#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
387cifs_create_set_dentry:
388#endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
389 if (rc != 0) {
390 cifs_dbg(FYI, "Create worked, get_inode_info failed rc = %d\n",
391 rc);
392 goto out_err;
393 }
394
395 if (newinode)
396 if (S_ISDIR(newinode->i_mode)) {
397 rc = -EISDIR;
398 goto out_err;
399 }
400
401 d_drop(direntry);
402 d_add(direntry, newinode);
403
404out:
405 kfree(buf);
406 free_dentry_path(page);
407 return rc;
408
409out_err:
410 if (server->ops->close)
411 server->ops->close(xid, tcon, fid);
412 if (newinode)
413 iput(newinode);
414 goto out;
415}
416
417int
418cifs_atomic_open(struct inode *inode, struct dentry *direntry,
419 struct file *file, unsigned oflags, umode_t mode)
420{
421 int rc;
422 unsigned int xid;
423 struct tcon_link *tlink;
424 struct cifs_tcon *tcon;
425 struct TCP_Server_Info *server;
426 struct cifs_fid fid;
427 struct cifs_pending_open open;
428 __u32 oplock;
429 struct cifsFileInfo *file_info;
430
431 if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb))))
432 return -EIO;
433
434 /*
435 * Posix open is only called (at lookup time) for file create now. For
436 * opens (rather than creates), because we do not know if it is a file
437 * or directory yet, and current Samba no longer allows us to do posix
438 * open on dirs, we could end up wasting an open call on what turns out
439 * to be a dir. For file opens, we wait to call posix open till
440 * cifs_open. It could be added to atomic_open in the future but the
441 * performance tradeoff of the extra network request when EISDIR or
442 * EACCES is returned would have to be weighed against the 50% reduction
443 * in network traffic in the other paths.
444 */
445 if (!(oflags & O_CREAT)) {
446 struct dentry *res;
447
448 /*
449 * Check for hashed negative dentry. We have already revalidated
450 * the dentry and it is fine. No need to perform another lookup.
451 */
452 if (!d_in_lookup(direntry))
453 return -ENOENT;
454
455 res = cifs_lookup(inode, direntry, 0);
456 if (IS_ERR(res))
457 return PTR_ERR(res);
458
459 return finish_no_open(file, res);
460 }
461
462 xid = get_xid();
463
464 cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
465 inode, direntry, direntry);
466
467 tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb));
468 if (IS_ERR(tlink)) {
469 rc = PTR_ERR(tlink);
470 goto out_free_xid;
471 }
472
473 tcon = tlink_tcon(tlink);
474
475 rc = check_name(direntry, tcon);
476 if (rc)
477 goto out;
478
479 server = tcon->ses->server;
480
481 if (server->ops->new_lease_key)
482 server->ops->new_lease_key(&fid);
483
484 cifs_add_pending_open(&fid, tlink, &open);
485
486 rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode,
487 &oplock, &fid);
488
489 if (rc) {
490 cifs_del_pending_open(&open);
491 goto out;
492 }
493
494 if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
495 file->f_mode |= FMODE_CREATED;
496
497 rc = finish_open(file, direntry, generic_file_open);
498 if (rc) {
499 if (server->ops->close)
500 server->ops->close(xid, tcon, &fid);
501 cifs_del_pending_open(&open);
502 goto out;
503 }
504
505 if (file->f_flags & O_DIRECT &&
506 CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
507 if (CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
508 file->f_op = &cifs_file_direct_nobrl_ops;
509 else
510 file->f_op = &cifs_file_direct_ops;
511 }
512
513 file_info = cifs_new_fileinfo(&fid, file, tlink, oplock);
514 if (file_info == NULL) {
515 if (server->ops->close)
516 server->ops->close(xid, tcon, &fid);
517 cifs_del_pending_open(&open);
518 rc = -ENOMEM;
519 goto out;
520 }
521
522 fscache_use_cookie(cifs_inode_cookie(file_inode(file)),
523 file->f_mode & FMODE_WRITE);
524
525out:
526 cifs_put_tlink(tlink);
527out_free_xid:
528 free_xid(xid);
529 return rc;
530}
531
532int cifs_create(struct user_namespace *mnt_userns, struct inode *inode,
533 struct dentry *direntry, umode_t mode, bool excl)
534{
535 int rc;
536 unsigned int xid = get_xid();
537 /*
538 * BB below access is probably too much for mknod to request
539 * but we have to do query and setpathinfo so requesting
540 * less could fail (unless we want to request getatr and setatr
541 * permissions (only). At least for POSIX we do not have to
542 * request so much.
543 */
544 unsigned oflags = O_EXCL | O_CREAT | O_RDWR;
545 struct tcon_link *tlink;
546 struct cifs_tcon *tcon;
547 struct TCP_Server_Info *server;
548 struct cifs_fid fid;
549 __u32 oplock;
550
551 cifs_dbg(FYI, "cifs_create parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
552 inode, direntry, direntry);
553
554 if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb))))
555 return -EIO;
556
557 tlink = cifs_sb_tlink(CIFS_SB(inode->i_sb));
558 rc = PTR_ERR(tlink);
559 if (IS_ERR(tlink))
560 goto out_free_xid;
561
562 tcon = tlink_tcon(tlink);
563 server = tcon->ses->server;
564
565 if (server->ops->new_lease_key)
566 server->ops->new_lease_key(&fid);
567
568 rc = cifs_do_create(inode, direntry, xid, tlink, oflags, mode,
569 &oplock, &fid);
570 if (!rc && server->ops->close)
571 server->ops->close(xid, tcon, &fid);
572
573 cifs_put_tlink(tlink);
574out_free_xid:
575 free_xid(xid);
576 return rc;
577}
578
579int cifs_mknod(struct user_namespace *mnt_userns, struct inode *inode,
580 struct dentry *direntry, umode_t mode, dev_t device_number)
581{
582 int rc = -EPERM;
583 unsigned int xid;
584 struct cifs_sb_info *cifs_sb;
585 struct tcon_link *tlink;
586 struct cifs_tcon *tcon;
587 const char *full_path;
588 void *page;
589
590 if (!old_valid_dev(device_number))
591 return -EINVAL;
592
593 cifs_sb = CIFS_SB(inode->i_sb);
594 if (unlikely(cifs_forced_shutdown(cifs_sb)))
595 return -EIO;
596
597 tlink = cifs_sb_tlink(cifs_sb);
598 if (IS_ERR(tlink))
599 return PTR_ERR(tlink);
600
601 page = alloc_dentry_path();
602 tcon = tlink_tcon(tlink);
603 xid = get_xid();
604
605 full_path = build_path_from_dentry(direntry, page);
606 if (IS_ERR(full_path)) {
607 rc = PTR_ERR(full_path);
608 goto mknod_out;
609 }
610
611 rc = tcon->ses->server->ops->make_node(xid, inode, direntry, tcon,
612 full_path, mode,
613 device_number);
614
615mknod_out:
616 free_dentry_path(page);
617 free_xid(xid);
618 cifs_put_tlink(tlink);
619 return rc;
620}
621
622struct dentry *
623cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
624 unsigned int flags)
625{
626 unsigned int xid;
627 int rc = 0; /* to get around spurious gcc warning, set to zero here */
628 struct cifs_sb_info *cifs_sb;
629 struct tcon_link *tlink;
630 struct cifs_tcon *pTcon;
631 struct inode *newInode = NULL;
632 const char *full_path;
633 void *page;
634 int retry_count = 0;
635
636 xid = get_xid();
637
638 cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
639 parent_dir_inode, direntry, direntry);
640
641 /* check whether path exists */
642
643 cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
644 tlink = cifs_sb_tlink(cifs_sb);
645 if (IS_ERR(tlink)) {
646 free_xid(xid);
647 return ERR_CAST(tlink);
648 }
649 pTcon = tlink_tcon(tlink);
650
651 rc = check_name(direntry, pTcon);
652 if (unlikely(rc)) {
653 cifs_put_tlink(tlink);
654 free_xid(xid);
655 return ERR_PTR(rc);
656 }
657
658 /* can not grab the rename sem here since it would
659 deadlock in the cases (beginning of sys_rename itself)
660 in which we already have the sb rename sem */
661 page = alloc_dentry_path();
662 full_path = build_path_from_dentry(direntry, page);
663 if (IS_ERR(full_path)) {
664 cifs_put_tlink(tlink);
665 free_xid(xid);
666 free_dentry_path(page);
667 return ERR_CAST(full_path);
668 }
669
670 if (d_really_is_positive(direntry)) {
671 cifs_dbg(FYI, "non-NULL inode in lookup\n");
672 } else {
673 cifs_dbg(FYI, "NULL inode in lookup\n");
674 }
675 cifs_dbg(FYI, "Full path: %s inode = 0x%p\n",
676 full_path, d_inode(direntry));
677
678again:
679 if (pTcon->posix_extensions)
680 rc = smb311_posix_get_inode_info(&newInode, full_path, parent_dir_inode->i_sb, xid);
681 else if (pTcon->unix_ext) {
682 rc = cifs_get_inode_info_unix(&newInode, full_path,
683 parent_dir_inode->i_sb, xid);
684 } else {
685 rc = cifs_get_inode_info(&newInode, full_path, NULL,
686 parent_dir_inode->i_sb, xid, NULL);
687 }
688
689 if (rc == 0) {
690 /* since paths are not looked up by component - the parent
691 directories are presumed to be good here */
692 renew_parental_timestamps(direntry);
693 } else if (rc == -EAGAIN && retry_count++ < 10) {
694 goto again;
695 } else if (rc == -ENOENT) {
696 cifs_set_time(direntry, jiffies);
697 newInode = NULL;
698 } else {
699 if (rc != -EACCES) {
700 cifs_dbg(FYI, "Unexpected lookup error %d\n", rc);
701 /* We special case check for Access Denied - since that
702 is a common return code */
703 }
704 newInode = ERR_PTR(rc);
705 }
706 free_dentry_path(page);
707 cifs_put_tlink(tlink);
708 free_xid(xid);
709 return d_splice_alias(newInode, direntry);
710}
711
712static int
713cifs_d_revalidate(struct dentry *direntry, unsigned int flags)
714{
715 struct inode *inode;
716 int rc;
717
718 if (flags & LOOKUP_RCU)
719 return -ECHILD;
720
721 if (d_really_is_positive(direntry)) {
722 inode = d_inode(direntry);
723 if ((flags & LOOKUP_REVAL) && !CIFS_CACHE_READ(CIFS_I(inode)))
724 CIFS_I(inode)->time = 0; /* force reval */
725
726 rc = cifs_revalidate_dentry(direntry);
727 if (rc) {
728 cifs_dbg(FYI, "cifs_revalidate_dentry failed with rc=%d", rc);
729 switch (rc) {
730 case -ENOENT:
731 case -ESTALE:
732 /*
733 * Those errors mean the dentry is invalid
734 * (file was deleted or recreated)
735 */
736 return 0;
737 default:
738 /*
739 * Otherwise some unexpected error happened
740 * report it as-is to VFS layer
741 */
742 return rc;
743 }
744 }
745 else {
746 /*
747 * If the inode wasn't known to be a dfs entry when
748 * the dentry was instantiated, such as when created
749 * via ->readdir(), it needs to be set now since the
750 * attributes will have been updated by
751 * cifs_revalidate_dentry().
752 */
753 if (IS_AUTOMOUNT(inode) &&
754 !(direntry->d_flags & DCACHE_NEED_AUTOMOUNT)) {
755 spin_lock(&direntry->d_lock);
756 direntry->d_flags |= DCACHE_NEED_AUTOMOUNT;
757 spin_unlock(&direntry->d_lock);
758 }
759
760 return 1;
761 }
762 }
763
764 /*
765 * This may be nfsd (or something), anyway, we can't see the
766 * intent of this. So, since this can be for creation, drop it.
767 */
768 if (!flags)
769 return 0;
770
771 /*
772 * Drop the negative dentry, in order to make sure to use the
773 * case sensitive name which is specified by user if this is
774 * for creation.
775 */
776 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
777 return 0;
778
779 if (time_after(jiffies, cifs_get_time(direntry) + HZ) || !lookupCacheEnabled)
780 return 0;
781
782 return 1;
783}
784
785/* static int cifs_d_delete(struct dentry *direntry)
786{
787 int rc = 0;
788
789 cifs_dbg(FYI, "In cifs d_delete, name = %pd\n", direntry);
790
791 return rc;
792} */
793
794const struct dentry_operations cifs_dentry_ops = {
795 .d_revalidate = cifs_d_revalidate,
796 .d_automount = cifs_dfs_d_automount,
797/* d_delete: cifs_d_delete, */ /* not needed except for debugging */
798};
799
800static int cifs_ci_hash(const struct dentry *dentry, struct qstr *q)
801{
802 struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
803 unsigned long hash;
804 wchar_t c;
805 int i, charlen;
806
807 hash = init_name_hash(dentry);
808 for (i = 0; i < q->len; i += charlen) {
809 charlen = codepage->char2uni(&q->name[i], q->len - i, &c);
810 /* error out if we can't convert the character */
811 if (unlikely(charlen < 0))
812 return charlen;
813 hash = partial_name_hash(cifs_toupper(c), hash);
814 }
815 q->hash = end_name_hash(hash);
816
817 return 0;
818}
819
820static int cifs_ci_compare(const struct dentry *dentry,
821 unsigned int len, const char *str, const struct qstr *name)
822{
823 struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
824 wchar_t c1, c2;
825 int i, l1, l2;
826
827 /*
828 * We make the assumption here that uppercase characters in the local
829 * codepage are always the same length as their lowercase counterparts.
830 *
831 * If that's ever not the case, then this will fail to match it.
832 */
833 if (name->len != len)
834 return 1;
835
836 for (i = 0; i < len; i += l1) {
837 /* Convert characters in both strings to UTF-16. */
838 l1 = codepage->char2uni(&str[i], len - i, &c1);
839 l2 = codepage->char2uni(&name->name[i], name->len - i, &c2);
840
841 /*
842 * If we can't convert either character, just declare it to
843 * be 1 byte long and compare the original byte.
844 */
845 if (unlikely(l1 < 0 && l2 < 0)) {
846 if (str[i] != name->name[i])
847 return 1;
848 l1 = 1;
849 continue;
850 }
851
852 /*
853 * Here, we again ass|u|me that upper/lowercase versions of
854 * a character are the same length in the local NLS.
855 */
856 if (l1 != l2)
857 return 1;
858
859 /* Now compare uppercase versions of these characters */
860 if (cifs_toupper(c1) != cifs_toupper(c2))
861 return 1;
862 }
863
864 return 0;
865}
866
867const struct dentry_operations cifs_ci_dentry_ops = {
868 .d_revalidate = cifs_d_revalidate,
869 .d_hash = cifs_ci_hash,
870 .d_compare = cifs_ci_compare,
871 .d_automount = cifs_dfs_d_automount,
872};