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