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