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