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