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