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