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 strreplace(s, '/', dirsep);
119 }
120 if (dfsplen) {
121 s -= dfsplen;
122 memcpy(s, tree, dfsplen);
123 if (sbflags & CIFS_MOUNT_POSIX_PATHS) {
124 int i;
125 for (i = 0; i < dfsplen; i++) {
126 if (s[i] == '\\')
127 s[i] = '/';
128 }
129 }
130 }
131 return s;
132 }
133
build_path_from_dentry_optional_prefix(struct dentry * direntry,void * page,bool prefix)134 char *build_path_from_dentry_optional_prefix(struct dentry *direntry, void *page,
135 bool prefix)
136 {
137 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
138 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
139
140 return __build_path_from_dentry_optional_prefix(direntry, page, tcon->tree_name,
141 MAX_TREE_SIZE, prefix);
142 }
143
144 /*
145 * Don't allow path components longer than the server max.
146 * Don't allow the separator character in a path component.
147 * The VFS will not allow "/", but "\" is allowed by posix.
148 */
149 static int
check_name(struct dentry * direntry,struct cifs_tcon * tcon)150 check_name(struct dentry *direntry, struct cifs_tcon *tcon)
151 {
152 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry);
153 int i;
154
155 if (unlikely(tcon->fsAttrInfo.MaxPathNameComponentLength &&
156 direntry->d_name.len >
157 le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength)))
158 return -ENAMETOOLONG;
159
160 if (!(cifs_sb_flags(cifs_sb) & CIFS_MOUNT_POSIX_PATHS)) {
161 for (i = 0; i < direntry->d_name.len; i++) {
162 if (direntry->d_name.name[i] == '\\') {
163 cifs_dbg(FYI, "Invalid file name\n");
164 return -EINVAL;
165 }
166 }
167 }
168 return 0;
169 }
170
alloc_parent_path(struct dentry * dentry,size_t namelen)171 static char *alloc_parent_path(struct dentry *dentry, size_t namelen)
172 {
173 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry);
174 void *page = alloc_dentry_path();
175 const char *path;
176 size_t size;
177 char *npath;
178
179 path = build_path_from_dentry(dentry->d_parent, page);
180 if (IS_ERR(path)) {
181 npath = ERR_CAST(path);
182 goto out;
183 }
184
185 size = strlen(path) + namelen + 2;
186 npath = kmalloc(size, GFP_KERNEL);
187 if (!npath)
188 npath = ERR_PTR(-ENOMEM);
189 else
190 scnprintf(npath, size, "%s%c", path, CIFS_DIR_SEP(cifs_sb));
191 out:
192 free_dentry_path(page);
193 return npath;
194 }
195
196 /* Inode operations in similar order to how they appear in Linux file fs.h */
__cifs_do_create(struct inode * dir,struct dentry * direntry,const char * full_path,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,struct inode ** inode,bool * opened)197 static int __cifs_do_create(struct inode *dir, struct dentry *direntry,
198 const char *full_path, unsigned int xid,
199 struct tcon_link *tlink, unsigned int oflags,
200 umode_t mode, __u32 *oplock, struct cifs_fid *fid,
201 struct cifs_open_info_data *buf,
202 struct inode **inode, bool *opened)
203 {
204 int rc = -ENOENT;
205 int create_options = CREATE_NOT_DIR;
206 int desired_access;
207 struct cifs_sb_info *cifs_sb = CIFS_SB(dir);
208 struct cifs_tcon *tcon = tlink_tcon(tlink);
209 struct inode *newinode = NULL;
210 unsigned int sbflags = cifs_sb_flags(cifs_sb);
211 int disposition;
212 struct TCP_Server_Info *server = tcon->ses->server;
213 struct cifs_open_parms oparms;
214 struct cached_fid *parent_cfid = NULL;
215 int rdwr_for_fscache = 0;
216 __le32 lease_flags = 0;
217
218 *inode = NULL;
219 *opened = false;
220 *oplock = 0;
221 if (tcon->ses->server->oplocks)
222 *oplock = REQ_OPLOCK;
223
224 /* If we're caching, we need to be able to fill in around partial writes. */
225 if (cifs_fscache_enabled(dir) && (oflags & O_ACCMODE) == O_WRONLY)
226 rdwr_for_fscache = 1;
227
228 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
229 if (tcon->unix_ext && cap_unix(tcon->ses) && !tcon->broken_posix_open &&
230 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
231 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
232 rc = cifs_posix_open(full_path, &newinode, dir->i_sb, mode,
233 oflags, oplock, &fid->netfid, xid);
234 switch (rc) {
235 case 0:
236 *opened = true;
237 if (newinode == NULL) {
238 /* query inode info */
239 goto cifs_create_get_file_info;
240 }
241
242 if ((oflags & __O_REGULAR) && !S_ISREG(newinode->i_mode)) {
243 CIFSSMBClose(xid, tcon, fid->netfid);
244 iput(newinode);
245 return -EFTYPE;
246 }
247
248 if (S_ISDIR(newinode->i_mode)) {
249 CIFSSMBClose(xid, tcon, fid->netfid);
250 iput(newinode);
251 return -EISDIR;
252 }
253
254 if (!S_ISREG(newinode->i_mode)) {
255 /*
256 * The server may allow us to open things like
257 * FIFOs, but the client isn't set up to deal
258 * with that. Keep the handle until the caller
259 * can finish the lookup.
260 */
261 goto cifs_create_get_file_info;
262 }
263 /* success, no need to query */
264 goto cifs_create_set_dentry;
265
266 case -ENOENT:
267 goto cifs_create_get_file_info;
268
269 case -EIO:
270 case -EINVAL:
271 /*
272 * EIO could indicate that (posix open) operation is not
273 * supported, despite what server claimed in capability
274 * negotiation.
275 *
276 * POSIX open in samba versions 3.3.1 and earlier could
277 * incorrectly fail with invalid parameter.
278 */
279 tcon->broken_posix_open = true;
280 break;
281
282 case -EREMOTE:
283 case -EOPNOTSUPP:
284 /*
285 * EREMOTE indicates DFS junction, which is not handled
286 * in posix open. If either that or op not supported
287 * returned, follow the normal lookup.
288 */
289 break;
290
291 default:
292 return rc;
293 }
294 /*
295 * fallthrough to retry, using older open call, this is case
296 * where server does not support this SMB level, and falsely
297 * claims capability (also get here for DFS case which should be
298 * rare for path not covered on files)
299 */
300 }
301 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
302
303 desired_access = 0;
304 if (OPEN_FMODE(oflags) & FMODE_READ)
305 desired_access |= GENERIC_READ; /* is this too little? */
306 if (OPEN_FMODE(oflags) & FMODE_WRITE)
307 desired_access |= GENERIC_WRITE;
308 if (rdwr_for_fscache == 1)
309 desired_access |= GENERIC_READ;
310 if (oflags & O_TMPFILE)
311 desired_access |= DELETE;
312
313 disposition = FILE_OVERWRITE_IF;
314 if (oflags & O_CREAT) {
315 if (oflags & O_EXCL)
316 disposition = FILE_CREATE;
317 else if (oflags & O_TRUNC)
318 disposition = FILE_OVERWRITE_IF;
319 else
320 disposition = FILE_OPEN_IF;
321 } else if (oflags & O_TMPFILE) {
322 disposition = FILE_CREATE;
323 } else {
324 cifs_dbg(FYI, "Create flag not set in create function\n");
325 }
326
327 /*
328 * BB add processing to set equivalent of mode - e.g. via CreateX with
329 * ACLs
330 */
331
332 if (!server->ops->open)
333 return -EOPNOTSUPP;
334
335 create_options |= cifs_open_create_options(oflags, create_options);
336 /*
337 * if we're not using unix extensions, see if we need to set
338 * ATTR_READONLY on the create call
339 */
340 if (!tcon->unix_ext && (mode & S_IWUGO) == 0)
341 create_options |= CREATE_OPTION_READONLY;
342
343
344 retry_open:
345 if (tcon->cfids && direntry->d_parent && server->dialect >= SMB30_PROT_ID) {
346 parent_cfid = NULL;
347 spin_lock(&tcon->cfids->cfid_list_lock);
348 list_for_each_entry(parent_cfid, &tcon->cfids->entries, entry) {
349 if (parent_cfid->dentry == direntry->d_parent) {
350 cifs_dbg(FYI, "found a parent cached file handle\n");
351 if (is_valid_cached_dir(parent_cfid)) {
352 lease_flags
353 |= SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE;
354 memcpy(fid->parent_lease_key,
355 parent_cfid->fid.lease_key,
356 SMB2_LEASE_KEY_SIZE);
357 parent_cfid->dirents.is_valid = false;
358 parent_cfid->dirents.is_failed = true;
359 }
360 break;
361 }
362 }
363 spin_unlock(&tcon->cfids->cfid_list_lock);
364 }
365
366 oparms = (struct cifs_open_parms) {
367 .tcon = tcon,
368 .cifs_sb = cifs_sb,
369 .desired_access = desired_access,
370 .create_options = cifs_create_options(cifs_sb, create_options),
371 .disposition = disposition,
372 .path = full_path,
373 .fid = fid,
374 .lease_flags = lease_flags,
375 .mode = mode,
376 };
377 rc = server->ops->open(xid, &oparms, oplock, buf);
378 if (rc) {
379 cifs_dbg(FYI, "cifs_create returned 0x%x\n", rc);
380 if (rc == -EACCES && rdwr_for_fscache == 1) {
381 desired_access &= ~GENERIC_READ;
382 rdwr_for_fscache = 2;
383 goto retry_open;
384 }
385 return rc;
386 }
387 *opened = true;
388 if (rdwr_for_fscache == 2)
389 cifs_invalidate_cache(dir, FSCACHE_INVAL_DIO_WRITE);
390
391 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
392 /*
393 * If Open reported that we actually created a file then we now have to
394 * set the mode if possible.
395 */
396 if ((tcon->unix_ext) && (*oplock & CIFS_CREATE_ACTION)) {
397 struct cifs_unix_set_info_args args = {
398 .mode = mode,
399 .ctime = NO_CHANGE_64,
400 .atime = NO_CHANGE_64,
401 .mtime = NO_CHANGE_64,
402 .device = 0,
403 };
404
405 if (sbflags & CIFS_MOUNT_SET_UID) {
406 args.uid = current_fsuid();
407 if (dir->i_mode & S_ISGID)
408 args.gid = dir->i_gid;
409 else
410 args.gid = current_fsgid();
411 } else {
412 args.uid = INVALID_UID; /* no change */
413 args.gid = INVALID_GID; /* no change */
414 }
415 CIFSSMBUnixSetFileInfo(xid, tcon, &args, fid->netfid,
416 current->tgid);
417 } else {
418 /*
419 * BB implement mode setting via Windows security
420 * descriptors e.g.
421 */
422 /* CIFSSMBWinSetPerms(xid,tcon,path,mode,-1,-1,nls);*/
423
424 /* Could set r/o dos attribute if mode & 0222 == 0 */
425 }
426
427 cifs_create_get_file_info:
428 /* server might mask mode so we have to query for it */
429 if (tcon->unix_ext)
430 rc = cifs_get_inode_info_unix(&newinode, full_path, dir->i_sb,
431 xid);
432 else {
433 #else
434 {
435 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
436 /* TODO: Add support for calling POSIX query info here, but passing in fid */
437 rc = cifs_get_inode_info(&newinode, full_path, buf, dir->i_sb, xid, fid);
438 if (newinode) {
439 if (server->ops->set_lease_key)
440 server->ops->set_lease_key(newinode, fid);
441 if ((*oplock & CIFS_CREATE_ACTION) && S_ISREG(newinode->i_mode)) {
442 if (sbflags & CIFS_MOUNT_DYNPERM)
443 newinode->i_mode = mode;
444 if (sbflags & CIFS_MOUNT_SET_UID) {
445 newinode->i_uid = current_fsuid();
446 if (dir->i_mode & S_ISGID)
447 newinode->i_gid = dir->i_gid;
448 else
449 newinode->i_gid = current_fsgid();
450 }
451 }
452 }
453 }
454
455 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
456 cifs_create_set_dentry:
457 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
458 if (rc != 0) {
459 cifs_dbg(FYI, "Create worked, get_inode_info failed rc = %d\n",
460 rc);
461 goto out_err;
462 }
463
464 if (newinode) {
465 if ((oflags & __O_REGULAR) && !S_ISREG(newinode->i_mode)) {
466 rc = -EFTYPE;
467 goto out_err;
468 }
469 if (S_ISDIR(newinode->i_mode)) {
470 rc = -EISDIR;
471 goto out_err;
472 }
473 }
474
475 *inode = newinode;
476 return rc;
477
478 out_err:
479 if (*opened && server->ops->close)
480 server->ops->close(xid, tcon, fid);
481 if (newinode)
482 iput(newinode);
483 return rc;
484 }
485
486 static int cifs_do_create(struct inode *dir, struct dentry *direntry,
487 unsigned int xid, struct tcon_link *tlink,
488 unsigned int oflags, umode_t mode,
489 __u32 *oplock, struct cifs_fid *fid,
490 struct cifs_open_info_data *buf,
491 struct inode **inode, bool *opened)
492 {
493 void *page = alloc_dentry_path();
494 const char *full_path;
495 int rc;
496
497 full_path = build_path_from_dentry(direntry, page);
498 if (IS_ERR(full_path)) {
499 rc = PTR_ERR(full_path);
500 *opened = false;
501 } else {
502 rc = __cifs_do_create(dir, direntry, full_path, xid,
503 tlink, oflags, mode, oplock,
504 fid, buf, inode, opened);
505 }
506 free_dentry_path(page);
507 return rc;
508 }
509
510
511 /*
512 * Look up, create and open a CIFS file.
513 *
514 * The initial dentry state is in-lookup or hashed-negative. On success, dentry
515 * will become hashed-positive by calling d_splice_alias() if in-lookup,
516 * otherwise d_instantiate().
517 */
518 int cifs_atomic_open(struct inode *dir, struct dentry *direntry,
519 struct file *file, unsigned int oflags, umode_t mode)
520 {
521 struct cifs_sb_info *cifs_sb = CIFS_SB(dir);
522 struct cifs_open_info_data buf = {};
523 struct TCP_Server_Info *server;
524 struct cifsFileInfo *file_info;
525 struct cifs_pending_open open;
526 struct cifs_fid fid = {};
527 struct tcon_link *tlink;
528 struct cifs_tcon *tcon;
529 unsigned int sbflags;
530 struct dentry *alias;
531 struct inode *inode;
532 unsigned int xid;
533 __u32 oplock;
534 bool is_regular;
535 bool opened;
536 int rc;
537
538 if (unlikely(cifs_forced_shutdown(cifs_sb)))
539 return smb_EIO(smb_eio_trace_forced_shutdown);
540
541 /*
542 * Posix open is only called (at lookup time) for file create now. For
543 * opens (rather than creates), because we do not know if it is a file
544 * or directory yet, and current Samba no longer allows us to do posix
545 * open on dirs, we could end up wasting an open call on what turns out
546 * to be a dir. For file opens, we wait to call posix open till
547 * cifs_open. It could be added to atomic_open in the future but the
548 * performance tradeoff of the extra network request when EISDIR or
549 * EACCES is returned would have to be weighed against the 50% reduction
550 * in network traffic in the other paths.
551 */
552 if (!(oflags & O_CREAT)) {
553 /*
554 * Check for hashed negative dentry. We have already revalidated
555 * the dentry and it is fine. No need to perform another lookup.
556 */
557 if (!d_in_lookup(direntry))
558 return -ENOENT;
559
560 return finish_no_open(file, cifs_lookup(dir, direntry, 0));
561 }
562
563 xid = get_xid();
564
565 cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
566 dir, direntry, direntry);
567
568 tlink = cifs_sb_tlink(cifs_sb);
569 if (IS_ERR(tlink)) {
570 rc = PTR_ERR(tlink);
571 goto out_free_xid;
572 }
573
574 tcon = tlink_tcon(tlink);
575
576 rc = check_name(direntry, tcon);
577 if (rc)
578 goto out;
579
580 server = tcon->ses->server;
581
582 if (server->ops->new_lease_key)
583 server->ops->new_lease_key(&fid);
584
585 cifs_add_pending_open(&fid, tlink, &open);
586
587 rc = cifs_do_create(dir, direntry, xid, tlink, oflags, mode,
588 &oplock, &fid, &buf, &inode, &opened);
589 if (rc) {
590 cifs_del_pending_open(&open);
591 goto out;
592 }
593
594 is_regular = S_ISREG(inode->i_mode);
595 if (!is_regular || !opened) {
596 if (opened && server->ops->close)
597 server->ops->close(xid, tcon, &fid);
598 cifs_del_pending_open(&open);
599 if (S_ISLNK(inode->i_mode) &&
600 (oflags & (O_NOFOLLOW | __O_REGULAR)) ==
601 (O_NOFOLLOW | __O_REGULAR) && !(oflags & O_EXCL)) {
602 iput(inode);
603 rc = -ELOOP;
604 goto out;
605 }
606 }
607
608 if (d_in_lookup(direntry)) {
609 alias = d_splice_alias(inode, direntry);
610 if (!IS_ERR_OR_NULL(alias))
611 direntry = alias;
612 } else {
613 d_instantiate(direntry, inode);
614 }
615
616 if (is_regular && opened &&
617 (oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
618 file->f_mode |= FMODE_CREATED;
619
620 if (!is_regular || !opened) {
621 rc = finish_no_open(file, NULL);
622 goto out;
623 }
624
625 rc = finish_open(file, direntry, generic_file_open);
626 if (rc) {
627 if (server->ops->close)
628 server->ops->close(xid, tcon, &fid);
629 cifs_del_pending_open(&open);
630 goto out;
631 }
632
633 sbflags = cifs_sb_flags(cifs_sb);
634 if ((file->f_flags & O_DIRECT) && (sbflags & CIFS_MOUNT_STRICT_IO)) {
635 if (sbflags & CIFS_MOUNT_NO_BRL)
636 file->f_op = &cifs_file_direct_nobrl_ops;
637 else
638 file->f_op = &cifs_file_direct_ops;
639 }
640
641 file_info = cifs_new_fileinfo(&fid, file, tlink, oplock, buf.symlink_target);
642 if (file_info == NULL) {
643 if (server->ops->close)
644 server->ops->close(xid, tcon, &fid);
645 cifs_del_pending_open(&open);
646 rc = -ENOMEM;
647 goto out;
648 }
649
650 fscache_use_cookie(cifs_inode_cookie(file_inode(file)),
651 file->f_mode & FMODE_WRITE);
652
653 out:
654 cifs_put_tlink(tlink);
655 out_free_xid:
656 free_xid(xid);
657 cifs_free_open_info(&buf);
658 return rc;
659 }
660
661 /*
662 * Create a CIFS file.
663 *
664 * The initial dentry state is hashed-negative. On success, dentry will become
665 * hashed-positive by calling d_instantiate().
666 */
667 int cifs_create(struct mnt_idmap *idmap, struct inode *dir,
668 struct dentry *direntry, umode_t mode)
669 {
670 struct cifs_sb_info *cifs_sb = CIFS_SB(dir);
671 int rc;
672 unsigned int xid = get_xid();
673 /*
674 * BB below access is probably too much for mknod to request
675 * but we have to do query and setpathinfo so requesting
676 * less could fail (unless we want to request getatr and setatr
677 * permissions (only). At least for POSIX we do not have to
678 * request so much.
679 */
680 unsigned oflags = O_EXCL | O_CREAT | O_RDWR;
681 struct tcon_link *tlink;
682 struct cifs_tcon *tcon;
683 struct TCP_Server_Info *server;
684 struct inode *inode;
685 struct cifs_fid fid;
686 __u32 oplock;
687 bool opened;
688 struct cifs_open_info_data buf = {};
689
690 cifs_dbg(FYI, "cifs_create parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
691 dir, direntry, direntry);
692
693 if (unlikely(cifs_forced_shutdown(cifs_sb))) {
694 rc = smb_EIO(smb_eio_trace_forced_shutdown);
695 goto out_free_xid;
696 }
697
698 tlink = cifs_sb_tlink(cifs_sb);
699 rc = PTR_ERR(tlink);
700 if (IS_ERR(tlink))
701 goto out_free_xid;
702
703 tcon = tlink_tcon(tlink);
704 server = tcon->ses->server;
705
706 if (server->ops->new_lease_key)
707 server->ops->new_lease_key(&fid);
708
709 rc = cifs_do_create(dir, direntry, xid, tlink, oflags,
710 mode, &oplock, &fid, &buf, &inode, &opened);
711 if (!rc) {
712 d_instantiate(direntry, inode);
713 if (opened && server->ops->close)
714 server->ops->close(xid, tcon, &fid);
715 }
716
717 cifs_free_open_info(&buf);
718 cifs_put_tlink(tlink);
719 out_free_xid:
720 free_xid(xid);
721 return rc;
722 }
723
724 int cifs_mknod(struct mnt_idmap *idmap, struct inode *inode,
725 struct dentry *direntry, umode_t mode, dev_t device_number)
726 {
727 int rc = -EPERM;
728 unsigned int xid;
729 struct cifs_sb_info *cifs_sb;
730 struct tcon_link *tlink;
731 struct cifs_tcon *tcon;
732 const char *full_path;
733 void *page;
734
735 if (!old_valid_dev(device_number))
736 return -EINVAL;
737
738 cifs_sb = CIFS_SB(inode->i_sb);
739 if (unlikely(cifs_forced_shutdown(cifs_sb)))
740 return smb_EIO(smb_eio_trace_forced_shutdown);
741
742 tlink = cifs_sb_tlink(cifs_sb);
743 if (IS_ERR(tlink))
744 return PTR_ERR(tlink);
745
746 page = alloc_dentry_path();
747 tcon = tlink_tcon(tlink);
748 xid = get_xid();
749
750 full_path = build_path_from_dentry(direntry, page);
751 if (IS_ERR(full_path)) {
752 rc = PTR_ERR(full_path);
753 goto mknod_out;
754 }
755
756 trace_smb3_mknod_enter(xid, tcon->tid, tcon->ses->Suid, full_path);
757
758 rc = tcon->ses->server->ops->make_node(xid, inode, direntry, tcon,
759 full_path, mode,
760 device_number);
761
762 mknod_out:
763 if (rc)
764 trace_smb3_mknod_err(xid, tcon->tid, tcon->ses->Suid, rc);
765 else
766 trace_smb3_mknod_done(xid, tcon->tid, tcon->ses->Suid);
767
768 free_dentry_path(page);
769 free_xid(xid);
770 cifs_put_tlink(tlink);
771 return rc;
772 }
773
774 struct dentry *
775 cifs_lookup(struct inode *parent_dir_inode, struct dentry *direntry,
776 unsigned int flags)
777 {
778 unsigned int xid;
779 int rc = 0; /* to get around spurious gcc warning, set to zero here */
780 struct cifs_sb_info *cifs_sb;
781 struct tcon_link *tlink;
782 struct cifs_tcon *pTcon;
783 struct inode *newInode = NULL;
784 const char *full_path;
785 void *page;
786 int retry_count = 0;
787 struct dentry *de;
788
789 xid = get_xid();
790
791 cifs_dbg(FYI, "parent inode = 0x%p name is: %pd and dentry = 0x%p\n",
792 parent_dir_inode, direntry, direntry);
793
794 /* check whether path exists */
795
796 cifs_sb = CIFS_SB(parent_dir_inode->i_sb);
797 tlink = cifs_sb_tlink(cifs_sb);
798 if (IS_ERR(tlink)) {
799 de = ERR_CAST(tlink);
800 goto free_xid;
801 }
802 pTcon = tlink_tcon(tlink);
803
804 rc = check_name(direntry, pTcon);
805 if (unlikely(rc)) {
806 de = ERR_PTR(rc);
807 goto put_tlink;
808 }
809
810 /* can not grab the rename sem here since it would
811 deadlock in the cases (beginning of sys_rename itself)
812 in which we already have the sb rename sem */
813 page = alloc_dentry_path();
814 full_path = build_path_from_dentry(direntry, page);
815 if (IS_ERR(full_path)) {
816 de = ERR_CAST(full_path);
817 goto free_dentry_path;
818 }
819
820 if (d_really_is_positive(direntry)) {
821 cifs_dbg(FYI, "non-NULL inode in lookup\n");
822 } else {
823 struct cached_fid *cfid = NULL;
824
825 cifs_dbg(FYI, "NULL inode in lookup\n");
826
827 /*
828 * We can only rely on negative dentries having the same
829 * spelling as the cached dirent if case insensitivity is
830 * forced on mount.
831 *
832 * XXX: if servers correctly announce Case Sensitivity Search
833 * on GetInfo of FileFSAttributeInformation, then we can take
834 * correct action even if case insensitive is not forced on
835 * mount.
836 */
837 if (pTcon->nocase && !open_cached_dir_by_dentry(pTcon, direntry->d_parent, &cfid)) {
838 /*
839 * dentry is negative and parent is fully cached:
840 * we can assume file does not exist
841 */
842 if (cfid->dirents.is_valid) {
843 close_cached_dir(cfid);
844 goto out;
845 }
846 close_cached_dir(cfid);
847 }
848 }
849 cifs_dbg(FYI, "Full path: %s inode = 0x%p\n",
850 full_path, d_inode(direntry));
851
852 again:
853 if (pTcon->posix_extensions) {
854 rc = smb311_posix_get_inode_info(&newInode, full_path, NULL,
855 parent_dir_inode->i_sb, xid);
856 } else if (pTcon->unix_ext) {
857 rc = cifs_get_inode_info_unix(&newInode, full_path,
858 parent_dir_inode->i_sb, xid);
859 } else {
860 rc = cifs_get_inode_info(&newInode, full_path, NULL,
861 parent_dir_inode->i_sb, xid, NULL);
862 }
863
864 if (rc == 0) {
865 /* since paths are not looked up by component - the parent
866 directories are presumed to be good here */
867 renew_parental_timestamps(direntry);
868 } else if (rc == -EAGAIN && retry_count++ < 10) {
869 goto again;
870 } else if (rc == -ENOENT) {
871 cifs_set_time(direntry, jiffies);
872 newInode = NULL;
873 } else {
874 if (rc != -EACCES) {
875 cifs_dbg(FYI, "Unexpected lookup error %d\n", rc);
876 /* We special case check for Access Denied - since that
877 is a common return code */
878 }
879 newInode = ERR_PTR(rc);
880 }
881
882 out:
883 de = d_splice_alias(newInode, direntry);
884 free_dentry_path:
885 free_dentry_path(page);
886 put_tlink:
887 cifs_put_tlink(tlink);
888 free_xid:
889 free_xid(xid);
890 return de;
891 }
892
893 static int
894 cifs_d_revalidate(struct inode *dir, const struct qstr *name,
895 struct dentry *direntry, unsigned int flags)
896 {
897 if (flags & LOOKUP_RCU)
898 return -ECHILD;
899
900 if (d_really_is_positive(direntry)) {
901 int rc;
902 struct inode *inode = d_inode(direntry);
903
904 if ((flags & LOOKUP_REVAL) && !CIFS_CACHE_READ(CIFS_I(inode)))
905 CIFS_I(inode)->time = 0; /* force reval */
906
907 rc = cifs_revalidate_dentry(direntry);
908 if (rc) {
909 cifs_dbg(FYI, "cifs_revalidate_dentry failed with rc=%d", rc);
910 switch (rc) {
911 case -ENOENT:
912 case -ESTALE:
913 /*
914 * Those errors mean the dentry is invalid
915 * (file was deleted or recreated)
916 */
917 return 0;
918 default:
919 /*
920 * Otherwise some unexpected error happened
921 * report it as-is to VFS layer
922 */
923 return rc;
924 }
925 }
926 else {
927 /*
928 * If the inode wasn't known to be a dfs entry when
929 * the dentry was instantiated, such as when created
930 * via ->readdir(), it needs to be set now since the
931 * attributes will have been updated by
932 * cifs_revalidate_dentry().
933 */
934 if (IS_AUTOMOUNT(inode) &&
935 !(direntry->d_flags & DCACHE_NEED_AUTOMOUNT)) {
936 spin_lock(&direntry->d_lock);
937 direntry->d_flags |= DCACHE_NEED_AUTOMOUNT;
938 spin_unlock(&direntry->d_lock);
939 }
940
941 return 1;
942 }
943 } else {
944 struct cifs_sb_info *cifs_sb = CIFS_SB(dir->i_sb);
945 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
946 struct cached_fid *cfid;
947
948 if (!open_cached_dir_by_dentry(tcon, direntry->d_parent, &cfid)) {
949 /*
950 * dentry is negative and parent is fully cached:
951 * we can assume file does not exist
952 */
953 if (cfid->dirents.is_valid) {
954 close_cached_dir(cfid);
955 return 1;
956 }
957 close_cached_dir(cfid);
958 }
959 }
960
961 /*
962 * This may be nfsd (or something), anyway, we can't see the
963 * intent of this. So, since this can be for creation, drop it.
964 */
965 if (!flags)
966 return 0;
967
968 /*
969 * Drop the negative dentry, in order to make sure to use the
970 * case sensitive name which is specified by user if this is
971 * for creation.
972 */
973 if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
974 return 0;
975
976 if (time_after(jiffies, cifs_get_time(direntry) + HZ) || !lookupCacheEnabled)
977 return 0;
978
979 return 1;
980 }
981
982 /* static int cifs_d_delete(struct dentry *direntry)
983 {
984 int rc = 0;
985
986 cifs_dbg(FYI, "In cifs d_delete, name = %pd\n", direntry);
987
988 return rc;
989 } */
990
991 const struct dentry_operations cifs_dentry_ops = {
992 .d_revalidate = cifs_d_revalidate,
993 .d_automount = cifs_d_automount,
994 /* d_delete: cifs_d_delete, */ /* not needed except for debugging */
995 };
996
997 static int cifs_ci_hash(const struct dentry *dentry, struct qstr *q)
998 {
999 struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
1000 unsigned long hash;
1001 wchar_t c;
1002 int i, charlen;
1003
1004 hash = init_name_hash(dentry);
1005 for (i = 0; i < q->len; i += charlen) {
1006 charlen = codepage->char2uni(&q->name[i], q->len - i, &c);
1007 /* error out if we can't convert the character */
1008 if (unlikely(charlen < 0))
1009 return charlen;
1010 hash = partial_name_hash(cifs_toupper(c), hash);
1011 }
1012 q->hash = end_name_hash(hash);
1013
1014 return 0;
1015 }
1016
1017 static int cifs_ci_compare(const struct dentry *dentry,
1018 unsigned int len, const char *str, const struct qstr *name)
1019 {
1020 struct nls_table *codepage = CIFS_SB(dentry->d_sb)->local_nls;
1021 wchar_t c1, c2;
1022 int i, l1, l2;
1023
1024 /*
1025 * We make the assumption here that uppercase characters in the local
1026 * codepage are always the same length as their lowercase counterparts.
1027 *
1028 * If that's ever not the case, then this will fail to match it.
1029 */
1030 if (name->len != len)
1031 return 1;
1032
1033 for (i = 0; i < len; i += l1) {
1034 /* Convert characters in both strings to UTF-16. */
1035 l1 = codepage->char2uni(&str[i], len - i, &c1);
1036 l2 = codepage->char2uni(&name->name[i], name->len - i, &c2);
1037
1038 /*
1039 * If we can't convert either character, just declare it to
1040 * be 1 byte long and compare the original byte.
1041 */
1042 if (unlikely(l1 < 0 && l2 < 0)) {
1043 if (str[i] != name->name[i])
1044 return 1;
1045 l1 = 1;
1046 continue;
1047 }
1048
1049 /*
1050 * Here, we again ass|u|me that upper/lowercase versions of
1051 * a character are the same length in the local NLS.
1052 */
1053 if (l1 != l2)
1054 return 1;
1055
1056 /* Now compare uppercase versions of these characters */
1057 if (cifs_toupper(c1) != cifs_toupper(c2))
1058 return 1;
1059 }
1060
1061 return 0;
1062 }
1063
1064 static int set_tmpfile_attr(const unsigned int xid, unsigned int oflags,
1065 struct inode *inode, const char *full_path,
1066 struct TCP_Server_Info *server)
1067 {
1068 struct cifsInodeInfo *cinode = CIFS_I(inode);
1069 FILE_BASIC_INFO fi;
1070
1071 cinode->cifsAttrs |= ATTR_HIDDEN;
1072 if (oflags & O_EXCL)
1073 cinode->cifsAttrs |= ATTR_TEMPORARY;
1074
1075 fi = (FILE_BASIC_INFO) {
1076 .Attributes = cpu_to_le32(cinode->cifsAttrs),
1077 };
1078 return server->ops->set_file_info(inode, full_path, &fi, xid);
1079 }
1080
1081 /*
1082 * Create a hidden temporary CIFS file with delete-on-close bit set.
1083 *
1084 * The initial dentry state is unhashed-negative. On success, dentry will
1085 * become unhashed-positive by calling d_instantiate().
1086 */
1087 int cifs_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
1088 struct file *file, umode_t mode)
1089 {
1090 struct dentry *dentry = file->f_path.dentry;
1091 struct cifs_sb_info *cifs_sb = CIFS_SB(dir);
1092 size_t namesize = CIFS_TMPNAME_LEN + 1;
1093 char *path __free(kfree) = NULL, *name;
1094 unsigned int oflags = file->f_flags;
1095 int retries = 0, max_retries = 16;
1096 struct TCP_Server_Info *server;
1097 struct cifs_pending_open open;
1098 struct cifsFileInfo *cfile;
1099 struct cifs_fid fid = {};
1100 struct tcon_link *tlink;
1101 struct cifs_tcon *tcon;
1102 unsigned int sbflags;
1103 struct inode *inode;
1104 unsigned int xid;
1105 __u32 oplock;
1106 bool opened;
1107 int namelen;
1108 int rc;
1109
1110 if (unlikely(cifs_forced_shutdown(cifs_sb)))
1111 return smb_EIO(smb_eio_trace_forced_shutdown);
1112
1113 tlink = cifs_sb_tlink(cifs_sb);
1114 if (IS_ERR(tlink))
1115 return PTR_ERR(tlink);
1116 tcon = tlink_tcon(tlink);
1117 server = tcon->ses->server;
1118
1119 xid = get_xid();
1120
1121 if (server->vals->protocol_id < SMB20_PROT_ID) {
1122 cifs_dbg(VFS | ONCE, "O_TMPFILE is supported only in SMB2+\n");
1123 rc = -EOPNOTSUPP;
1124 goto out;
1125 }
1126
1127 if (server->ops->new_lease_key)
1128 server->ops->new_lease_key(&fid);
1129 cifs_add_pending_open(&fid, tlink, &open);
1130
1131 path = alloc_parent_path(dentry, namesize - 1);
1132 if (IS_ERR(path)) {
1133 cifs_del_pending_open(&open);
1134 rc = PTR_ERR(path);
1135 path = NULL;
1136 goto out;
1137 }
1138
1139 name = path + strlen(path);
1140 do {
1141 /* Append tmpfile name to @path */
1142 namelen = scnprintf(name, namesize, CIFS_TMPNAME_PREFIX "%x",
1143 atomic_inc_return(&cifs_tmpcounter));
1144 rc = __cifs_do_create(dir, dentry, path, xid, tlink, oflags,
1145 mode, &oplock, &fid, NULL, &inode, &opened);
1146 if (!rc) {
1147 rc = d_mark_tmpfile_name(file, &QSTR_LEN(name, namelen));
1148 if (rc) {
1149 cifs_dbg(VFS | ONCE, "%s: failed to set filename in dentry: %d\n",
1150 __func__, rc);
1151 rc = -EISDIR;
1152 iput(inode);
1153 goto err_open;
1154 }
1155 set_nlink(inode, 0);
1156 mark_inode_dirty(inode);
1157 d_instantiate(dentry, inode);
1158 break;
1159 }
1160 } while (unlikely(rc == -EEXIST) && ++retries < max_retries);
1161
1162 if (rc) {
1163 if (rc == -ENOENT)
1164 rc = -EOPNOTSUPP;
1165 cifs_del_pending_open(&open);
1166 goto out;
1167 }
1168
1169 rc = finish_open(file, dentry, generic_file_open);
1170 if (rc)
1171 goto err_open;
1172
1173 sbflags = cifs_sb_flags(cifs_sb);
1174 if ((file->f_flags & O_DIRECT) && (sbflags & CIFS_MOUNT_STRICT_IO)) {
1175 if (sbflags & CIFS_MOUNT_NO_BRL)
1176 file->f_op = &cifs_file_direct_nobrl_ops;
1177 else
1178 file->f_op = &cifs_file_direct_ops;
1179 }
1180
1181 cfile = cifs_new_fileinfo(&fid, file, tlink, oplock, NULL);
1182 if (!cfile) {
1183 rc = -ENOMEM;
1184 goto err_open;
1185 }
1186
1187 rc = set_tmpfile_attr(xid, oflags, inode, path, server);
1188 if (rc)
1189 goto out;
1190
1191 fscache_use_cookie(cifs_inode_cookie(file_inode(file)),
1192 file->f_mode & FMODE_WRITE);
1193 out:
1194 cifs_put_tlink(tlink);
1195 free_xid(xid);
1196 return rc;
1197 err_open:
1198 cifs_del_pending_open(&open);
1199 if (server->ops->close)
1200 server->ops->close(xid, tcon, &fid);
1201 goto out;
1202 }
1203
1204 char *cifs_silly_fullpath(struct dentry *dentry)
1205 {
1206 unsigned char name[CIFS_SILLYNAME_LEN + 1];
1207 int retries = 0, max_retries = 16;
1208 size_t namesize = sizeof(name);
1209 struct dentry *sdentry = NULL;
1210 char *path;
1211
1212 do {
1213 dput(sdentry);
1214 scnprintf(name, namesize, CIFS_SILLYNAME_PREFIX "%x",
1215 atomic_inc_return(&cifs_sillycounter));
1216 sdentry = lookup_noperm(&QSTR(name), dentry->d_parent);
1217 if (IS_ERR(sdentry))
1218 return ERR_CAST(sdentry);
1219 if (d_is_negative(sdentry)) {
1220 dput(sdentry);
1221 path = alloc_parent_path(dentry, CIFS_SILLYNAME_LEN);
1222 if (!IS_ERR(path))
1223 strcat(path, name);
1224 return path;
1225 }
1226 } while (++retries < max_retries);
1227 dput(sdentry);
1228 return ERR_PTR(-EBUSY);
1229 }
1230
1231 const struct dentry_operations cifs_ci_dentry_ops = {
1232 .d_revalidate = cifs_d_revalidate,
1233 .d_hash = cifs_ci_hash,
1234 .d_compare = cifs_ci_compare,
1235 .d_automount = cifs_d_automount,
1236 };
1237