1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3 *
4 * Copyright (C) International Business Machines Corp., 2002,2010
5 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 */
8 #include <linux/fs.h>
9 #include <linux/fs_struct.h>
10 #include <linux/stat.h>
11 #include <linux/slab.h>
12 #include <linux/pagemap.h>
13 #include <linux/freezer.h>
14 #include <linux/sched/signal.h>
15 #include <linux/wait_bit.h>
16 #include <linux/fiemap.h>
17 #include <asm/div64.h>
18 #include "cifsfs.h"
19 #include "cifspdu.h"
20 #include "cifsglob.h"
21 #include "cifsproto.h"
22 #include "smb2proto.h"
23 #include "cifs_debug.h"
24 #include "cifs_fs_sb.h"
25 #include "cifs_unicode.h"
26 #include "fscache.h"
27 #include "fs_context.h"
28 #include "cifs_ioctl.h"
29 #include "cached_dir.h"
30 #include "reparse.h"
31
32 /*
33 * Set parameters for the netfs library
34 */
cifs_set_netfs_context(struct inode * inode)35 static void cifs_set_netfs_context(struct inode *inode)
36 {
37 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
38
39 netfs_inode_init(&cifs_i->netfs, &cifs_req_ops, true);
40 }
41
cifs_set_ops(struct inode * inode)42 static void cifs_set_ops(struct inode *inode)
43 {
44 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
45 struct netfs_inode *ictx = netfs_inode(inode);
46
47 switch (inode->i_mode & S_IFMT) {
48 case S_IFREG:
49 inode->i_op = &cifs_file_inode_ops;
50 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
51 set_bit(NETFS_ICTX_UNBUFFERED, &ictx->flags);
52 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
53 inode->i_fop = &cifs_file_direct_nobrl_ops;
54 else
55 inode->i_fop = &cifs_file_direct_ops;
56 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
57 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
58 inode->i_fop = &cifs_file_strict_nobrl_ops;
59 else
60 inode->i_fop = &cifs_file_strict_ops;
61 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
62 inode->i_fop = &cifs_file_nobrl_ops;
63 else { /* not direct, send byte range locks */
64 inode->i_fop = &cifs_file_ops;
65 }
66
67 /* check if server can support readahead */
68 if (cifs_sb_master_tcon(cifs_sb)->ses->server->max_read <
69 PAGE_SIZE + MAX_CIFS_HDR_SIZE)
70 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
71 else
72 inode->i_data.a_ops = &cifs_addr_ops;
73 mapping_set_large_folios(inode->i_mapping);
74 break;
75 case S_IFDIR:
76 if (IS_AUTOMOUNT(inode)) {
77 inode->i_op = &cifs_namespace_inode_operations;
78 } else {
79 inode->i_op = &cifs_dir_inode_ops;
80 inode->i_fop = &cifs_dir_ops;
81 }
82 break;
83 case S_IFLNK:
84 inode->i_op = &cifs_symlink_inode_ops;
85 break;
86 default:
87 init_special_inode(inode, inode->i_mode, inode->i_rdev);
88 break;
89 }
90 }
91
92 /* check inode attributes against fattr. If they don't match, tag the
93 * inode for cache invalidation
94 */
95 static void
cifs_revalidate_cache(struct inode * inode,struct cifs_fattr * fattr)96 cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
97 {
98 struct cifs_fscache_inode_coherency_data cd;
99 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
100 struct timespec64 mtime;
101
102 cifs_dbg(FYI, "%s: revalidating inode %llu\n",
103 __func__, cifs_i->uniqueid);
104
105 if (inode_state_read_once(inode) & I_NEW) {
106 cifs_dbg(FYI, "%s: inode %llu is new\n",
107 __func__, cifs_i->uniqueid);
108 return;
109 }
110
111 /* don't bother with revalidation if we have an oplock */
112 if (CIFS_CACHE_READ(cifs_i)) {
113 cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
114 __func__, cifs_i->uniqueid);
115 return;
116 }
117
118 /* revalidate if mtime or size have changed */
119 fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
120 mtime = inode_get_mtime(inode);
121 if (timespec64_equal(&mtime, &fattr->cf_mtime) &&
122 cifs_i->netfs.remote_i_size == fattr->cf_eof) {
123 cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
124 __func__, cifs_i->uniqueid);
125 return;
126 }
127
128 cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
129 __func__, cifs_i->uniqueid);
130 set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
131 /* Invalidate fscache cookie */
132 cifs_fscache_fill_coherency(&cifs_i->netfs.inode, &cd);
133 fscache_invalidate(cifs_inode_cookie(inode), &cd, i_size_read(inode), 0);
134 }
135
136 /*
137 * copy nlink to the inode, unless it wasn't provided. Provide
138 * sane values if we don't have an existing one and none was provided
139 */
140 static void
cifs_nlink_fattr_to_inode(struct inode * inode,struct cifs_fattr * fattr)141 cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
142 {
143 /*
144 * if we're in a situation where we can't trust what we
145 * got from the server (readdir, some non-unix cases)
146 * fake reasonable values
147 */
148 if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) {
149 /* only provide fake values on a new inode */
150 if (inode_state_read_once(inode) & I_NEW) {
151 if (fattr->cf_cifsattrs & ATTR_DIRECTORY)
152 set_nlink(inode, 2);
153 else
154 set_nlink(inode, 1);
155 }
156 return;
157 }
158
159 /* we trust the server, so update it */
160 set_nlink(inode, fattr->cf_nlink);
161 }
162
163 /* populate an inode with info from a cifs_fattr struct */
164 int
cifs_fattr_to_inode(struct inode * inode,struct cifs_fattr * fattr,bool from_readdir)165 cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr,
166 bool from_readdir)
167 {
168 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
169 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
170
171 if (!(inode_state_read_once(inode) & I_NEW) &&
172 unlikely(inode_wrong_type(inode, fattr->cf_mode))) {
173 CIFS_I(inode)->time = 0; /* force reval */
174 return -ESTALE;
175 }
176 if (inode_state_read_once(inode) & I_NEW)
177 CIFS_I(inode)->netfs.zero_point = fattr->cf_eof;
178
179 cifs_revalidate_cache(inode, fattr);
180
181 spin_lock(&inode->i_lock);
182 fattr->cf_mtime = timestamp_truncate(fattr->cf_mtime, inode);
183 fattr->cf_atime = timestamp_truncate(fattr->cf_atime, inode);
184 fattr->cf_ctime = timestamp_truncate(fattr->cf_ctime, inode);
185 /* we do not want atime to be less than mtime, it broke some apps */
186 if (timespec64_compare(&fattr->cf_atime, &fattr->cf_mtime) < 0)
187 inode_set_atime_to_ts(inode, fattr->cf_mtime);
188 else
189 inode_set_atime_to_ts(inode, fattr->cf_atime);
190 inode_set_mtime_to_ts(inode, fattr->cf_mtime);
191 inode_set_ctime_to_ts(inode, fattr->cf_ctime);
192 inode->i_rdev = fattr->cf_rdev;
193 cifs_nlink_fattr_to_inode(inode, fattr);
194 inode->i_uid = fattr->cf_uid;
195 inode->i_gid = fattr->cf_gid;
196
197 /* if dynperm is set, don't clobber existing mode */
198 if (inode_state_read(inode) & I_NEW ||
199 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
200 inode->i_mode = fattr->cf_mode;
201
202 cifs_i->cifsAttrs = fattr->cf_cifsattrs;
203 cifs_i->reparse_tag = fattr->cf_cifstag;
204
205 if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
206 cifs_i->time = 0;
207 else
208 cifs_i->time = jiffies;
209
210 if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
211 set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
212 else
213 clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
214
215 cifs_i->netfs.remote_i_size = fattr->cf_eof;
216 /*
217 * Can't safely change the file size here if the client is writing to
218 * it due to potential races.
219 */
220 if (is_size_safe_to_change(cifs_i, fattr->cf_eof, from_readdir)) {
221 i_size_write(inode, fattr->cf_eof);
222
223 /*
224 * i_blocks is not related to (i_size / i_blksize),
225 * but instead 512 byte (2**9) size is required for
226 * calculating num blocks.
227 */
228 inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
229 }
230
231 if (S_ISLNK(fattr->cf_mode) && fattr->cf_symlink_target) {
232 kfree(cifs_i->symlink_target);
233 cifs_i->symlink_target = fattr->cf_symlink_target;
234 fattr->cf_symlink_target = NULL;
235 }
236 spin_unlock(&inode->i_lock);
237
238 if (fattr->cf_flags & CIFS_FATTR_JUNCTION)
239 inode->i_flags |= S_AUTOMOUNT;
240 if (inode_state_read_once(inode) & I_NEW) {
241 cifs_set_netfs_context(inode);
242 cifs_set_ops(inode);
243 }
244 return 0;
245 }
246
247 void
cifs_fill_uniqueid(struct super_block * sb,struct cifs_fattr * fattr)248 cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
249 {
250 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
251
252 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
253 return;
254
255 fattr->cf_uniqueid = iunique(sb, ROOT_I);
256 }
257
258 /* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
259 void
cifs_unix_basic_to_fattr(struct cifs_fattr * fattr,FILE_UNIX_BASIC_INFO * info,struct cifs_sb_info * cifs_sb)260 cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
261 struct cifs_sb_info *cifs_sb)
262 {
263 memset(fattr, 0, sizeof(*fattr));
264 fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
265 fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
266 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
267
268 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
269 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
270 fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
271 /* old POSIX extensions don't get create time */
272
273 fattr->cf_mode = le64_to_cpu(info->Permissions);
274
275 /*
276 * Since we set the inode type below we need to mask off
277 * to avoid strange results if bits set above.
278 */
279 fattr->cf_mode &= ~S_IFMT;
280 switch (le32_to_cpu(info->Type)) {
281 case UNIX_FILE:
282 fattr->cf_mode |= S_IFREG;
283 fattr->cf_dtype = DT_REG;
284 break;
285 case UNIX_SYMLINK:
286 fattr->cf_mode |= S_IFLNK;
287 fattr->cf_dtype = DT_LNK;
288 break;
289 case UNIX_DIR:
290 fattr->cf_mode |= S_IFDIR;
291 fattr->cf_dtype = DT_DIR;
292 break;
293 case UNIX_CHARDEV:
294 fattr->cf_mode |= S_IFCHR;
295 fattr->cf_dtype = DT_CHR;
296 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
297 le64_to_cpu(info->DevMinor) & MINORMASK);
298 break;
299 case UNIX_BLOCKDEV:
300 fattr->cf_mode |= S_IFBLK;
301 fattr->cf_dtype = DT_BLK;
302 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
303 le64_to_cpu(info->DevMinor) & MINORMASK);
304 break;
305 case UNIX_FIFO:
306 fattr->cf_mode |= S_IFIFO;
307 fattr->cf_dtype = DT_FIFO;
308 break;
309 case UNIX_SOCKET:
310 fattr->cf_mode |= S_IFSOCK;
311 fattr->cf_dtype = DT_SOCK;
312 break;
313 default:
314 /* safest to call it a file if we do not know */
315 fattr->cf_mode |= S_IFREG;
316 fattr->cf_dtype = DT_REG;
317 cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
318 break;
319 }
320
321 fattr->cf_uid = cifs_sb->ctx->linux_uid;
322 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
323 u64 id = le64_to_cpu(info->Uid);
324 if (id < ((uid_t)-1)) {
325 kuid_t uid = make_kuid(&init_user_ns, id);
326 if (uid_valid(uid))
327 fattr->cf_uid = uid;
328 }
329 }
330
331 fattr->cf_gid = cifs_sb->ctx->linux_gid;
332 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
333 u64 id = le64_to_cpu(info->Gid);
334 if (id < ((gid_t)-1)) {
335 kgid_t gid = make_kgid(&init_user_ns, id);
336 if (gid_valid(gid))
337 fattr->cf_gid = gid;
338 }
339 }
340
341 fattr->cf_nlink = le64_to_cpu(info->Nlinks);
342 }
343
344 /*
345 * Fill a cifs_fattr struct with fake inode info.
346 *
347 * Needed to setup cifs_fattr data for the directory which is the
348 * junction to the new submount (ie to setup the fake directory
349 * which represents a DFS referral or reparse mount point).
350 */
cifs_create_junction_fattr(struct cifs_fattr * fattr,struct super_block * sb)351 static void cifs_create_junction_fattr(struct cifs_fattr *fattr,
352 struct super_block *sb)
353 {
354 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
355
356 cifs_dbg(FYI, "%s: creating fake fattr\n", __func__);
357
358 memset(fattr, 0, sizeof(*fattr));
359 fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
360 fattr->cf_uid = cifs_sb->ctx->linux_uid;
361 fattr->cf_gid = cifs_sb->ctx->linux_gid;
362 ktime_get_coarse_real_ts64(&fattr->cf_mtime);
363 fattr->cf_atime = fattr->cf_ctime = fattr->cf_mtime;
364 fattr->cf_nlink = 2;
365 fattr->cf_flags = CIFS_FATTR_JUNCTION;
366 }
367
368 /* Update inode with final fattr data */
update_inode_info(struct super_block * sb,struct cifs_fattr * fattr,struct inode ** inode)369 static int update_inode_info(struct super_block *sb,
370 struct cifs_fattr *fattr,
371 struct inode **inode)
372 {
373 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
374 int rc = 0;
375
376 if (!*inode) {
377 *inode = cifs_iget(sb, fattr);
378 if (!*inode)
379 rc = -ENOMEM;
380 return rc;
381 }
382 /* We already have inode, update it.
383 *
384 * If file type or uniqueid is different, return error.
385 */
386 if (unlikely((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
387 CIFS_I(*inode)->uniqueid != fattr->cf_uniqueid)) {
388 CIFS_I(*inode)->time = 0; /* force reval */
389 return -ESTALE;
390 }
391 return cifs_fattr_to_inode(*inode, fattr, false);
392 }
393
394 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
395 static int
cifs_get_file_info_unix(struct file * filp)396 cifs_get_file_info_unix(struct file *filp)
397 {
398 int rc;
399 unsigned int xid;
400 FILE_UNIX_BASIC_INFO find_data;
401 struct cifs_fattr fattr = {};
402 struct inode *inode = file_inode(filp);
403 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
404 struct cifsFileInfo *cfile = filp->private_data;
405 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
406
407 xid = get_xid();
408
409 if (cfile->symlink_target) {
410 fattr.cf_symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
411 if (!fattr.cf_symlink_target) {
412 rc = -ENOMEM;
413 goto cifs_gfiunix_out;
414 }
415 }
416
417 rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
418 if (!rc) {
419 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
420 } else if (rc == -EREMOTE) {
421 cifs_create_junction_fattr(&fattr, inode->i_sb);
422 } else
423 goto cifs_gfiunix_out;
424
425 rc = cifs_fattr_to_inode(inode, &fattr, false);
426
427 cifs_gfiunix_out:
428 free_xid(xid);
429 return rc;
430 }
431
cifs_get_unix_fattr(const unsigned char * full_path,struct super_block * sb,struct cifs_fattr * fattr,struct inode ** pinode,const unsigned int xid)432 static int cifs_get_unix_fattr(const unsigned char *full_path,
433 struct super_block *sb,
434 struct cifs_fattr *fattr,
435 struct inode **pinode,
436 const unsigned int xid)
437 {
438 struct TCP_Server_Info *server;
439 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
440 FILE_UNIX_BASIC_INFO find_data;
441 struct cifs_tcon *tcon;
442 struct tcon_link *tlink;
443 int rc, tmprc;
444
445 cifs_dbg(FYI, "Getting info on %s\n", full_path);
446
447 tlink = cifs_sb_tlink(cifs_sb);
448 if (IS_ERR(tlink))
449 return PTR_ERR(tlink);
450 tcon = tlink_tcon(tlink);
451 server = tcon->ses->server;
452
453 /* could have done a find first instead but this returns more info */
454 rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
455 cifs_sb->local_nls, cifs_remap(cifs_sb));
456 cifs_dbg(FYI, "%s: query path info: rc = %d\n", __func__, rc);
457 cifs_put_tlink(tlink);
458
459 if (!rc) {
460 cifs_unix_basic_to_fattr(fattr, &find_data, cifs_sb);
461 } else if (rc == -EREMOTE) {
462 cifs_create_junction_fattr(fattr, sb);
463 rc = 0;
464 } else {
465 return rc;
466 }
467
468 if (!*pinode)
469 cifs_fill_uniqueid(sb, fattr);
470
471 /* check for Minshall+French symlinks */
472 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
473 tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
474 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
475 }
476
477 if (S_ISLNK(fattr->cf_mode) && !fattr->cf_symlink_target) {
478 if (!server->ops->query_symlink)
479 return -EOPNOTSUPP;
480 rc = server->ops->query_symlink(xid, tcon,
481 cifs_sb, full_path,
482 &fattr->cf_symlink_target);
483 cifs_dbg(FYI, "%s: query_symlink: %d\n", __func__, rc);
484 }
485 return rc;
486 }
487
cifs_get_inode_info_unix(struct inode ** pinode,const unsigned char * full_path,struct super_block * sb,unsigned int xid)488 int cifs_get_inode_info_unix(struct inode **pinode,
489 const unsigned char *full_path,
490 struct super_block *sb, unsigned int xid)
491 {
492 struct cifs_fattr fattr = {};
493 int rc;
494
495 rc = cifs_get_unix_fattr(full_path, sb, &fattr, pinode, xid);
496 if (rc)
497 goto out;
498
499 rc = update_inode_info(sb, &fattr, pinode);
500 out:
501 kfree(fattr.cf_symlink_target);
502 return rc;
503 }
504 #else
cifs_get_unix_fattr(const unsigned char * full_path,struct super_block * sb,struct cifs_fattr * fattr,struct inode ** pinode,const unsigned int xid)505 static inline int cifs_get_unix_fattr(const unsigned char *full_path,
506 struct super_block *sb,
507 struct cifs_fattr *fattr,
508 struct inode **pinode,
509 const unsigned int xid)
510 {
511 return -EOPNOTSUPP;
512 }
513
cifs_get_inode_info_unix(struct inode ** pinode,const unsigned char * full_path,struct super_block * sb,unsigned int xid)514 int cifs_get_inode_info_unix(struct inode **pinode,
515 const unsigned char *full_path,
516 struct super_block *sb, unsigned int xid)
517 {
518 return -EOPNOTSUPP;
519 }
520 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
521
522 static int
cifs_sfu_type(struct cifs_fattr * fattr,const char * path,struct cifs_sb_info * cifs_sb,unsigned int xid)523 cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
524 struct cifs_sb_info *cifs_sb, unsigned int xid)
525 {
526 int rc;
527 __u32 oplock;
528 struct tcon_link *tlink;
529 struct cifs_tcon *tcon;
530 struct cifs_fid fid;
531 struct cifs_open_parms oparms;
532 struct cifs_io_parms io_parms = {0};
533 char *symlink_buf_utf16;
534 unsigned int symlink_len_utf16;
535 char buf[24];
536 unsigned int bytes_read;
537 char *pbuf;
538 int buf_type = CIFS_NO_BUFFER;
539
540 pbuf = buf;
541
542 fattr->cf_mode &= ~S_IFMT;
543
544 if (fattr->cf_eof == 0) {
545 cifs_dbg(FYI, "Fifo\n");
546 fattr->cf_mode |= S_IFIFO;
547 fattr->cf_dtype = DT_FIFO;
548 return 0;
549 } else if (fattr->cf_eof > 1 && fattr->cf_eof < 8) {
550 fattr->cf_mode |= S_IFREG;
551 fattr->cf_dtype = DT_REG;
552 return -EINVAL; /* EOPNOTSUPP? */
553 }
554
555 tlink = cifs_sb_tlink(cifs_sb);
556 if (IS_ERR(tlink))
557 return PTR_ERR(tlink);
558 tcon = tlink_tcon(tlink);
559
560 oparms = (struct cifs_open_parms) {
561 .tcon = tcon,
562 .cifs_sb = cifs_sb,
563 .desired_access = GENERIC_READ,
564 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
565 .disposition = FILE_OPEN,
566 .path = path,
567 .fid = &fid,
568 };
569
570 if (tcon->ses->server->oplocks)
571 oplock = REQ_OPLOCK;
572 else
573 oplock = 0;
574 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
575 if (rc) {
576 cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc);
577 cifs_put_tlink(tlink);
578 return rc;
579 }
580
581 /* Read header */
582 io_parms.netfid = fid.netfid;
583 io_parms.pid = current->tgid;
584 io_parms.tcon = tcon;
585 io_parms.offset = 0;
586 io_parms.length = 24;
587
588 rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
589 &bytes_read, &pbuf, &buf_type);
590 if ((rc == 0) && (bytes_read >= 8)) {
591 if (memcmp("IntxBLK\0", pbuf, 8) == 0) {
592 cifs_dbg(FYI, "Block device\n");
593 fattr->cf_mode |= S_IFBLK;
594 fattr->cf_dtype = DT_BLK;
595 if (bytes_read == 24) {
596 /* we have enough to decode dev num */
597 __u64 mjr; /* major */
598 __u64 mnr; /* minor */
599 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
600 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
601 fattr->cf_rdev = MKDEV(mjr, mnr);
602 } else if (bytes_read == 16) {
603 /*
604 * Windows NFS server before Windows Server 2012
605 * stores major and minor number in SFU-modified
606 * style, just as 32-bit numbers. Recognize it.
607 */
608 __u32 mjr; /* major */
609 __u32 mnr; /* minor */
610 mjr = le32_to_cpu(*(__le32 *)(pbuf+8));
611 mnr = le32_to_cpu(*(__le32 *)(pbuf+12));
612 fattr->cf_rdev = MKDEV(mjr, mnr);
613 }
614 } else if (memcmp("IntxCHR\0", pbuf, 8) == 0) {
615 cifs_dbg(FYI, "Char device\n");
616 fattr->cf_mode |= S_IFCHR;
617 fattr->cf_dtype = DT_CHR;
618 if (bytes_read == 24) {
619 /* we have enough to decode dev num */
620 __u64 mjr; /* major */
621 __u64 mnr; /* minor */
622 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
623 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
624 fattr->cf_rdev = MKDEV(mjr, mnr);
625 } else if (bytes_read == 16) {
626 /*
627 * Windows NFS server before Windows Server 2012
628 * stores major and minor number in SFU-modified
629 * style, just as 32-bit numbers. Recognize it.
630 */
631 __u32 mjr; /* major */
632 __u32 mnr; /* minor */
633 mjr = le32_to_cpu(*(__le32 *)(pbuf+8));
634 mnr = le32_to_cpu(*(__le32 *)(pbuf+12));
635 fattr->cf_rdev = MKDEV(mjr, mnr);
636 }
637 } else if (memcmp("LnxSOCK", pbuf, 8) == 0) {
638 cifs_dbg(FYI, "Socket\n");
639 fattr->cf_mode |= S_IFSOCK;
640 fattr->cf_dtype = DT_SOCK;
641 } else if (memcmp("IntxLNK\1", pbuf, 8) == 0) {
642 cifs_dbg(FYI, "Symlink\n");
643 fattr->cf_mode |= S_IFLNK;
644 fattr->cf_dtype = DT_LNK;
645 if ((fattr->cf_eof > 8) && (fattr->cf_eof % 2 == 0)) {
646 symlink_buf_utf16 = kmalloc(fattr->cf_eof-8 + 1, GFP_KERNEL);
647 if (symlink_buf_utf16) {
648 io_parms.offset = 8;
649 io_parms.length = fattr->cf_eof-8 + 1;
650 buf_type = CIFS_NO_BUFFER;
651 rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
652 &symlink_len_utf16,
653 &symlink_buf_utf16,
654 &buf_type);
655 /*
656 * Check that read buffer has valid length and does not
657 * contain UTF-16 null codepoint (via UniStrnlen() call)
658 * because Linux cannot process symlink with null byte.
659 */
660 if ((rc == 0) &&
661 (symlink_len_utf16 > 0) &&
662 (symlink_len_utf16 < fattr->cf_eof-8 + 1) &&
663 (symlink_len_utf16 % 2 == 0) &&
664 (UniStrnlen((wchar_t *)symlink_buf_utf16, symlink_len_utf16/2) == symlink_len_utf16/2)) {
665 fattr->cf_symlink_target =
666 cifs_strndup_from_utf16(symlink_buf_utf16,
667 symlink_len_utf16,
668 true,
669 cifs_sb->local_nls);
670 if (!fattr->cf_symlink_target)
671 rc = -ENOMEM;
672 }
673 kfree(symlink_buf_utf16);
674 } else {
675 rc = -ENOMEM;
676 }
677 }
678 } else if (memcmp("LnxFIFO", pbuf, 8) == 0) {
679 cifs_dbg(FYI, "FIFO\n");
680 fattr->cf_mode |= S_IFIFO;
681 fattr->cf_dtype = DT_FIFO;
682 } else {
683 fattr->cf_mode |= S_IFREG; /* file? */
684 fattr->cf_dtype = DT_REG;
685 rc = -EOPNOTSUPP;
686 }
687 } else if ((rc == 0) && (bytes_read == 1) && (pbuf[0] == '\0')) {
688 cifs_dbg(FYI, "Socket\n");
689 fattr->cf_mode |= S_IFSOCK;
690 fattr->cf_dtype = DT_SOCK;
691 } else {
692 fattr->cf_mode |= S_IFREG; /* then it is a file */
693 fattr->cf_dtype = DT_REG;
694 rc = -EOPNOTSUPP; /* or some unknown SFU type */
695 }
696
697 tcon->ses->server->ops->close(xid, tcon, &fid);
698 cifs_put_tlink(tlink);
699 return rc;
700 }
701
702 #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
703
704 /*
705 * Fetch mode bits as provided by SFU.
706 *
707 * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
708 */
cifs_sfu_mode(struct cifs_fattr * fattr,const unsigned char * path,struct cifs_sb_info * cifs_sb,unsigned int xid)709 static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
710 struct cifs_sb_info *cifs_sb, unsigned int xid)
711 {
712 #ifdef CONFIG_CIFS_XATTR
713 ssize_t rc;
714 char ea_value[4];
715 __u32 mode;
716 struct tcon_link *tlink;
717 struct cifs_tcon *tcon;
718
719 tlink = cifs_sb_tlink(cifs_sb);
720 if (IS_ERR(tlink))
721 return PTR_ERR(tlink);
722 tcon = tlink_tcon(tlink);
723
724 if (tcon->ses->server->ops->query_all_EAs == NULL) {
725 cifs_put_tlink(tlink);
726 return -EOPNOTSUPP;
727 }
728
729 rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
730 "SETFILEBITS", ea_value, 4 /* size of buf */,
731 cifs_sb);
732 cifs_put_tlink(tlink);
733 if (rc < 0)
734 return (int)rc;
735 else if (rc > 3) {
736 mode = le32_to_cpu(*((__le32 *)ea_value));
737 fattr->cf_mode &= ~SFBITS_MASK;
738 cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
739 mode, fattr->cf_mode);
740 fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
741 cifs_dbg(FYI, "special mode bits 0%o\n", mode);
742 }
743
744 return 0;
745 #else
746 return -EOPNOTSUPP;
747 #endif
748 }
749
750 #define POSIX_TYPE_FILE 0
751 #define POSIX_TYPE_DIR 1
752 #define POSIX_TYPE_SYMLINK 2
753 #define POSIX_TYPE_CHARDEV 3
754 #define POSIX_TYPE_BLKDEV 4
755 #define POSIX_TYPE_FIFO 5
756 #define POSIX_TYPE_SOCKET 6
757
758 #define POSIX_X_OTH 0000001
759 #define POSIX_W_OTH 0000002
760 #define POSIX_R_OTH 0000004
761 #define POSIX_X_GRP 0000010
762 #define POSIX_W_GRP 0000020
763 #define POSIX_R_GRP 0000040
764 #define POSIX_X_USR 0000100
765 #define POSIX_W_USR 0000200
766 #define POSIX_R_USR 0000400
767 #define POSIX_STICKY 0001000
768 #define POSIX_SET_GID 0002000
769 #define POSIX_SET_UID 0004000
770
771 #define POSIX_OTH_MASK 0000007
772 #define POSIX_GRP_MASK 0000070
773 #define POSIX_USR_MASK 0000700
774 #define POSIX_PERM_MASK 0000777
775 #define POSIX_FILETYPE_MASK 0070000
776
777 #define POSIX_FILETYPE_SHIFT 12
778
wire_perms_to_posix(u32 wire)779 static u32 wire_perms_to_posix(u32 wire)
780 {
781 u32 mode = 0;
782
783 mode |= (wire & POSIX_X_OTH) ? S_IXOTH : 0;
784 mode |= (wire & POSIX_W_OTH) ? S_IWOTH : 0;
785 mode |= (wire & POSIX_R_OTH) ? S_IROTH : 0;
786 mode |= (wire & POSIX_X_GRP) ? S_IXGRP : 0;
787 mode |= (wire & POSIX_W_GRP) ? S_IWGRP : 0;
788 mode |= (wire & POSIX_R_GRP) ? S_IRGRP : 0;
789 mode |= (wire & POSIX_X_USR) ? S_IXUSR : 0;
790 mode |= (wire & POSIX_W_USR) ? S_IWUSR : 0;
791 mode |= (wire & POSIX_R_USR) ? S_IRUSR : 0;
792 mode |= (wire & POSIX_STICKY) ? S_ISVTX : 0;
793 mode |= (wire & POSIX_SET_GID) ? S_ISGID : 0;
794 mode |= (wire & POSIX_SET_UID) ? S_ISUID : 0;
795
796 return mode;
797 }
798
799 static u32 posix_filetypes[] = {
800 S_IFREG,
801 S_IFDIR,
802 S_IFLNK,
803 S_IFCHR,
804 S_IFBLK,
805 S_IFIFO,
806 S_IFSOCK
807 };
808
wire_filetype_to_posix(u32 wire_type)809 static u32 wire_filetype_to_posix(u32 wire_type)
810 {
811 if (wire_type >= ARRAY_SIZE(posix_filetypes)) {
812 pr_warn("Unexpected type %u", wire_type);
813 return 0;
814 }
815 return posix_filetypes[wire_type];
816 }
817
wire_mode_to_posix(u32 wire,bool is_dir)818 umode_t wire_mode_to_posix(u32 wire, bool is_dir)
819 {
820 u32 wire_type;
821 u32 mode;
822
823 wire_type = (wire & POSIX_FILETYPE_MASK) >> POSIX_FILETYPE_SHIFT;
824 /* older servers do not set POSIX file type in the mode field in the response */
825 if ((wire_type == 0) && is_dir)
826 mode = wire_perms_to_posix(wire) | S_IFDIR;
827 else
828 mode = (wire_perms_to_posix(wire) | wire_filetype_to_posix(wire_type));
829 return (umode_t)mode;
830 }
831
832 /* Fill a cifs_fattr struct with info from POSIX info struct */
smb311_posix_info_to_fattr(struct cifs_fattr * fattr,struct cifs_open_info_data * data,struct super_block * sb)833 static void smb311_posix_info_to_fattr(struct cifs_fattr *fattr,
834 struct cifs_open_info_data *data,
835 struct super_block *sb)
836 {
837 struct smb311_posix_qinfo *info = &data->posix_fi;
838 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
839 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
840
841 memset(fattr, 0, sizeof(*fattr));
842
843 /* no fattr->flags to set */
844 fattr->cf_cifsattrs = le32_to_cpu(info->DosAttributes);
845 fattr->cf_uniqueid = le64_to_cpu(info->Inode);
846
847 if (info->LastAccessTime)
848 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
849 else
850 ktime_get_coarse_real_ts64(&fattr->cf_atime);
851
852 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
853 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
854
855 if (data->adjust_tz) {
856 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
857 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
858 }
859
860 /*
861 * The srv fs device id is overridden on network mount so setting
862 * @fattr->cf_rdev isn't needed here.
863 */
864 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
865 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
866 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
867 fattr->cf_nlink = le32_to_cpu(info->HardLinks);
868 fattr->cf_mode = wire_mode_to_posix(le32_to_cpu(info->Mode),
869 fattr->cf_cifsattrs & ATTR_DIRECTORY);
870
871 if (cifs_open_data_reparse(data) &&
872 cifs_reparse_point_to_fattr(cifs_sb, fattr, data))
873 goto out_reparse;
874
875 fattr->cf_dtype = S_DT(fattr->cf_mode);
876
877 out_reparse:
878 if (S_ISLNK(fattr->cf_mode)) {
879 if (likely(data->symlink_target))
880 fattr->cf_eof = strnlen(data->symlink_target, PATH_MAX);
881 fattr->cf_symlink_target = data->symlink_target;
882 data->symlink_target = NULL;
883 }
884 sid_to_id(cifs_sb, &data->posix_owner, fattr, SIDOWNER);
885 sid_to_id(cifs_sb, &data->posix_group, fattr, SIDGROUP);
886
887 cifs_dbg(FYI, "POSIX query info: mode 0x%x uniqueid 0x%llx nlink %d\n",
888 fattr->cf_mode, fattr->cf_uniqueid, fattr->cf_nlink);
889 }
890
cifs_open_info_to_fattr(struct cifs_fattr * fattr,struct cifs_open_info_data * data,struct super_block * sb)891 static void cifs_open_info_to_fattr(struct cifs_fattr *fattr,
892 struct cifs_open_info_data *data,
893 struct super_block *sb)
894 {
895 struct smb2_file_all_info *info = &data->fi;
896 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
897 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
898
899 memset(fattr, 0, sizeof(*fattr));
900 fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
901 if (info->DeletePending)
902 fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
903
904 if (info->LastAccessTime)
905 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
906 else
907 ktime_get_coarse_real_ts64(&fattr->cf_atime);
908
909 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
910 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
911
912 if (data->adjust_tz) {
913 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
914 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
915 }
916
917 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
918 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
919 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
920 fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
921 fattr->cf_uid = cifs_sb->ctx->linux_uid;
922 fattr->cf_gid = cifs_sb->ctx->linux_gid;
923
924 fattr->cf_mode = cifs_sb->ctx->file_mode;
925 if (cifs_open_data_reparse(data) &&
926 cifs_reparse_point_to_fattr(cifs_sb, fattr, data))
927 goto out_reparse;
928
929 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
930 fattr->cf_mode = S_IFDIR | cifs_sb->ctx->dir_mode;
931 fattr->cf_dtype = DT_DIR;
932 /*
933 * Server can return wrong NumberOfLinks value for directories
934 * when Unix extensions are disabled - fake it.
935 */
936 if (!tcon->unix_ext)
937 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
938 } else {
939 fattr->cf_mode = S_IFREG | cifs_sb->ctx->file_mode;
940 fattr->cf_dtype = DT_REG;
941
942 /*
943 * Don't accept zero nlink from non-unix servers unless
944 * delete is pending. Instead mark it as unknown.
945 */
946 if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
947 !info->DeletePending) {
948 cifs_dbg(VFS, "bogus file nlink value %u\n",
949 fattr->cf_nlink);
950 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
951 }
952 }
953
954 /* clear write bits if ATTR_READONLY is set */
955 if (fattr->cf_cifsattrs & ATTR_READONLY)
956 fattr->cf_mode &= ~(S_IWUGO);
957
958 out_reparse:
959 if (S_ISLNK(fattr->cf_mode)) {
960 if (likely(data->symlink_target))
961 fattr->cf_eof = strnlen(data->symlink_target, PATH_MAX);
962 fattr->cf_symlink_target = data->symlink_target;
963 data->symlink_target = NULL;
964 }
965 }
966
967 static int
cifs_get_file_info(struct file * filp)968 cifs_get_file_info(struct file *filp)
969 {
970 int rc;
971 unsigned int xid;
972 struct cifs_open_info_data data = {};
973 struct cifs_fattr fattr;
974 struct inode *inode = file_inode(filp);
975 struct cifsFileInfo *cfile = filp->private_data;
976 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
977 struct TCP_Server_Info *server = tcon->ses->server;
978 struct dentry *dentry = filp->f_path.dentry;
979 void *page = alloc_dentry_path();
980 const unsigned char *path;
981
982 if (!server->ops->query_file_info) {
983 free_dentry_path(page);
984 return -ENOSYS;
985 }
986
987 xid = get_xid();
988 rc = server->ops->query_file_info(xid, tcon, cfile, &data);
989 switch (rc) {
990 case 0:
991 /* TODO: add support to query reparse tag */
992 data.adjust_tz = false;
993 if (data.symlink_target) {
994 data.reparse_point = true;
995 data.reparse.tag = IO_REPARSE_TAG_SYMLINK;
996 }
997 path = build_path_from_dentry(dentry, page);
998 if (IS_ERR(path)) {
999 rc = PTR_ERR(path);
1000 goto cgfi_exit;
1001 }
1002 cifs_open_info_to_fattr(&fattr, &data, inode->i_sb);
1003 if (fattr.cf_flags & CIFS_FATTR_DELETE_PENDING)
1004 cifs_mark_open_handles_for_deleted_file(inode, path);
1005 break;
1006 case -EREMOTE:
1007 cifs_create_junction_fattr(&fattr, inode->i_sb);
1008 break;
1009 case -EOPNOTSUPP:
1010 case -EINVAL:
1011 /*
1012 * FIXME: legacy server -- fall back to path-based call?
1013 * for now, just skip revalidating and mark inode for
1014 * immediate reval.
1015 */
1016 rc = 0;
1017 CIFS_I(inode)->time = 0;
1018 goto cgfi_exit;
1019 default:
1020 goto cgfi_exit;
1021 }
1022
1023 /*
1024 * don't bother with SFU junk here -- just mark inode as needing
1025 * revalidation.
1026 */
1027 fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
1028 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
1029 /* if filetype is different, return error */
1030 rc = cifs_fattr_to_inode(inode, &fattr, false);
1031 cgfi_exit:
1032 cifs_free_open_info(&data);
1033 free_dentry_path(page);
1034 free_xid(xid);
1035 return rc;
1036 }
1037
1038 /* Simple function to return a 64 bit hash of string. Rarely called */
simple_hashstr(const char * str)1039 static __u64 simple_hashstr(const char *str)
1040 {
1041 const __u64 hash_mult = 1125899906842597ULL; /* a big enough prime */
1042 __u64 hash = 0;
1043
1044 while (*str)
1045 hash = (hash + (__u64) *str++) * hash_mult;
1046
1047 return hash;
1048 }
1049
1050 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1051 /**
1052 * cifs_backup_query_path_info - SMB1 fallback code to get ino
1053 *
1054 * Fallback code to get file metadata when we don't have access to
1055 * full_path (EACCES) and have backup creds.
1056 *
1057 * @xid: transaction id used to identify original request in logs
1058 * @tcon: information about the server share we have mounted
1059 * @sb: the superblock stores info such as disk space available
1060 * @full_path: name of the file we are getting the metadata for
1061 * @resp_buf: will be set to cifs resp buf and needs to be freed with
1062 * cifs_buf_release() when done with @data
1063 * @data: will be set to search info result buffer
1064 */
1065 static int
cifs_backup_query_path_info(int xid,struct cifs_tcon * tcon,struct super_block * sb,const char * full_path,void ** resp_buf,FILE_ALL_INFO ** data)1066 cifs_backup_query_path_info(int xid,
1067 struct cifs_tcon *tcon,
1068 struct super_block *sb,
1069 const char *full_path,
1070 void **resp_buf,
1071 FILE_ALL_INFO **data)
1072 {
1073 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1074 struct cifs_search_info info = {0};
1075 u16 flags;
1076 int rc;
1077
1078 *resp_buf = NULL;
1079 info.endOfSearch = false;
1080 if (tcon->unix_ext)
1081 info.info_level = SMB_FIND_FILE_UNIX;
1082 else if ((tcon->ses->capabilities &
1083 tcon->ses->server->vals->cap_nt_find) == 0)
1084 info.info_level = SMB_FIND_FILE_INFO_STANDARD;
1085 else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
1086 info.info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
1087 else /* no srvino useful for fallback to some netapp */
1088 info.info_level = SMB_FIND_FILE_DIRECTORY_INFO;
1089
1090 flags = CIFS_SEARCH_CLOSE_ALWAYS |
1091 CIFS_SEARCH_CLOSE_AT_END |
1092 CIFS_SEARCH_BACKUP_SEARCH;
1093
1094 rc = CIFSFindFirst(xid, tcon, full_path,
1095 cifs_sb, NULL, flags, &info, false);
1096 if (rc)
1097 return rc;
1098
1099 *resp_buf = (void *)info.ntwrk_buf_start;
1100 *data = (FILE_ALL_INFO *)info.srch_entries_start;
1101 return 0;
1102 }
1103 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1104
cifs_set_fattr_ino(int xid,struct cifs_tcon * tcon,struct super_block * sb,struct inode ** inode,const char * full_path,struct cifs_open_info_data * data,struct cifs_fattr * fattr)1105 static void cifs_set_fattr_ino(int xid, struct cifs_tcon *tcon, struct super_block *sb,
1106 struct inode **inode, const char *full_path,
1107 struct cifs_open_info_data *data, struct cifs_fattr *fattr)
1108 {
1109 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1110 struct TCP_Server_Info *server = tcon->ses->server;
1111 int rc;
1112
1113 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)) {
1114 if (*inode)
1115 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
1116 else
1117 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1118 return;
1119 }
1120
1121 /*
1122 * If we have an inode pass a NULL tcon to ensure we don't
1123 * make a round trip to the server. This only works for SMB2+.
1124 */
1125 rc = server->ops->get_srv_inum(xid, *inode ? NULL : tcon, cifs_sb, full_path,
1126 &fattr->cf_uniqueid, data);
1127 if (rc) {
1128 /*
1129 * If that fails reuse existing ino or generate one
1130 * and disable server ones
1131 */
1132 if (*inode)
1133 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
1134 else {
1135 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1136 cifs_autodisable_serverino(cifs_sb);
1137 }
1138 return;
1139 }
1140
1141 /* If no errors, check for zero root inode (invalid) */
1142 if (fattr->cf_uniqueid == 0 && strlen(full_path) == 0) {
1143 cifs_dbg(FYI, "Invalid (0) inodenum\n");
1144 if (*inode) {
1145 /* reuse */
1146 fattr->cf_uniqueid = CIFS_I(*inode)->uniqueid;
1147 } else {
1148 /* make an ino by hashing the UNC */
1149 fattr->cf_flags |= CIFS_FATTR_FAKE_ROOT_INO;
1150 fattr->cf_uniqueid = simple_hashstr(tcon->tree_name);
1151 }
1152 }
1153 }
1154
is_inode_cache_good(struct inode * ino)1155 static inline bool is_inode_cache_good(struct inode *ino)
1156 {
1157 return ino && CIFS_CACHE_READ(CIFS_I(ino)) && CIFS_I(ino)->time != 0;
1158 }
1159
reparse_info_to_fattr(struct cifs_open_info_data * data,struct super_block * sb,const unsigned int xid,struct cifs_tcon * tcon,const char * full_path,struct cifs_fattr * fattr)1160 static int reparse_info_to_fattr(struct cifs_open_info_data *data,
1161 struct super_block *sb,
1162 const unsigned int xid,
1163 struct cifs_tcon *tcon,
1164 const char *full_path,
1165 struct cifs_fattr *fattr)
1166 {
1167 struct TCP_Server_Info *server = tcon->ses->server;
1168 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1169 struct kvec rsp_iov, *iov = NULL;
1170 int rsp_buftype = CIFS_NO_BUFFER;
1171 u32 tag = data->reparse.tag;
1172 int rc = 0;
1173
1174 if (!tag && server->ops->query_reparse_point) {
1175 rc = server->ops->query_reparse_point(xid, tcon, cifs_sb,
1176 full_path, &tag,
1177 &rsp_iov, &rsp_buftype);
1178 if (!rc)
1179 iov = &rsp_iov;
1180 } else if (data->reparse.io.buftype != CIFS_NO_BUFFER &&
1181 data->reparse.io.iov.iov_base) {
1182 iov = &data->reparse.io.iov;
1183 }
1184
1185 rc = -EOPNOTSUPP;
1186 data->reparse.tag = tag;
1187 if (!data->reparse.tag) {
1188 if (server->ops->query_symlink) {
1189 rc = server->ops->query_symlink(xid, tcon,
1190 cifs_sb, full_path,
1191 &data->symlink_target);
1192 }
1193 if (rc == -EOPNOTSUPP)
1194 data->reparse.tag = IO_REPARSE_TAG_INTERNAL;
1195 }
1196
1197 switch (data->reparse.tag) {
1198 case 0: /* SMB1 symlink */
1199 break;
1200 case IO_REPARSE_TAG_INTERNAL:
1201 rc = 0;
1202 if (le32_to_cpu(data->fi.Attributes) & ATTR_DIRECTORY) {
1203 cifs_create_junction_fattr(fattr, sb);
1204 goto out;
1205 }
1206 break;
1207 default:
1208 /* Check for cached reparse point data */
1209 if (data->symlink_target || data->reparse.buf) {
1210 rc = 0;
1211 } else if (iov && server->ops->get_reparse_point_buffer) {
1212 struct reparse_data_buffer *reparse_buf;
1213 u32 reparse_len;
1214
1215 reparse_buf = server->ops->get_reparse_point_buffer(iov, &reparse_len);
1216 rc = parse_reparse_point(reparse_buf, reparse_len,
1217 cifs_sb, full_path, data);
1218 /*
1219 * If the reparse point was not handled but it is the
1220 * name surrogate which points to directory, then treat
1221 * is as a new mount point. Name surrogate reparse point
1222 * represents another named entity in the system.
1223 */
1224 if (rc == -EOPNOTSUPP &&
1225 IS_REPARSE_TAG_NAME_SURROGATE(data->reparse.tag) &&
1226 (le32_to_cpu(data->fi.Attributes) & ATTR_DIRECTORY)) {
1227 rc = 0;
1228 cifs_create_junction_fattr(fattr, sb);
1229 goto out;
1230 }
1231 /*
1232 * If the reparse point is unsupported by the Linux SMB
1233 * client then let it process by the SMB server. So mask
1234 * the -EOPNOTSUPP error code. This will allow Linux SMB
1235 * client to send SMB OPEN request to server. If server
1236 * does not support this reparse point too then server
1237 * will return error during open the path.
1238 */
1239 if (rc == -EOPNOTSUPP)
1240 rc = 0;
1241 }
1242
1243 if (data->reparse.tag == IO_REPARSE_TAG_SYMLINK && !rc) {
1244 bool directory = le32_to_cpu(data->fi.Attributes) & ATTR_DIRECTORY;
1245 rc = smb2_fix_symlink_target_type(&data->symlink_target, directory, cifs_sb);
1246 }
1247 break;
1248 }
1249
1250 if (tcon->posix_extensions)
1251 smb311_posix_info_to_fattr(fattr, data, sb);
1252 else
1253 cifs_open_info_to_fattr(fattr, data, sb);
1254 out:
1255 fattr->cf_cifstag = data->reparse.tag;
1256 free_rsp_buf(rsp_buftype, rsp_iov.iov_base);
1257 return rc;
1258 }
1259
cifs_get_fattr(struct cifs_open_info_data * data,struct super_block * sb,int xid,const struct cifs_fid * fid,struct cifs_fattr * fattr,struct inode ** inode,const char * full_path)1260 static int cifs_get_fattr(struct cifs_open_info_data *data,
1261 struct super_block *sb, int xid,
1262 const struct cifs_fid *fid,
1263 struct cifs_fattr *fattr,
1264 struct inode **inode,
1265 const char *full_path)
1266 {
1267 struct cifs_open_info_data tmp_data = {};
1268 struct cifs_tcon *tcon;
1269 struct TCP_Server_Info *server;
1270 struct tcon_link *tlink;
1271 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1272 void *smb1_backup_rsp_buf = NULL;
1273 int rc = 0;
1274 int tmprc = 0;
1275
1276 tlink = cifs_sb_tlink(cifs_sb);
1277 if (IS_ERR(tlink))
1278 return PTR_ERR(tlink);
1279 tcon = tlink_tcon(tlink);
1280 server = tcon->ses->server;
1281
1282 /*
1283 * 1. Fetch file metadata if not provided (data)
1284 */
1285
1286 if (!data) {
1287 rc = server->ops->query_path_info(xid, tcon, cifs_sb,
1288 full_path, &tmp_data);
1289 data = &tmp_data;
1290 }
1291
1292 /*
1293 * 2. Convert it to internal cifs metadata (fattr)
1294 */
1295
1296 switch (rc) {
1297 case 0:
1298 /*
1299 * If the file is a reparse point, it is more complicated
1300 * since we have to check if its reparse tag matches a known
1301 * special file type e.g. symlink or fifo or char etc.
1302 */
1303 if (cifs_open_data_reparse(data)) {
1304 rc = reparse_info_to_fattr(data, sb, xid, tcon,
1305 full_path, fattr);
1306 } else {
1307 cifs_open_info_to_fattr(fattr, data, sb);
1308 }
1309 if (!rc && *inode &&
1310 (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING))
1311 cifs_mark_open_handles_for_deleted_file(*inode, full_path);
1312 break;
1313 case -EREMOTE:
1314 /* DFS link, no metadata available on this server */
1315 cifs_create_junction_fattr(fattr, sb);
1316 rc = 0;
1317 break;
1318 case -EACCES:
1319 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1320 /*
1321 * perm errors, try again with backup flags if possible
1322 *
1323 * For SMB2 and later the backup intent flag
1324 * is already sent if needed on open and there
1325 * is no path based FindFirst operation to use
1326 * to retry with
1327 */
1328 if (backup_cred(cifs_sb) && is_smb1_server(server)) {
1329 /* for easier reading */
1330 FILE_ALL_INFO *fi;
1331 FILE_DIRECTORY_INFO *fdi;
1332 SEARCH_ID_FULL_DIR_INFO *si;
1333
1334 rc = cifs_backup_query_path_info(xid, tcon, sb,
1335 full_path,
1336 &smb1_backup_rsp_buf,
1337 &fi);
1338 if (rc)
1339 goto out;
1340
1341 move_cifs_info_to_smb2(&data->fi, fi);
1342 fdi = (FILE_DIRECTORY_INFO *)fi;
1343 si = (SEARCH_ID_FULL_DIR_INFO *)fi;
1344
1345 cifs_dir_info_to_fattr(fattr, fdi, cifs_sb);
1346 fattr->cf_uniqueid = le64_to_cpu(si->UniqueId);
1347 /* uniqueid set, skip get inum step */
1348 goto handle_mnt_opt;
1349 } else {
1350 /* nothing we can do, bail out */
1351 goto out;
1352 }
1353 #else
1354 goto out;
1355 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1356 break;
1357 default:
1358 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1359 goto out;
1360 }
1361
1362 /*
1363 * 3. Get or update inode number (fattr->cf_uniqueid)
1364 */
1365
1366 cifs_set_fattr_ino(xid, tcon, sb, inode, full_path, data, fattr);
1367
1368 /*
1369 * 4. Tweak fattr based on mount options
1370 */
1371 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1372 handle_mnt_opt:
1373 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1374 /* query for SFU type info if supported and needed */
1375 if ((fattr->cf_cifsattrs & ATTR_SYSTEM) &&
1376 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) {
1377 tmprc = cifs_sfu_type(fattr, full_path, cifs_sb, xid);
1378 if (tmprc)
1379 cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
1380 }
1381
1382 /* fill in 0777 bits from ACL */
1383 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) {
1384 rc = cifs_acl_to_fattr(cifs_sb, fattr, *inode,
1385 true, full_path, fid);
1386 if (rc == -EREMOTE)
1387 rc = 0;
1388 if (rc) {
1389 cifs_dbg(FYI, "%s: Get mode from SID failed. rc=%d\n",
1390 __func__, rc);
1391 goto out;
1392 }
1393 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
1394 rc = cifs_acl_to_fattr(cifs_sb, fattr, *inode,
1395 false, full_path, fid);
1396 if (rc == -EREMOTE)
1397 rc = 0;
1398 if (rc) {
1399 cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
1400 __func__, rc);
1401 goto out;
1402 }
1403 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
1404 /* fill in remaining high mode bits e.g. SUID, VTX */
1405 cifs_sfu_mode(fattr, full_path, cifs_sb, xid);
1406 else if (!(tcon->posix_extensions))
1407 /* clear write bits if ATTR_READONLY is set */
1408 if (fattr->cf_cifsattrs & ATTR_READONLY)
1409 fattr->cf_mode &= ~(S_IWUGO);
1410
1411
1412 /* check for Minshall+French symlinks */
1413 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1414 tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
1415 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1416 }
1417
1418 out:
1419 cifs_buf_release(smb1_backup_rsp_buf);
1420 cifs_put_tlink(tlink);
1421 cifs_free_open_info(&tmp_data);
1422 return rc;
1423 }
1424
cifs_get_inode_info(struct inode ** inode,const char * full_path,struct cifs_open_info_data * data,struct super_block * sb,int xid,const struct cifs_fid * fid)1425 int cifs_get_inode_info(struct inode **inode,
1426 const char *full_path,
1427 struct cifs_open_info_data *data,
1428 struct super_block *sb, int xid,
1429 const struct cifs_fid *fid)
1430 {
1431 struct cifs_fattr fattr = {};
1432 int rc;
1433
1434 if (!data && is_inode_cache_good(*inode)) {
1435 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1436 return 0;
1437 }
1438
1439 rc = cifs_get_fattr(data, sb, xid, fid, &fattr, inode, full_path);
1440 if (rc)
1441 goto out;
1442
1443 rc = update_inode_info(sb, &fattr, inode);
1444 out:
1445 kfree(fattr.cf_symlink_target);
1446 return rc;
1447 }
1448
smb311_posix_get_fattr(struct cifs_open_info_data * data,struct cifs_fattr * fattr,const char * full_path,struct super_block * sb,const unsigned int xid)1449 static int smb311_posix_get_fattr(struct cifs_open_info_data *data,
1450 struct cifs_fattr *fattr,
1451 const char *full_path,
1452 struct super_block *sb,
1453 const unsigned int xid)
1454 {
1455 struct cifs_open_info_data tmp_data = {};
1456 struct TCP_Server_Info *server;
1457 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1458 struct cifs_tcon *tcon;
1459 struct tcon_link *tlink;
1460 int tmprc;
1461 int rc = 0;
1462
1463 tlink = cifs_sb_tlink(cifs_sb);
1464 if (IS_ERR(tlink))
1465 return PTR_ERR(tlink);
1466 tcon = tlink_tcon(tlink);
1467 server = tcon->ses->server;
1468
1469 /*
1470 * 1. Fetch file metadata if not provided (data)
1471 */
1472 if (!data) {
1473 rc = server->ops->query_path_info(xid, tcon, cifs_sb,
1474 full_path, &tmp_data);
1475 data = &tmp_data;
1476 }
1477
1478 /*
1479 * 2. Convert it to internal cifs metadata (fattr)
1480 */
1481
1482 switch (rc) {
1483 case 0:
1484 if (cifs_open_data_reparse(data)) {
1485 rc = reparse_info_to_fattr(data, sb, xid, tcon,
1486 full_path, fattr);
1487 } else {
1488 smb311_posix_info_to_fattr(fattr, data, sb);
1489 }
1490 break;
1491 case -EREMOTE:
1492 /* DFS link, no metadata available on this server */
1493 cifs_create_junction_fattr(fattr, sb);
1494 rc = 0;
1495 break;
1496 case -EACCES:
1497 /*
1498 * For SMB2 and later the backup intent flag
1499 * is already sent if needed on open and there
1500 * is no path based FindFirst operation to use
1501 * to retry with so nothing we can do, bail out
1502 */
1503 goto out;
1504 default:
1505 cifs_dbg(FYI, "%s: unhandled err rc %d\n", __func__, rc);
1506 goto out;
1507 }
1508
1509 /*
1510 * 3. Tweak fattr based on mount options
1511 */
1512 /* check for Minshall+French symlinks */
1513 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
1514 tmprc = check_mf_symlink(xid, tcon, cifs_sb, fattr, full_path);
1515 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1516 }
1517
1518 out:
1519 cifs_put_tlink(tlink);
1520 cifs_free_open_info(data);
1521 return rc;
1522 }
1523
smb311_posix_get_inode_info(struct inode ** inode,const char * full_path,struct cifs_open_info_data * data,struct super_block * sb,const unsigned int xid)1524 int smb311_posix_get_inode_info(struct inode **inode,
1525 const char *full_path,
1526 struct cifs_open_info_data *data,
1527 struct super_block *sb,
1528 const unsigned int xid)
1529 {
1530 struct cifs_fattr fattr = {};
1531 int rc;
1532
1533 if (!data && is_inode_cache_good(*inode)) {
1534 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
1535 return 0;
1536 }
1537
1538 rc = smb311_posix_get_fattr(data, &fattr, full_path, sb, xid);
1539 if (rc)
1540 goto out;
1541
1542 rc = update_inode_info(sb, &fattr, inode);
1543 if (!rc && fattr.cf_flags & CIFS_FATTR_DELETE_PENDING)
1544 cifs_mark_open_handles_for_deleted_file(*inode, full_path);
1545 out:
1546 kfree(fattr.cf_symlink_target);
1547 return rc;
1548 }
1549
1550 static const struct inode_operations cifs_ipc_inode_ops = {
1551 .lookup = cifs_lookup,
1552 };
1553
1554 static int
cifs_find_inode(struct inode * inode,void * opaque)1555 cifs_find_inode(struct inode *inode, void *opaque)
1556 {
1557 struct cifs_fattr *fattr = opaque;
1558
1559 /* [!] The compared values must be the same in struct cifs_fscache_inode_key. */
1560
1561 /* don't match inode with different uniqueid */
1562 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
1563 return 0;
1564
1565 /* use createtime like an i_generation field */
1566 if (CIFS_I(inode)->createtime != fattr->cf_createtime)
1567 return 0;
1568
1569 /* don't match inode of different type */
1570 if (inode_wrong_type(inode, fattr->cf_mode))
1571 return 0;
1572
1573 /* if it's not a directory or has no dentries, then flag it */
1574 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
1575 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
1576
1577 return 1;
1578 }
1579
1580 static int
cifs_init_inode(struct inode * inode,void * opaque)1581 cifs_init_inode(struct inode *inode, void *opaque)
1582 {
1583 struct cifs_fattr *fattr = opaque;
1584
1585 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
1586 CIFS_I(inode)->createtime = fattr->cf_createtime;
1587 return 0;
1588 }
1589
1590 /*
1591 * walk dentry list for an inode and report whether it has aliases that
1592 * are hashed. We use this to determine if a directory inode can actually
1593 * be used.
1594 */
1595 static bool
inode_has_hashed_dentries(struct inode * inode)1596 inode_has_hashed_dentries(struct inode *inode)
1597 {
1598 struct dentry *dentry;
1599
1600 spin_lock(&inode->i_lock);
1601 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
1602 if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
1603 spin_unlock(&inode->i_lock);
1604 return true;
1605 }
1606 }
1607 spin_unlock(&inode->i_lock);
1608 return false;
1609 }
1610
1611 /* Given fattrs, get a corresponding inode */
1612 struct inode *
cifs_iget(struct super_block * sb,struct cifs_fattr * fattr)1613 cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
1614 {
1615 unsigned long hash;
1616 struct inode *inode;
1617
1618 retry_iget5_locked:
1619 cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
1620
1621 /* hash down to 32-bits on 32-bit arch */
1622 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
1623
1624 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
1625 if (inode) {
1626 /* was there a potentially problematic inode collision? */
1627 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
1628 fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
1629
1630 if (inode_has_hashed_dentries(inode)) {
1631 cifs_autodisable_serverino(CIFS_SB(sb));
1632 iput(inode);
1633 fattr->cf_uniqueid = iunique(sb, ROOT_I);
1634 goto retry_iget5_locked;
1635 }
1636 }
1637
1638 /* can't fail - see cifs_find_inode() */
1639 cifs_fattr_to_inode(inode, fattr, false);
1640 if (sb->s_flags & SB_NOATIME)
1641 inode->i_flags |= S_NOATIME | S_NOCMTIME;
1642 if (inode_state_read_once(inode) & I_NEW) {
1643 inode->i_ino = hash;
1644 cifs_fscache_get_inode_cookie(inode);
1645 unlock_new_inode(inode);
1646 }
1647 }
1648
1649 return inode;
1650 }
1651
1652 /* gets root inode */
cifs_root_iget(struct super_block * sb)1653 struct inode *cifs_root_iget(struct super_block *sb)
1654 {
1655 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1656 struct cifs_fattr fattr = {};
1657 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
1658 struct inode *inode = NULL;
1659 unsigned int xid;
1660 char *path = NULL;
1661 int len;
1662 int rc;
1663
1664 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
1665 && cifs_sb->prepath) {
1666 len = strlen(cifs_sb->prepath);
1667 path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
1668 if (path == NULL)
1669 return ERR_PTR(-ENOMEM);
1670 path[0] = '/';
1671 memcpy(path+1, cifs_sb->prepath, len);
1672 } else {
1673 path = kstrdup("", GFP_KERNEL);
1674 if (path == NULL)
1675 return ERR_PTR(-ENOMEM);
1676 }
1677
1678 xid = get_xid();
1679 if (tcon->unix_ext) {
1680 rc = cifs_get_unix_fattr(path, sb, &fattr, &inode, xid);
1681 /* some servers mistakenly claim POSIX support */
1682 if (rc != -EOPNOTSUPP)
1683 goto iget_root;
1684 cifs_dbg(VFS, "server does not support POSIX extensions\n");
1685 tcon->unix_ext = false;
1686 }
1687
1688 convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
1689 if (tcon->posix_extensions)
1690 rc = smb311_posix_get_fattr(NULL, &fattr, path, sb, xid);
1691 else
1692 rc = cifs_get_fattr(NULL, sb, xid, NULL, &fattr, &inode, path);
1693
1694 iget_root:
1695 if (!rc) {
1696 if (fattr.cf_flags & CIFS_FATTR_JUNCTION) {
1697 fattr.cf_flags &= ~CIFS_FATTR_JUNCTION;
1698 cifs_autodisable_serverino(cifs_sb);
1699 }
1700 inode = cifs_iget(sb, &fattr);
1701 }
1702
1703 if (!inode) {
1704 inode = ERR_PTR(rc);
1705 goto out;
1706 }
1707
1708 if (!rc && fattr.cf_flags & CIFS_FATTR_DELETE_PENDING)
1709 cifs_mark_open_handles_for_deleted_file(inode, path);
1710
1711 if (rc && tcon->pipe) {
1712 cifs_dbg(FYI, "ipc connection - fake read inode\n");
1713 spin_lock(&inode->i_lock);
1714 inode->i_mode |= S_IFDIR;
1715 set_nlink(inode, 2);
1716 inode->i_op = &cifs_ipc_inode_ops;
1717 inode->i_fop = &simple_dir_operations;
1718 inode->i_uid = cifs_sb->ctx->linux_uid;
1719 inode->i_gid = cifs_sb->ctx->linux_gid;
1720 spin_unlock(&inode->i_lock);
1721 } else if (rc) {
1722 iget_failed(inode);
1723 inode = ERR_PTR(rc);
1724 }
1725
1726 out:
1727 kfree(path);
1728 free_xid(xid);
1729 kfree(fattr.cf_symlink_target);
1730 return inode;
1731 }
1732
1733 int
cifs_set_file_info(struct inode * inode,struct iattr * attrs,unsigned int xid,const char * full_path,__u32 dosattr)1734 cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
1735 const char *full_path, __u32 dosattr)
1736 {
1737 bool set_time = false;
1738 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1739 struct TCP_Server_Info *server;
1740 FILE_BASIC_INFO info_buf;
1741
1742 if (attrs == NULL)
1743 return -EINVAL;
1744
1745 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1746 if (!server->ops->set_file_info)
1747 return -ENOSYS;
1748
1749 info_buf.Pad = 0;
1750
1751 if (attrs->ia_valid & ATTR_ATIME) {
1752 set_time = true;
1753 info_buf.LastAccessTime =
1754 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1755 } else
1756 info_buf.LastAccessTime = 0;
1757
1758 if (attrs->ia_valid & ATTR_MTIME) {
1759 set_time = true;
1760 info_buf.LastWriteTime =
1761 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1762 } else
1763 info_buf.LastWriteTime = 0;
1764
1765 /*
1766 * Samba throws this field away, but windows may actually use it.
1767 * Do not set ctime unless other time stamps are changed explicitly
1768 * (i.e. by utimes()) since we would then have a mix of client and
1769 * server times.
1770 */
1771 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
1772 cifs_dbg(FYI, "CIFS - CTIME changed\n");
1773 info_buf.ChangeTime =
1774 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1775 } else
1776 info_buf.ChangeTime = 0;
1777
1778 info_buf.CreationTime = 0; /* don't change */
1779 info_buf.Attributes = cpu_to_le32(dosattr);
1780
1781 return server->ops->set_file_info(inode, full_path, &info_buf, xid);
1782 }
1783
1784 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1785 /*
1786 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
1787 * and rename it to a random name that hopefully won't conflict with
1788 * anything else.
1789 */
1790 int
cifs_rename_pending_delete(const char * full_path,struct dentry * dentry,const unsigned int xid)1791 cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1792 const unsigned int xid)
1793 {
1794 int oplock = 0;
1795 int rc;
1796 struct cifs_fid fid;
1797 struct cifs_open_parms oparms;
1798 struct inode *inode = d_inode(dentry);
1799 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1800 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1801 struct tcon_link *tlink;
1802 struct cifs_tcon *tcon;
1803 __u32 dosattr, origattr;
1804 FILE_BASIC_INFO *info_buf = NULL;
1805
1806 tlink = cifs_sb_tlink(cifs_sb);
1807 if (IS_ERR(tlink))
1808 return PTR_ERR(tlink);
1809 tcon = tlink_tcon(tlink);
1810
1811 /*
1812 * We cannot rename the file if the server doesn't support
1813 * CAP_INFOLEVEL_PASSTHRU
1814 */
1815 if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1816 rc = -EBUSY;
1817 goto out;
1818 }
1819
1820 oparms = (struct cifs_open_parms) {
1821 .tcon = tcon,
1822 .cifs_sb = cifs_sb,
1823 .desired_access = DELETE | FILE_WRITE_ATTRIBUTES,
1824 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
1825 .disposition = FILE_OPEN,
1826 .path = full_path,
1827 .fid = &fid,
1828 };
1829
1830 rc = CIFS_open(xid, &oparms, &oplock, NULL);
1831 if (rc != 0)
1832 goto out;
1833
1834 origattr = cifsInode->cifsAttrs;
1835 if (origattr == 0)
1836 origattr |= ATTR_NORMAL;
1837
1838 dosattr = origattr & ~ATTR_READONLY;
1839 if (dosattr == 0)
1840 dosattr |= ATTR_NORMAL;
1841 dosattr |= ATTR_HIDDEN;
1842
1843 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1844 if (dosattr != origattr) {
1845 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1846 if (info_buf == NULL) {
1847 rc = -ENOMEM;
1848 goto out_close;
1849 }
1850 info_buf->Attributes = cpu_to_le32(dosattr);
1851 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1852 current->tgid);
1853 /* although we would like to mark the file hidden
1854 if that fails we will still try to rename it */
1855 if (!rc)
1856 cifsInode->cifsAttrs = dosattr;
1857 else
1858 dosattr = origattr; /* since not able to change them */
1859 }
1860
1861 /* rename the file */
1862 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1863 cifs_sb->local_nls,
1864 cifs_remap(cifs_sb));
1865 if (rc != 0) {
1866 rc = -EBUSY;
1867 goto undo_setattr;
1868 }
1869
1870 /* try to set DELETE_ON_CLOSE */
1871 if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
1872 rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
1873 current->tgid);
1874 /*
1875 * some samba versions return -ENOENT when we try to set the
1876 * file disposition here. Likely a samba bug, but work around
1877 * it for now. This means that some cifsXXX files may hang
1878 * around after they shouldn't.
1879 *
1880 * BB: remove this hack after more servers have the fix
1881 */
1882 if (rc == -ENOENT)
1883 rc = 0;
1884 else if (rc != 0) {
1885 rc = -EBUSY;
1886 goto undo_rename;
1887 }
1888 set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
1889 }
1890
1891 out_close:
1892 CIFSSMBClose(xid, tcon, fid.netfid);
1893 out:
1894 kfree(info_buf);
1895 cifs_put_tlink(tlink);
1896 return rc;
1897
1898 /*
1899 * reset everything back to the original state. Don't bother
1900 * dealing with errors here since we can't do anything about
1901 * them anyway.
1902 */
1903 undo_rename:
1904 CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
1905 cifs_sb->local_nls, cifs_remap(cifs_sb));
1906 undo_setattr:
1907 if (dosattr != origattr) {
1908 info_buf->Attributes = cpu_to_le32(origattr);
1909 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
1910 current->tgid))
1911 cifsInode->cifsAttrs = origattr;
1912 }
1913
1914 goto out_close;
1915 }
1916 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
1917
1918 /* copied from fs/nfs/dir.c with small changes */
1919 static void
cifs_drop_nlink(struct inode * inode)1920 cifs_drop_nlink(struct inode *inode)
1921 {
1922 spin_lock(&inode->i_lock);
1923 if (inode->i_nlink > 0)
1924 drop_nlink(inode);
1925 spin_unlock(&inode->i_lock);
1926 }
1927
1928 /*
1929 * If d_inode(dentry) is null (usually meaning the cached dentry
1930 * is a negative dentry) then we would attempt a standard SMB delete, but
1931 * if that fails we can not attempt the fall back mechanisms on EACCES
1932 * but will return the EACCES to the caller. Note that the VFS does not call
1933 * unlink on negative dentries currently.
1934 */
__cifs_unlink(struct inode * dir,struct dentry * dentry,bool sillyrename)1935 static int __cifs_unlink(struct inode *dir, struct dentry *dentry, bool sillyrename)
1936 {
1937 int rc = 0;
1938 unsigned int xid;
1939 const char *full_path;
1940 void *page;
1941 struct inode *inode = d_inode(dentry);
1942 struct cifsInodeInfo *cifs_inode;
1943 struct super_block *sb = dir->i_sb;
1944 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1945 struct tcon_link *tlink;
1946 struct cifs_tcon *tcon;
1947 __u32 dosattr = 0, origattr = 0;
1948 struct TCP_Server_Info *server;
1949 struct iattr *attrs = NULL;
1950 bool rehash = false;
1951
1952 cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
1953
1954 if (unlikely(cifs_forced_shutdown(cifs_sb)))
1955 return -EIO;
1956
1957 /* Unhash dentry in advance to prevent any concurrent opens */
1958 spin_lock(&dentry->d_lock);
1959 if (!d_unhashed(dentry)) {
1960 __d_drop(dentry);
1961 rehash = true;
1962 }
1963 spin_unlock(&dentry->d_lock);
1964
1965 tlink = cifs_sb_tlink(cifs_sb);
1966 if (IS_ERR(tlink))
1967 return PTR_ERR(tlink);
1968 tcon = tlink_tcon(tlink);
1969 server = tcon->ses->server;
1970
1971 xid = get_xid();
1972 page = alloc_dentry_path();
1973
1974 if (tcon->nodelete) {
1975 rc = -EACCES;
1976 goto unlink_out;
1977 }
1978
1979 /* Unlink can be called from rename so we can not take the
1980 * sb->s_vfs_rename_mutex here */
1981 full_path = build_path_from_dentry(dentry, page);
1982 if (IS_ERR(full_path)) {
1983 rc = PTR_ERR(full_path);
1984 goto unlink_out;
1985 }
1986
1987 netfs_wait_for_outstanding_io(inode);
1988 cifs_close_deferred_file_under_dentry(tcon, dentry);
1989 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
1990 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1991 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1992 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
1993 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1994 cifs_remap(cifs_sb));
1995 cifs_dbg(FYI, "posix del rc %d\n", rc);
1996 if ((rc == 0) || (rc == -ENOENT))
1997 goto psx_del_no_retry;
1998 }
1999 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2000
2001 retry_std_delete:
2002 if (!server->ops->unlink) {
2003 rc = -ENOSYS;
2004 goto psx_del_no_retry;
2005 }
2006
2007 /* For SMB2+, if the file is open, we always perform a silly rename.
2008 *
2009 * We check for d_count() right after calling
2010 * cifs_close_deferred_file_under_dentry() to make sure that the
2011 * dentry's refcount gets dropped in case the file had any deferred
2012 * close.
2013 */
2014 if (!sillyrename && server->vals->protocol_id > SMB10_PROT_ID) {
2015 spin_lock(&dentry->d_lock);
2016 if (d_count(dentry) > 1)
2017 sillyrename = true;
2018 spin_unlock(&dentry->d_lock);
2019 }
2020
2021 if (sillyrename)
2022 rc = -EBUSY;
2023 else
2024 rc = server->ops->unlink(xid, tcon, full_path, cifs_sb, dentry);
2025
2026 psx_del_no_retry:
2027 if (!rc) {
2028 if (inode) {
2029 cifs_mark_open_handles_for_deleted_file(inode, full_path);
2030 cifs_drop_nlink(inode);
2031 }
2032 } else if (rc == -ENOENT) {
2033 if (simple_positive(dentry))
2034 d_delete(dentry);
2035 } else if (rc == -EBUSY) {
2036 if (server->ops->rename_pending_delete) {
2037 rc = server->ops->rename_pending_delete(full_path,
2038 dentry, xid);
2039 if (rc == 0) {
2040 cifs_mark_open_handles_for_deleted_file(inode, full_path);
2041 cifs_drop_nlink(inode);
2042 }
2043 }
2044 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
2045 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
2046 if (attrs == NULL) {
2047 rc = -ENOMEM;
2048 goto out_reval;
2049 }
2050
2051 /* try to reset dos attributes */
2052 cifs_inode = CIFS_I(inode);
2053 origattr = cifs_inode->cifsAttrs;
2054 if (origattr == 0)
2055 origattr |= ATTR_NORMAL;
2056 dosattr = origattr & ~ATTR_READONLY;
2057 if (dosattr == 0)
2058 dosattr |= ATTR_NORMAL;
2059 dosattr |= ATTR_HIDDEN;
2060
2061 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
2062 if (rc != 0)
2063 goto out_reval;
2064
2065 goto retry_std_delete;
2066 }
2067
2068 /* undo the setattr if we errored out and it's needed */
2069 if (rc != 0 && dosattr != 0)
2070 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
2071
2072 out_reval:
2073 if (inode) {
2074 cifs_inode = CIFS_I(inode);
2075 cifs_inode->time = 0; /* will force revalidate to get info
2076 when needed */
2077 inode_set_ctime_current(inode);
2078 }
2079 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
2080 cifs_inode = CIFS_I(dir);
2081 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
2082 unlink_out:
2083 free_dentry_path(page);
2084 kfree(attrs);
2085 free_xid(xid);
2086 cifs_put_tlink(tlink);
2087 if (rehash)
2088 d_rehash(dentry);
2089 return rc;
2090 }
2091
cifs_unlink(struct inode * dir,struct dentry * dentry)2092 int cifs_unlink(struct inode *dir, struct dentry *dentry)
2093 {
2094 return __cifs_unlink(dir, dentry, false);
2095 }
2096
2097 static int
cifs_mkdir_qinfo(struct inode * parent,struct dentry * dentry,umode_t mode,const char * full_path,struct cifs_sb_info * cifs_sb,struct cifs_tcon * tcon,const unsigned int xid)2098 cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
2099 const char *full_path, struct cifs_sb_info *cifs_sb,
2100 struct cifs_tcon *tcon, const unsigned int xid)
2101 {
2102 int rc = 0;
2103 struct inode *inode = NULL;
2104
2105 if (tcon->posix_extensions) {
2106 rc = smb311_posix_get_inode_info(&inode, full_path,
2107 NULL, parent->i_sb, xid);
2108 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2109 } else if (tcon->unix_ext) {
2110 rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
2111 xid);
2112 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2113 } else {
2114 rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
2115 xid, NULL);
2116 }
2117
2118 if (rc)
2119 return rc;
2120
2121 if (!S_ISDIR(inode->i_mode)) {
2122 /*
2123 * mkdir succeeded, but another client has managed to remove the
2124 * sucker and replace it with non-directory. Return success,
2125 * but don't leave the child in dcache.
2126 */
2127 iput(inode);
2128 d_drop(dentry);
2129 return 0;
2130 }
2131 /*
2132 * setting nlink not necessary except in cases where we failed to get it
2133 * from the server or was set bogus. Also, since this is a brand new
2134 * inode, no need to grab the i_lock before setting the i_nlink.
2135 */
2136 if (inode->i_nlink < 2)
2137 set_nlink(inode, 2);
2138 mode &= ~current_umask();
2139 /* must turn on setgid bit if parent dir has it */
2140 if (parent->i_mode & S_ISGID)
2141 mode |= S_ISGID;
2142
2143 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2144 if (tcon->unix_ext) {
2145 struct cifs_unix_set_info_args args = {
2146 .mode = mode,
2147 .ctime = NO_CHANGE_64,
2148 .atime = NO_CHANGE_64,
2149 .mtime = NO_CHANGE_64,
2150 .device = 0,
2151 };
2152 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
2153 args.uid = current_fsuid();
2154 if (parent->i_mode & S_ISGID)
2155 args.gid = parent->i_gid;
2156 else
2157 args.gid = current_fsgid();
2158 } else {
2159 args.uid = INVALID_UID; /* no change */
2160 args.gid = INVALID_GID; /* no change */
2161 }
2162 CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
2163 cifs_sb->local_nls,
2164 cifs_remap(cifs_sb));
2165 } else {
2166 #else
2167 {
2168 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2169 struct TCP_Server_Info *server = tcon->ses->server;
2170 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
2171 (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
2172 server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
2173 tcon, xid);
2174 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
2175 inode->i_mode = (mode | S_IFDIR);
2176
2177 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
2178 inode->i_uid = current_fsuid();
2179 if (inode->i_mode & S_ISGID)
2180 inode->i_gid = parent->i_gid;
2181 else
2182 inode->i_gid = current_fsgid();
2183 }
2184 }
2185 d_instantiate(dentry, inode);
2186 return 0;
2187 }
2188
2189 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2190 static int
2191 cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
2192 const char *full_path, struct cifs_sb_info *cifs_sb,
2193 struct cifs_tcon *tcon, const unsigned int xid)
2194 {
2195 int rc = 0;
2196 u32 oplock = 0;
2197 FILE_UNIX_BASIC_INFO *info = NULL;
2198 struct inode *newinode = NULL;
2199 struct cifs_fattr fattr;
2200
2201 info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
2202 if (info == NULL) {
2203 rc = -ENOMEM;
2204 goto posix_mkdir_out;
2205 }
2206
2207 mode &= ~current_umask();
2208 rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
2209 NULL /* netfid */, info, &oplock, full_path,
2210 cifs_sb->local_nls, cifs_remap(cifs_sb));
2211 if (rc == -EOPNOTSUPP)
2212 goto posix_mkdir_out;
2213 else if (rc) {
2214 cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
2215 d_drop(dentry);
2216 goto posix_mkdir_out;
2217 }
2218
2219 if (info->Type == cpu_to_le32(-1))
2220 /* no return info, go query for it */
2221 goto posix_mkdir_get_info;
2222 /*
2223 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
2224 * need to set uid/gid.
2225 */
2226
2227 cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
2228 cifs_fill_uniqueid(inode->i_sb, &fattr);
2229 newinode = cifs_iget(inode->i_sb, &fattr);
2230 if (!newinode)
2231 goto posix_mkdir_get_info;
2232
2233 d_instantiate(dentry, newinode);
2234
2235 #ifdef CONFIG_CIFS_DEBUG2
2236 cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
2237 dentry, dentry, newinode);
2238
2239 if (newinode->i_nlink != 2)
2240 cifs_dbg(FYI, "unexpected number of links %d\n",
2241 newinode->i_nlink);
2242 #endif
2243
2244 posix_mkdir_out:
2245 kfree(info);
2246 return rc;
2247 posix_mkdir_get_info:
2248 rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
2249 xid);
2250 goto posix_mkdir_out;
2251 }
2252 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2253
2254 struct dentry *cifs_mkdir(struct mnt_idmap *idmap, struct inode *inode,
2255 struct dentry *direntry, umode_t mode)
2256 {
2257 int rc = 0;
2258 unsigned int xid;
2259 struct cifs_sb_info *cifs_sb;
2260 struct tcon_link *tlink;
2261 struct cifs_tcon *tcon;
2262 struct TCP_Server_Info *server;
2263 const char *full_path;
2264 void *page;
2265
2266 cifs_dbg(FYI, "In cifs_mkdir, mode = %04ho inode = 0x%p\n",
2267 mode, inode);
2268
2269 cifs_sb = CIFS_SB(inode->i_sb);
2270 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2271 return ERR_PTR(-EIO);
2272 tlink = cifs_sb_tlink(cifs_sb);
2273 if (IS_ERR(tlink))
2274 return ERR_CAST(tlink);
2275 tcon = tlink_tcon(tlink);
2276
2277 xid = get_xid();
2278
2279 page = alloc_dentry_path();
2280 full_path = build_path_from_dentry(direntry, page);
2281 if (IS_ERR(full_path)) {
2282 rc = PTR_ERR(full_path);
2283 goto mkdir_out;
2284 }
2285
2286 server = tcon->ses->server;
2287
2288 if ((server->ops->posix_mkdir) && (tcon->posix_extensions)) {
2289 rc = server->ops->posix_mkdir(xid, inode, mode, tcon, full_path,
2290 cifs_sb);
2291 d_drop(direntry); /* for time being always refresh inode info */
2292 goto mkdir_out;
2293 }
2294
2295 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2296 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
2297 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
2298 rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
2299 tcon, xid);
2300 if (rc != -EOPNOTSUPP)
2301 goto mkdir_out;
2302 }
2303 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2304
2305 if (!server->ops->mkdir) {
2306 rc = -ENOSYS;
2307 goto mkdir_out;
2308 }
2309
2310 /* BB add setting the equivalent of mode via CreateX w/ACLs */
2311 rc = server->ops->mkdir(xid, inode, mode, tcon, full_path, cifs_sb);
2312 if (rc) {
2313 cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
2314 d_drop(direntry);
2315 goto mkdir_out;
2316 }
2317
2318 /* TODO: skip this for smb2/smb3 */
2319 rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
2320 xid);
2321 mkdir_out:
2322 /*
2323 * Force revalidate to get parent dir info when needed since cached
2324 * attributes are invalid now.
2325 */
2326 CIFS_I(inode)->time = 0;
2327 free_dentry_path(page);
2328 free_xid(xid);
2329 cifs_put_tlink(tlink);
2330 return ERR_PTR(rc);
2331 }
2332
2333 int cifs_rmdir(struct inode *inode, struct dentry *direntry)
2334 {
2335 int rc = 0;
2336 unsigned int xid;
2337 struct cifs_sb_info *cifs_sb;
2338 struct tcon_link *tlink;
2339 struct cifs_tcon *tcon;
2340 struct TCP_Server_Info *server;
2341 const char *full_path;
2342 void *page = alloc_dentry_path();
2343 struct cifsInodeInfo *cifsInode;
2344
2345 cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
2346
2347 xid = get_xid();
2348
2349 full_path = build_path_from_dentry(direntry, page);
2350 if (IS_ERR(full_path)) {
2351 rc = PTR_ERR(full_path);
2352 goto rmdir_exit;
2353 }
2354
2355 cifs_sb = CIFS_SB(inode->i_sb);
2356 if (unlikely(cifs_forced_shutdown(cifs_sb))) {
2357 rc = -EIO;
2358 goto rmdir_exit;
2359 }
2360
2361 tlink = cifs_sb_tlink(cifs_sb);
2362 if (IS_ERR(tlink)) {
2363 rc = PTR_ERR(tlink);
2364 goto rmdir_exit;
2365 }
2366 tcon = tlink_tcon(tlink);
2367 server = tcon->ses->server;
2368
2369 if (!server->ops->rmdir) {
2370 rc = -ENOSYS;
2371 cifs_put_tlink(tlink);
2372 goto rmdir_exit;
2373 }
2374
2375 if (tcon->nodelete) {
2376 rc = -EACCES;
2377 cifs_put_tlink(tlink);
2378 goto rmdir_exit;
2379 }
2380
2381 rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
2382 cifs_put_tlink(tlink);
2383
2384 cifsInode = CIFS_I(d_inode(direntry));
2385
2386 if (!rc) {
2387 set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
2388 spin_lock(&d_inode(direntry)->i_lock);
2389 i_size_write(d_inode(direntry), 0);
2390 clear_nlink(d_inode(direntry));
2391 spin_unlock(&d_inode(direntry)->i_lock);
2392 }
2393
2394 /* force revalidate to go get info when needed */
2395 cifsInode->time = 0;
2396
2397 cifsInode = CIFS_I(inode);
2398 /*
2399 * Force revalidate to get parent dir info when needed since cached
2400 * attributes are invalid now.
2401 */
2402 cifsInode->time = 0;
2403
2404 inode_set_ctime_current(d_inode(direntry));
2405 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
2406
2407 rmdir_exit:
2408 free_dentry_path(page);
2409 free_xid(xid);
2410 return rc;
2411 }
2412
2413 static int
2414 cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
2415 const char *from_path, struct dentry *to_dentry,
2416 const char *to_path)
2417 {
2418 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
2419 struct tcon_link *tlink;
2420 struct cifs_tcon *tcon;
2421 struct TCP_Server_Info *server;
2422 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2423 struct cifs_fid fid;
2424 struct cifs_open_parms oparms;
2425 int oplock;
2426 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2427 int rc;
2428
2429 tlink = cifs_sb_tlink(cifs_sb);
2430 if (IS_ERR(tlink))
2431 return PTR_ERR(tlink);
2432 tcon = tlink_tcon(tlink);
2433 server = tcon->ses->server;
2434
2435 if (!server->ops->rename) {
2436 rc = -ENOSYS;
2437 goto do_rename_exit;
2438 }
2439
2440 /* try path-based rename first */
2441 rc = server->ops->rename(xid, tcon, from_dentry,
2442 from_path, to_path, cifs_sb);
2443
2444 /*
2445 * Don't bother with rename by filehandle unless file is busy and
2446 * source. Note that cross directory moves do not work with
2447 * rename by filehandle to various Windows servers.
2448 */
2449 if (rc == 0 || rc != -EBUSY)
2450 goto do_rename_exit;
2451
2452 /* Don't fall back to using SMB on SMB 2+ mount */
2453 if (server->vals->protocol_id != 0)
2454 goto do_rename_exit;
2455
2456 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2457 /* open-file renames don't work across directories */
2458 if (to_dentry->d_parent != from_dentry->d_parent)
2459 goto do_rename_exit;
2460
2461 /*
2462 * CIFSSMBRenameOpenFile() uses SMB_SET_FILE_RENAME_INFORMATION
2463 * which is SMB PASSTHROUGH level.
2464 */
2465 if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU))
2466 goto do_rename_exit;
2467
2468 oparms = (struct cifs_open_parms) {
2469 .tcon = tcon,
2470 .cifs_sb = cifs_sb,
2471 /* open the file to be renamed -- we need DELETE perms */
2472 .desired_access = DELETE,
2473 .create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR),
2474 .disposition = FILE_OPEN,
2475 .path = from_path,
2476 .fid = &fid,
2477 };
2478
2479 rc = CIFS_open(xid, &oparms, &oplock, NULL);
2480 if (rc == 0) {
2481 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
2482 (const char *) to_dentry->d_name.name,
2483 cifs_sb->local_nls, cifs_remap(cifs_sb));
2484 CIFSSMBClose(xid, tcon, fid.netfid);
2485 }
2486 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2487 do_rename_exit:
2488 if (rc == 0)
2489 d_move(from_dentry, to_dentry);
2490 cifs_put_tlink(tlink);
2491 return rc;
2492 }
2493
2494 int
2495 cifs_rename2(struct mnt_idmap *idmap, struct inode *source_dir,
2496 struct dentry *source_dentry, struct inode *target_dir,
2497 struct dentry *target_dentry, unsigned int flags)
2498 {
2499 const char *from_name, *to_name;
2500 struct TCP_Server_Info *server;
2501 void *page1, *page2;
2502 struct cifs_sb_info *cifs_sb;
2503 struct tcon_link *tlink;
2504 struct cifs_tcon *tcon;
2505 bool rehash = false;
2506 unsigned int xid;
2507 int rc, tmprc;
2508 int retry_count = 0;
2509 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
2510 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2511 FILE_UNIX_BASIC_INFO *info_buf_target;
2512 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2513
2514 if (flags & ~RENAME_NOREPLACE)
2515 return -EINVAL;
2516
2517 cifs_sb = CIFS_SB(source_dir->i_sb);
2518 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2519 return -EIO;
2520
2521 /*
2522 * Prevent any concurrent opens on the target by unhashing the dentry.
2523 * VFS already unhashes the target when renaming directories.
2524 */
2525 if (d_is_positive(target_dentry) && !d_is_dir(target_dentry)) {
2526 if (!d_unhashed(target_dentry)) {
2527 d_drop(target_dentry);
2528 rehash = true;
2529 }
2530 }
2531
2532 tlink = cifs_sb_tlink(cifs_sb);
2533 if (IS_ERR(tlink))
2534 return PTR_ERR(tlink);
2535 tcon = tlink_tcon(tlink);
2536 server = tcon->ses->server;
2537
2538 page1 = alloc_dentry_path();
2539 page2 = alloc_dentry_path();
2540 xid = get_xid();
2541
2542 from_name = build_path_from_dentry(source_dentry, page1);
2543 if (IS_ERR(from_name)) {
2544 rc = PTR_ERR(from_name);
2545 goto cifs_rename_exit;
2546 }
2547
2548 to_name = build_path_from_dentry(target_dentry, page2);
2549 if (IS_ERR(to_name)) {
2550 rc = PTR_ERR(to_name);
2551 goto cifs_rename_exit;
2552 }
2553
2554 cifs_close_deferred_file_under_dentry(tcon, source_dentry);
2555 if (d_inode(target_dentry) != NULL) {
2556 netfs_wait_for_outstanding_io(d_inode(target_dentry));
2557 cifs_close_deferred_file_under_dentry(tcon, target_dentry);
2558 }
2559
2560 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2561 to_name);
2562
2563 if (rc == -EACCES) {
2564 while (retry_count < 3) {
2565 cifs_close_all_deferred_files(tcon);
2566 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
2567 to_name);
2568 if (rc != -EACCES)
2569 break;
2570 retry_count++;
2571 }
2572 }
2573
2574 if (!rc)
2575 rehash = false;
2576 /*
2577 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
2578 */
2579 if (flags & RENAME_NOREPLACE)
2580 goto cifs_rename_exit;
2581
2582 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2583 if (rc == -EEXIST && tcon->unix_ext) {
2584 /*
2585 * Are src and dst hardlinks of same inode? We can only tell
2586 * with unix extensions enabled.
2587 */
2588 info_buf_source =
2589 kmalloc_array(2, sizeof(FILE_UNIX_BASIC_INFO),
2590 GFP_KERNEL);
2591 if (info_buf_source == NULL) {
2592 rc = -ENOMEM;
2593 goto cifs_rename_exit;
2594 }
2595
2596 info_buf_target = info_buf_source + 1;
2597 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
2598 info_buf_source,
2599 cifs_sb->local_nls,
2600 cifs_remap(cifs_sb));
2601 if (tmprc != 0)
2602 goto unlink_target;
2603
2604 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
2605 info_buf_target,
2606 cifs_sb->local_nls,
2607 cifs_remap(cifs_sb));
2608
2609 if (tmprc == 0 && (info_buf_source->UniqueId ==
2610 info_buf_target->UniqueId)) {
2611 /* same file, POSIX says that this is a noop */
2612 rc = 0;
2613 goto cifs_rename_exit;
2614 }
2615 }
2616 /*
2617 * else ... BB we could add the same check for Windows by
2618 * checking the UniqueId via FILE_INTERNAL_INFO
2619 */
2620
2621 unlink_target:
2622 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2623 if (d_really_is_positive(target_dentry)) {
2624 if (!rc) {
2625 struct inode *inode = d_inode(target_dentry);
2626 /*
2627 * Samba and ksmbd servers allow renaming a target
2628 * directory that is open, so make sure to update
2629 * ->i_nlink and then mark it as delete pending.
2630 */
2631 if (S_ISDIR(inode->i_mode)) {
2632 drop_cached_dir_by_name(xid, tcon, to_name, cifs_sb);
2633 spin_lock(&inode->i_lock);
2634 i_size_write(inode, 0);
2635 clear_nlink(inode);
2636 spin_unlock(&inode->i_lock);
2637 set_bit(CIFS_INO_DELETE_PENDING, &CIFS_I(inode)->flags);
2638 CIFS_I(inode)->time = 0; /* force reval */
2639 inode_set_ctime_current(inode);
2640 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
2641 }
2642 } else if (rc == -EACCES || rc == -EEXIST) {
2643 /*
2644 * Rename failed, possibly due to a busy target.
2645 * Retry it by unliking the target first.
2646 */
2647 if (d_is_dir(target_dentry)) {
2648 tmprc = cifs_rmdir(target_dir, target_dentry);
2649 } else {
2650 tmprc = __cifs_unlink(target_dir, target_dentry,
2651 server->vals->protocol_id > SMB10_PROT_ID);
2652 }
2653 if (tmprc) {
2654 /*
2655 * Some servers will return STATUS_ACCESS_DENIED
2656 * or STATUS_DIRECTORY_NOT_EMPTY when failing to
2657 * rename a non-empty directory. Make sure to
2658 * propagate the appropriate error back to
2659 * userspace.
2660 */
2661 if (tmprc == -EEXIST || tmprc == -ENOTEMPTY)
2662 rc = tmprc;
2663 goto cifs_rename_exit;
2664 }
2665 rc = cifs_do_rename(xid, source_dentry, from_name,
2666 target_dentry, to_name);
2667 if (!rc)
2668 rehash = false;
2669 }
2670 }
2671
2672 /* force revalidate to go get info when needed */
2673 CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
2674
2675 cifs_rename_exit:
2676 if (rehash)
2677 d_rehash(target_dentry);
2678 kfree(info_buf_source);
2679 free_dentry_path(page2);
2680 free_dentry_path(page1);
2681 free_xid(xid);
2682 cifs_put_tlink(tlink);
2683 return rc;
2684 }
2685
2686 static bool
2687 cifs_dentry_needs_reval(struct dentry *dentry)
2688 {
2689 struct inode *inode = d_inode(dentry);
2690 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2691 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2692 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2693 struct cached_fid *cfid = NULL;
2694
2695 if (test_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags))
2696 return false;
2697 if (cifs_i->time == 0)
2698 return true;
2699
2700 if (CIFS_CACHE_READ(cifs_i))
2701 return false;
2702
2703 if (!lookupCacheEnabled)
2704 return true;
2705
2706 if (!open_cached_dir_by_dentry(tcon, dentry->d_parent, &cfid)) {
2707 if (cifs_i->time > cfid->time) {
2708 close_cached_dir(cfid);
2709 return false;
2710 }
2711 close_cached_dir(cfid);
2712 }
2713 /*
2714 * depending on inode type, check if attribute caching disabled for
2715 * files or directories
2716 */
2717 if (S_ISDIR(inode->i_mode)) {
2718 if (!cifs_sb->ctx->acdirmax)
2719 return true;
2720 if (!time_in_range(jiffies, cifs_i->time,
2721 cifs_i->time + cifs_sb->ctx->acdirmax))
2722 return true;
2723 } else { /* file */
2724 if (!cifs_sb->ctx->acregmax)
2725 return true;
2726 if (!time_in_range(jiffies, cifs_i->time,
2727 cifs_i->time + cifs_sb->ctx->acregmax))
2728 return true;
2729 }
2730
2731 /* hardlinked files w/ noserverino get "special" treatment */
2732 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
2733 S_ISREG(inode->i_mode) && inode->i_nlink != 1)
2734 return true;
2735
2736 return false;
2737 }
2738
2739 /**
2740 * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
2741 *
2742 * @key: currently unused
2743 * @mode: the task state to sleep in
2744 */
2745 static int
2746 cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
2747 {
2748 schedule();
2749 if (signal_pending_state(mode, current))
2750 return -ERESTARTSYS;
2751 return 0;
2752 }
2753
2754 int
2755 cifs_revalidate_mapping(struct inode *inode)
2756 {
2757 int rc;
2758 struct cifsInodeInfo *cifs_inode = CIFS_I(inode);
2759 unsigned long *flags = &cifs_inode->flags;
2760 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2761
2762 /* swapfiles are not supposed to be shared */
2763 if (IS_SWAPFILE(inode))
2764 return 0;
2765
2766 rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
2767 TASK_KILLABLE|TASK_FREEZABLE_UNSAFE);
2768 if (rc)
2769 return rc;
2770
2771 if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
2772 /* for cache=singleclient, do not invalidate */
2773 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RW_CACHE)
2774 goto skip_invalidate;
2775
2776 cifs_inode->netfs.zero_point = cifs_inode->netfs.remote_i_size;
2777 rc = filemap_invalidate_inode(inode, true, 0, LLONG_MAX);
2778 if (rc) {
2779 cifs_dbg(VFS, "%s: invalidate inode %p failed with rc %d\n",
2780 __func__, inode, rc);
2781 set_bit(CIFS_INO_INVALID_MAPPING, flags);
2782 }
2783 }
2784
2785 skip_invalidate:
2786 clear_bit_unlock(CIFS_INO_LOCK, flags);
2787 smp_mb__after_atomic();
2788 wake_up_bit(flags, CIFS_INO_LOCK);
2789
2790 return rc;
2791 }
2792
2793 int
2794 cifs_zap_mapping(struct inode *inode)
2795 {
2796 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
2797 return cifs_revalidate_mapping(inode);
2798 }
2799
2800 int cifs_revalidate_file_attr(struct file *filp)
2801 {
2802 int rc = 0;
2803 struct dentry *dentry = file_dentry(filp);
2804 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2805 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
2806 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2807
2808 if (!cifs_dentry_needs_reval(dentry))
2809 return rc;
2810
2811 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
2812 if (tlink_tcon(cfile->tlink)->unix_ext)
2813 rc = cifs_get_file_info_unix(filp);
2814 else
2815 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
2816 rc = cifs_get_file_info(filp);
2817
2818 return rc;
2819 }
2820
2821 int cifs_revalidate_dentry_attr(struct dentry *dentry)
2822 {
2823 unsigned int xid;
2824 int rc = 0;
2825 struct inode *inode = d_inode(dentry);
2826 struct super_block *sb = dentry->d_sb;
2827 const char *full_path;
2828 void *page;
2829 int count = 0;
2830
2831 if (inode == NULL)
2832 return -ENOENT;
2833
2834 if (!cifs_dentry_needs_reval(dentry))
2835 return rc;
2836
2837 xid = get_xid();
2838
2839 page = alloc_dentry_path();
2840 full_path = build_path_from_dentry(dentry, page);
2841 if (IS_ERR(full_path)) {
2842 rc = PTR_ERR(full_path);
2843 goto out;
2844 }
2845
2846 cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
2847 full_path, inode, icount_read(inode),
2848 dentry, cifs_get_time(dentry), jiffies);
2849
2850 again:
2851 if (cifs_sb_master_tcon(CIFS_SB(sb))->posix_extensions) {
2852 rc = smb311_posix_get_inode_info(&inode, full_path,
2853 NULL, sb, xid);
2854 } else if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext) {
2855 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
2856 } else {
2857 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
2858 xid, NULL);
2859 }
2860 if (rc == -EAGAIN && count++ < 10)
2861 goto again;
2862 out:
2863 free_dentry_path(page);
2864 free_xid(xid);
2865
2866 return rc;
2867 }
2868
2869 int cifs_revalidate_file(struct file *filp)
2870 {
2871 int rc;
2872 struct inode *inode = file_inode(filp);
2873
2874 rc = cifs_revalidate_file_attr(filp);
2875 if (rc)
2876 return rc;
2877
2878 return cifs_revalidate_mapping(inode);
2879 }
2880
2881 /* revalidate a dentry's inode attributes */
2882 int cifs_revalidate_dentry(struct dentry *dentry)
2883 {
2884 int rc;
2885 struct inode *inode = d_inode(dentry);
2886
2887 rc = cifs_revalidate_dentry_attr(dentry);
2888 if (rc)
2889 return rc;
2890
2891 return cifs_revalidate_mapping(inode);
2892 }
2893
2894 int cifs_getattr(struct mnt_idmap *idmap, const struct path *path,
2895 struct kstat *stat, u32 request_mask, unsigned int flags)
2896 {
2897 struct dentry *dentry = path->dentry;
2898 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
2899 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2900 struct inode *inode = d_inode(dentry);
2901 int rc;
2902
2903 if (unlikely(cifs_forced_shutdown(CIFS_SB(inode->i_sb))))
2904 return -EIO;
2905
2906 /*
2907 * We need to be sure that all dirty pages are written and the server
2908 * has actual ctime, mtime and file length.
2909 */
2910 if ((request_mask & (STATX_CTIME | STATX_MTIME | STATX_SIZE | STATX_BLOCKS)) &&
2911 !CIFS_CACHE_READ(CIFS_I(inode)) &&
2912 inode->i_mapping && inode->i_mapping->nrpages != 0) {
2913 rc = filemap_fdatawait(inode->i_mapping);
2914 if (rc) {
2915 mapping_set_error(inode->i_mapping, rc);
2916 return rc;
2917 }
2918 }
2919
2920 if ((flags & AT_STATX_SYNC_TYPE) == AT_STATX_FORCE_SYNC)
2921 CIFS_I(inode)->time = 0; /* force revalidate */
2922
2923 /*
2924 * If the caller doesn't require syncing, only sync if
2925 * necessary (e.g. due to earlier truncate or setattr
2926 * invalidating the cached metadata)
2927 */
2928 if (((flags & AT_STATX_SYNC_TYPE) != AT_STATX_DONT_SYNC) ||
2929 (CIFS_I(inode)->time == 0)) {
2930 rc = cifs_revalidate_dentry_attr(dentry);
2931 if (rc)
2932 return rc;
2933 }
2934
2935 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
2936 stat->blksize = cifs_sb->ctx->bsize;
2937 stat->ino = CIFS_I(inode)->uniqueid;
2938
2939 /* old CIFS Unix Extensions doesn't return create time */
2940 if (CIFS_I(inode)->createtime) {
2941 stat->result_mask |= STATX_BTIME;
2942 stat->btime =
2943 cifs_NTtimeToUnix(cpu_to_le64(CIFS_I(inode)->createtime));
2944 }
2945
2946 stat->attributes_mask |= (STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED);
2947 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_COMPRESSED)
2948 stat->attributes |= STATX_ATTR_COMPRESSED;
2949 if (CIFS_I(inode)->cifsAttrs & FILE_ATTRIBUTE_ENCRYPTED)
2950 stat->attributes |= STATX_ATTR_ENCRYPTED;
2951
2952 /*
2953 * If on a multiuser mount without unix extensions or cifsacl being
2954 * enabled, and the admin hasn't overridden them, set the ownership
2955 * to the fsuid/fsgid of the current process.
2956 */
2957 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
2958 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
2959 !tcon->unix_ext) {
2960 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
2961 stat->uid = current_fsuid();
2962 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
2963 stat->gid = current_fsgid();
2964 }
2965 return 0;
2966 }
2967
2968 int cifs_fiemap(struct inode *inode, struct fiemap_extent_info *fei, u64 start,
2969 u64 len)
2970 {
2971 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
2972 struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_i->netfs.inode.i_sb);
2973 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2974 struct TCP_Server_Info *server = tcon->ses->server;
2975 struct cifsFileInfo *cfile;
2976 int rc;
2977
2978 if (unlikely(cifs_forced_shutdown(cifs_sb)))
2979 return -EIO;
2980
2981 /*
2982 * We need to be sure that all dirty pages are written as they
2983 * might fill holes on the server.
2984 */
2985 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
2986 inode->i_mapping->nrpages != 0) {
2987 rc = filemap_fdatawait(inode->i_mapping);
2988 if (rc) {
2989 mapping_set_error(inode->i_mapping, rc);
2990 return rc;
2991 }
2992 }
2993
2994 cfile = find_readable_file(cifs_i, false);
2995 if (cfile == NULL)
2996 return -EINVAL;
2997
2998 if (server->ops->fiemap) {
2999 rc = server->ops->fiemap(tcon, cfile, fei, start, len);
3000 cifsFileInfo_put(cfile);
3001 return rc;
3002 }
3003
3004 cifsFileInfo_put(cfile);
3005 return -EOPNOTSUPP;
3006 }
3007
3008 void cifs_setsize(struct inode *inode, loff_t offset)
3009 {
3010 spin_lock(&inode->i_lock);
3011 i_size_write(inode, offset);
3012 spin_unlock(&inode->i_lock);
3013 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
3014 truncate_pagecache(inode, offset);
3015 netfs_wait_for_outstanding_io(inode);
3016 }
3017
3018 int cifs_file_set_size(const unsigned int xid, struct dentry *dentry,
3019 const char *full_path, struct cifsFileInfo *open_file,
3020 loff_t size)
3021 {
3022 struct inode *inode = d_inode(dentry);
3023 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3024 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
3025 struct tcon_link *tlink = NULL;
3026 struct cifs_tcon *tcon = NULL;
3027 struct TCP_Server_Info *server;
3028 int rc = -EINVAL;
3029
3030 /*
3031 * To avoid spurious oplock breaks from server, in the case of
3032 * inodes that we already have open, avoid doing path based
3033 * setting of file size if we can do it by handle.
3034 * This keeps our caching token (oplock) and avoids timeouts
3035 * when the local oplock break takes longer to flush
3036 * writebehind data than the SMB timeout for the SetPathInfo
3037 * request would allow
3038 */
3039 if (open_file && (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE)) {
3040 tcon = tlink_tcon(open_file->tlink);
3041 server = tcon->ses->server;
3042 rc = server->ops->set_file_size(xid, tcon,
3043 open_file,
3044 size, false);
3045 cifs_dbg(FYI, "%s: set_file_size: rc = %d\n", __func__, rc);
3046 } else {
3047 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
3048 if (open_file) {
3049 tcon = tlink_tcon(open_file->tlink);
3050 server = tcon->ses->server;
3051 rc = server->ops->set_file_size(xid, tcon,
3052 open_file,
3053 size, false);
3054 cifs_dbg(FYI, "%s: set_file_size: rc = %d\n", __func__, rc);
3055 cifsFileInfo_put(open_file);
3056 }
3057 }
3058
3059 if (!rc)
3060 goto set_size_out;
3061
3062 if (tcon == NULL) {
3063 tlink = cifs_sb_tlink(cifs_sb);
3064 if (IS_ERR(tlink))
3065 return PTR_ERR(tlink);
3066 tcon = tlink_tcon(tlink);
3067 server = tcon->ses->server;
3068 }
3069
3070 /*
3071 * Set file size by pathname rather than by handle either because no
3072 * valid, writeable file handle for it was found or because there was
3073 * an error setting it by handle.
3074 */
3075 rc = server->ops->set_path_size(xid, tcon, full_path, size,
3076 cifs_sb, false, dentry);
3077 cifs_dbg(FYI, "%s: SetEOF by path (setattrs) rc = %d\n", __func__, rc);
3078 cifs_put_tlink(tlink);
3079
3080 set_size_out:
3081 if (rc == 0) {
3082 netfs_resize_file(&cifsInode->netfs, size, true);
3083 cifs_setsize(inode, size);
3084 /*
3085 * i_blocks is not related to (i_size / i_blksize), but instead
3086 * 512 byte (2**9) size is required for calculating num blocks.
3087 * Until we can query the server for actual allocation size,
3088 * this is best estimate we have for blocks allocated for a file
3089 * Number of blocks must be rounded up so size 1 is not 0 blocks
3090 */
3091 inode->i_blocks = (512 - 1 + size) >> 9;
3092 }
3093
3094 return rc;
3095 }
3096
3097 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3098 static int
3099 cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
3100 {
3101 int rc;
3102 unsigned int xid;
3103 const char *full_path;
3104 void *page = alloc_dentry_path();
3105 struct inode *inode = d_inode(direntry);
3106 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
3107 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3108 struct tcon_link *tlink;
3109 struct cifs_tcon *pTcon;
3110 struct cifs_unix_set_info_args *args = NULL;
3111 struct cifsFileInfo *open_file = NULL;
3112
3113 cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
3114 direntry, attrs->ia_valid);
3115
3116 xid = get_xid();
3117
3118 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
3119 attrs->ia_valid |= ATTR_FORCE;
3120
3121 rc = setattr_prepare(&nop_mnt_idmap, direntry, attrs);
3122 if (rc < 0)
3123 goto out;
3124
3125 if (attrs->ia_valid & ATTR_FILE)
3126 open_file = attrs->ia_file->private_data;
3127
3128 full_path = build_path_from_dentry(direntry, page);
3129 if (IS_ERR(full_path)) {
3130 rc = PTR_ERR(full_path);
3131 goto out;
3132 }
3133
3134 /*
3135 * Attempt to flush data before changing attributes. We need to do
3136 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
3137 * ownership or mode then we may also need to do this. Here, we take
3138 * the safe way out and just do the flush on all setattr requests. If
3139 * the flush returns error, store it to report later and continue.
3140 *
3141 * BB: This should be smarter. Why bother flushing pages that
3142 * will be truncated anyway? Also, should we error out here if
3143 * the flush returns error?
3144 */
3145 rc = filemap_write_and_wait(inode->i_mapping);
3146 if (is_interrupt_error(rc)) {
3147 rc = -ERESTARTSYS;
3148 goto out;
3149 }
3150
3151 mapping_set_error(inode->i_mapping, rc);
3152 rc = 0;
3153
3154 if (attrs->ia_valid & ATTR_SIZE) {
3155 rc = cifs_file_set_size(xid, direntry, full_path,
3156 open_file, attrs->ia_size);
3157 if (rc != 0)
3158 goto out;
3159 /*
3160 * Avoid setting timestamps on the server for ftruncate(2) to
3161 * prevent it from disabling automatic timestamp updates as per
3162 * MS-FSA 2.1.4.17.
3163 */
3164 attrs->ia_valid &= ~(ATTR_CTIME | ATTR_MTIME);
3165 }
3166
3167 /* skip mode change if it's just for clearing setuid/setgid */
3168 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
3169 attrs->ia_valid &= ~ATTR_MODE;
3170
3171 args = kmalloc(sizeof(*args), GFP_KERNEL);
3172 if (args == NULL) {
3173 rc = -ENOMEM;
3174 goto out;
3175 }
3176
3177 /* set up the struct */
3178 if (attrs->ia_valid & ATTR_MODE)
3179 args->mode = attrs->ia_mode;
3180 else
3181 args->mode = NO_CHANGE_64;
3182
3183 if (attrs->ia_valid & ATTR_UID)
3184 args->uid = attrs->ia_uid;
3185 else
3186 args->uid = INVALID_UID; /* no change */
3187
3188 if (attrs->ia_valid & ATTR_GID)
3189 args->gid = attrs->ia_gid;
3190 else
3191 args->gid = INVALID_GID; /* no change */
3192
3193 if (attrs->ia_valid & ATTR_ATIME)
3194 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
3195 else
3196 args->atime = NO_CHANGE_64;
3197
3198 if (attrs->ia_valid & ATTR_MTIME)
3199 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
3200 else
3201 args->mtime = NO_CHANGE_64;
3202
3203 if (attrs->ia_valid & ATTR_CTIME)
3204 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
3205 else
3206 args->ctime = NO_CHANGE_64;
3207
3208 args->device = 0;
3209 rc = -EINVAL;
3210 if (open_file && (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE)) {
3211 pTcon = tlink_tcon(open_file->tlink);
3212 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args,
3213 open_file->fid.netfid,
3214 open_file->pid);
3215 } else {
3216 open_file = find_writable_file(cifsInode, FIND_WR_FSUID_ONLY);
3217 if (open_file) {
3218 pTcon = tlink_tcon(open_file->tlink);
3219 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args,
3220 open_file->fid.netfid,
3221 open_file->pid);
3222 cifsFileInfo_put(open_file);
3223 }
3224 }
3225
3226 if (rc) {
3227 tlink = cifs_sb_tlink(cifs_sb);
3228 if (IS_ERR(tlink)) {
3229 rc = PTR_ERR(tlink);
3230 goto out;
3231 }
3232 pTcon = tlink_tcon(tlink);
3233 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
3234 cifs_sb->local_nls,
3235 cifs_remap(cifs_sb));
3236 cifs_put_tlink(tlink);
3237 }
3238
3239 if (rc)
3240 goto out;
3241
3242 if ((attrs->ia_valid & ATTR_SIZE) &&
3243 attrs->ia_size != i_size_read(inode)) {
3244 truncate_setsize(inode, attrs->ia_size);
3245 netfs_resize_file(&cifsInode->netfs, attrs->ia_size, true);
3246 fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
3247 }
3248
3249 setattr_copy(&nop_mnt_idmap, inode, attrs);
3250 mark_inode_dirty(inode);
3251
3252 /* force revalidate when any of these times are set since some
3253 of the fs types (eg ext3, fat) do not have fine enough
3254 time granularity to match protocol, and we do not have a
3255 a way (yet) to query the server fs's time granularity (and
3256 whether it rounds times down).
3257 */
3258 if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
3259 cifsInode->time = 0;
3260 out:
3261 kfree(args);
3262 free_dentry_path(page);
3263 free_xid(xid);
3264 return rc;
3265 }
3266 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3267
3268 static int
3269 cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
3270 {
3271 unsigned int xid;
3272 kuid_t uid = INVALID_UID;
3273 kgid_t gid = INVALID_GID;
3274 struct inode *inode = d_inode(direntry);
3275 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3276 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
3277 struct cifsFileInfo *cfile = NULL;
3278 const char *full_path;
3279 void *page = alloc_dentry_path();
3280 int rc = -EACCES;
3281 __u32 dosattr = 0;
3282 __u64 mode = NO_CHANGE_64;
3283 bool posix = cifs_sb_master_tcon(cifs_sb)->posix_extensions;
3284
3285 xid = get_xid();
3286
3287 cifs_dbg(FYI, "setattr on file %pd attrs->ia_valid 0x%x\n",
3288 direntry, attrs->ia_valid);
3289
3290 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
3291 attrs->ia_valid |= ATTR_FORCE;
3292
3293 rc = setattr_prepare(&nop_mnt_idmap, direntry, attrs);
3294 if (rc < 0)
3295 goto cifs_setattr_exit;
3296
3297 if (attrs->ia_valid & ATTR_FILE)
3298 cfile = attrs->ia_file->private_data;
3299
3300 full_path = build_path_from_dentry(direntry, page);
3301 if (IS_ERR(full_path)) {
3302 rc = PTR_ERR(full_path);
3303 goto cifs_setattr_exit;
3304 }
3305
3306 /*
3307 * Attempt to flush data before changing attributes. We need to do
3308 * this for ATTR_SIZE and ATTR_MTIME. If the flush of the data
3309 * returns error, store it to report later and continue.
3310 *
3311 * BB: This should be smarter. Why bother flushing pages that
3312 * will be truncated anyway? Also, should we error out here if
3313 * the flush returns error? Do we need to check for ATTR_MTIME_SET flag?
3314 */
3315 if (attrs->ia_valid & (ATTR_MTIME | ATTR_SIZE | ATTR_CTIME)) {
3316 rc = filemap_write_and_wait(inode->i_mapping);
3317 if (is_interrupt_error(rc)) {
3318 rc = -ERESTARTSYS;
3319 goto cifs_setattr_exit;
3320 }
3321 mapping_set_error(inode->i_mapping, rc);
3322 }
3323
3324 rc = 0;
3325
3326 if (attrs->ia_valid & ATTR_MTIME) {
3327 rc = cifs_file_flush(xid, inode, cfile);
3328 if (rc)
3329 goto cifs_setattr_exit;
3330 }
3331
3332 if (attrs->ia_valid & ATTR_SIZE) {
3333 rc = cifs_file_set_size(xid, direntry, full_path,
3334 cfile, attrs->ia_size);
3335 if (rc != 0)
3336 goto cifs_setattr_exit;
3337 /*
3338 * Avoid setting timestamps on the server for ftruncate(2) to
3339 * prevent it from disabling automatic timestamp updates as per
3340 * MS-FSA 2.1.4.17.
3341 */
3342 attrs->ia_valid &= ~(ATTR_CTIME | ATTR_MTIME);
3343 }
3344
3345 if (attrs->ia_valid & ATTR_UID)
3346 uid = attrs->ia_uid;
3347
3348 if (attrs->ia_valid & ATTR_GID)
3349 gid = attrs->ia_gid;
3350
3351 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
3352 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID)) {
3353 if (uid_valid(uid) || gid_valid(gid)) {
3354 mode = NO_CHANGE_64;
3355 rc = id_mode_to_cifs_acl(inode, full_path, &mode,
3356 uid, gid);
3357 if (rc) {
3358 cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
3359 __func__, rc);
3360 goto cifs_setattr_exit;
3361 }
3362 }
3363 } else
3364 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
3365 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
3366
3367 /* skip mode change if it's just for clearing setuid/setgid */
3368 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
3369 attrs->ia_valid &= ~ATTR_MODE;
3370
3371 if (attrs->ia_valid & ATTR_MODE) {
3372 mode = attrs->ia_mode;
3373 rc = 0;
3374 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) ||
3375 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MODE_FROM_SID) ||
3376 posix) {
3377 rc = id_mode_to_cifs_acl(inode, full_path, &mode,
3378 INVALID_UID, INVALID_GID);
3379 if (rc) {
3380 cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
3381 __func__, rc);
3382 goto cifs_setattr_exit;
3383 }
3384
3385 /*
3386 * In case of CIFS_MOUNT_CIFS_ACL, we cannot support all modes.
3387 * Pick up the actual mode bits that were set.
3388 */
3389 if (mode != attrs->ia_mode)
3390 attrs->ia_mode = mode;
3391 } else
3392 if (((mode & S_IWUGO) == 0) &&
3393 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
3394
3395 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
3396
3397 /* fix up mode if we're not using dynperm */
3398 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
3399 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
3400 } else if ((mode & S_IWUGO) &&
3401 (cifsInode->cifsAttrs & ATTR_READONLY)) {
3402
3403 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
3404 /* Attributes of 0 are ignored */
3405 if (dosattr == 0)
3406 dosattr |= ATTR_NORMAL;
3407
3408 /* reset local inode permissions to normal */
3409 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3410 attrs->ia_mode &= ~(S_IALLUGO);
3411 if (S_ISDIR(inode->i_mode))
3412 attrs->ia_mode |=
3413 cifs_sb->ctx->dir_mode;
3414 else
3415 attrs->ia_mode |=
3416 cifs_sb->ctx->file_mode;
3417 }
3418 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
3419 /* ignore mode change - ATTR_READONLY hasn't changed */
3420 attrs->ia_valid &= ~ATTR_MODE;
3421 }
3422 }
3423
3424 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
3425 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
3426 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
3427 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
3428
3429 /* Even if error on time set, no sense failing the call if
3430 the server would set the time to a reasonable value anyway,
3431 and this check ensures that we are not being called from
3432 sys_utimes in which case we ought to fail the call back to
3433 the user when the server rejects the call */
3434 if ((rc) && (attrs->ia_valid &
3435 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
3436 rc = 0;
3437 }
3438
3439 /* do not need local check to inode_check_ok since the server does
3440 that */
3441 if (rc)
3442 goto cifs_setattr_exit;
3443
3444 if ((attrs->ia_valid & ATTR_SIZE) &&
3445 attrs->ia_size != i_size_read(inode)) {
3446 truncate_setsize(inode, attrs->ia_size);
3447 netfs_resize_file(&cifsInode->netfs, attrs->ia_size, true);
3448 fscache_resize_cookie(cifs_inode_cookie(inode), attrs->ia_size);
3449 }
3450
3451 setattr_copy(&nop_mnt_idmap, inode, attrs);
3452 mark_inode_dirty(inode);
3453
3454 cifs_setattr_exit:
3455 free_xid(xid);
3456 free_dentry_path(page);
3457 return rc;
3458 }
3459
3460 int
3461 cifs_setattr(struct mnt_idmap *idmap, struct dentry *direntry,
3462 struct iattr *attrs)
3463 {
3464 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
3465 int rc, retries = 0;
3466 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3467 struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
3468 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3469
3470 if (unlikely(cifs_forced_shutdown(cifs_sb)))
3471 return -EIO;
3472 /*
3473 * Avoid setting [cm]time with O_TRUNC to prevent the server from
3474 * disabling automatic timestamp updates as specified in
3475 * MS-FSA 2.1.4.17.
3476 */
3477 if (attrs->ia_valid & ATTR_OPEN)
3478 return 0;
3479
3480 do {
3481 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
3482 if (pTcon->unix_ext)
3483 rc = cifs_setattr_unix(direntry, attrs);
3484 else
3485 #endif /* CONFIG_CIFS_ALLOW_INSECURE_LEGACY */
3486 rc = cifs_setattr_nounix(direntry, attrs);
3487 retries++;
3488 } while (is_retryable_error(rc) && retries < 2);
3489
3490 /* BB: add cifs_setattr_legacy for really old servers */
3491 return rc;
3492 }
3493