]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/cifs/inode.c
sched/headers: Prepare to move signal wakeup & sigpending methods from <linux/sched...
[mirror_ubuntu-artful-kernel.git] / fs / cifs / inode.c
CommitLineData
1da177e4
LT
1/*
2 * fs/cifs/inode.c
3 *
f19159dc 4 * Copyright (C) International Business Machines Corp., 2002,2010
1da177e4
LT
5 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21#include <linux/fs.h>
1da177e4 22#include <linux/stat.h>
5a0e3ad6 23#include <linux/slab.h>
1da177e4 24#include <linux/pagemap.h>
4f73c7d3 25#include <linux/freezer.h>
174cd4b1
IM
26#include <linux/sched/signal.h>
27
1da177e4
LT
28#include <asm/div64.h>
29#include "cifsfs.h"
30#include "cifspdu.h"
31#include "cifsglob.h"
32#include "cifsproto.h"
33#include "cifs_debug.h"
34#include "cifs_fs_sb.h"
2baa2682 35#include "cifs_unicode.h"
9451a9a5 36#include "fscache.h"
1da177e4 37
70eff55d 38
01c64fea 39static void cifs_set_ops(struct inode *inode)
70eff55d
CH
40{
41 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
42
43 switch (inode->i_mode & S_IFMT) {
44 case S_IFREG:
45 inode->i_op = &cifs_file_inode_ops;
46 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
47 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
48 inode->i_fop = &cifs_file_direct_nobrl_ops;
49 else
50 inode->i_fop = &cifs_file_direct_ops;
8be7e6ba
PS
51 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
52 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
53 inode->i_fop = &cifs_file_strict_nobrl_ops;
54 else
55 inode->i_fop = &cifs_file_strict_ops;
70eff55d
CH
56 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
57 inode->i_fop = &cifs_file_nobrl_ops;
58 else { /* not direct, send byte range locks */
59 inode->i_fop = &cifs_file_ops;
60 }
61
70eff55d 62 /* check if server can support readpages */
0d424ad0 63 if (cifs_sb_master_tcon(cifs_sb)->ses->server->maxBuf <
09cbfeaf 64 PAGE_SIZE + MAX_CIFS_HDR_SIZE)
70eff55d
CH
65 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
66 else
67 inode->i_data.a_ops = &cifs_addr_ops;
68 break;
69 case S_IFDIR:
bc5b6e24 70#ifdef CONFIG_CIFS_DFS_UPCALL
01c64fea 71 if (IS_AUTOMOUNT(inode)) {
7962670e
IM
72 inode->i_op = &cifs_dfs_referral_inode_operations;
73 } else {
bc5b6e24
SF
74#else /* NO DFS support, treat as a directory */
75 {
76#endif
7962670e
IM
77 inode->i_op = &cifs_dir_inode_ops;
78 inode->i_fop = &cifs_dir_ops;
79 }
70eff55d
CH
80 break;
81 case S_IFLNK:
82 inode->i_op = &cifs_symlink_inode_ops;
83 break;
84 default:
85 init_special_inode(inode, inode->i_mode, inode->i_rdev);
86 break;
87 }
88}
89
df2cf170
JL
90/* check inode attributes against fattr. If they don't match, tag the
91 * inode for cache invalidation
92 */
93static void
94cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
95{
96 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
97
f96637be
JP
98 cifs_dbg(FYI, "%s: revalidating inode %llu\n",
99 __func__, cifs_i->uniqueid);
df2cf170
JL
100
101 if (inode->i_state & I_NEW) {
f96637be
JP
102 cifs_dbg(FYI, "%s: inode %llu is new\n",
103 __func__, cifs_i->uniqueid);
df2cf170
JL
104 return;
105 }
106
107 /* don't bother with revalidation if we have an oplock */
18cceb6a 108 if (CIFS_CACHE_READ(cifs_i)) {
f96637be
JP
109 cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
110 __func__, cifs_i->uniqueid);
df2cf170
JL
111 return;
112 }
113
114 /* revalidate if mtime or size have changed */
115 if (timespec_equal(&inode->i_mtime, &fattr->cf_mtime) &&
116 cifs_i->server_eof == fattr->cf_eof) {
f96637be
JP
117 cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
118 __func__, cifs_i->uniqueid);
df2cf170
JL
119 return;
120 }
121
f96637be
JP
122 cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
123 __func__, cifs_i->uniqueid);
aff8d5ca 124 set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
df2cf170
JL
125}
126
74d290da
JM
127/*
128 * copy nlink to the inode, unless it wasn't provided. Provide
129 * sane values if we don't have an existing one and none was provided
130 */
131static void
132cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
133{
134 /*
135 * if we're in a situation where we can't trust what we
136 * got from the server (readdir, some non-unix cases)
137 * fake reasonable values
138 */
139 if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) {
140 /* only provide fake values on a new inode */
141 if (inode->i_state & I_NEW) {
142 if (fattr->cf_cifsattrs & ATTR_DIRECTORY)
143 set_nlink(inode, 2);
144 else
145 set_nlink(inode, 1);
146 }
147 return;
148 }
149
150 /* we trust the server, so update it */
151 set_nlink(inode, fattr->cf_nlink);
152}
153
cc0bad75
JL
154/* populate an inode with info from a cifs_fattr struct */
155void
156cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
75f12983 157{
cc0bad75 158 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
0b8f18e3 159 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
cc0bad75 160
df2cf170
JL
161 cifs_revalidate_cache(inode, fattr);
162
b7ca6928 163 spin_lock(&inode->i_lock);
cc0bad75
JL
164 inode->i_atime = fattr->cf_atime;
165 inode->i_mtime = fattr->cf_mtime;
166 inode->i_ctime = fattr->cf_ctime;
cc0bad75 167 inode->i_rdev = fattr->cf_rdev;
74d290da 168 cifs_nlink_fattr_to_inode(inode, fattr);
cc0bad75
JL
169 inode->i_uid = fattr->cf_uid;
170 inode->i_gid = fattr->cf_gid;
171
0b8f18e3
JL
172 /* if dynperm is set, don't clobber existing mode */
173 if (inode->i_state & I_NEW ||
174 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
175 inode->i_mode = fattr->cf_mode;
176
cc0bad75 177 cifs_i->cifsAttrs = fattr->cf_cifsattrs;
75f12983 178
0b8f18e3
JL
179 if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
180 cifs_i->time = 0;
181 else
182 cifs_i->time = jiffies;
183
aff8d5ca
JL
184 if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
185 set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
186 else
187 clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
cc0bad75 188
835a36ca 189 cifs_i->server_eof = fattr->cf_eof;
cc0bad75
JL
190 /*
191 * Can't safely change the file size here if the client is writing to
192 * it due to potential races.
193 */
cc0bad75
JL
194 if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
195 i_size_write(inode, fattr->cf_eof);
196
197 /*
198 * i_blocks is not related to (i_size / i_blksize),
199 * but instead 512 byte (2**9) size is required for
200 * calculating num blocks.
201 */
202 inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
203 }
204 spin_unlock(&inode->i_lock);
205
01c64fea
DH
206 if (fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL)
207 inode->i_flags |= S_AUTOMOUNT;
c2b93e06
JL
208 if (inode->i_state & I_NEW)
209 cifs_set_ops(inode);
cc0bad75
JL
210}
211
4065c802
JL
212void
213cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
214{
215 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
216
217 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
218 return;
219
220 fattr->cf_uniqueid = iunique(sb, ROOT_I);
221}
222
cc0bad75
JL
223/* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
224void
225cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
226 struct cifs_sb_info *cifs_sb)
227{
228 memset(fattr, 0, sizeof(*fattr));
229 fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
230 fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
231 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
232
233 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
234 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
235 fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
236 fattr->cf_mode = le64_to_cpu(info->Permissions);
75f12983
CH
237
238 /*
239 * Since we set the inode type below we need to mask off
240 * to avoid strange results if bits set above.
241 */
cc0bad75 242 fattr->cf_mode &= ~S_IFMT;
75f12983
CH
243 switch (le32_to_cpu(info->Type)) {
244 case UNIX_FILE:
cc0bad75
JL
245 fattr->cf_mode |= S_IFREG;
246 fattr->cf_dtype = DT_REG;
75f12983
CH
247 break;
248 case UNIX_SYMLINK:
cc0bad75
JL
249 fattr->cf_mode |= S_IFLNK;
250 fattr->cf_dtype = DT_LNK;
75f12983
CH
251 break;
252 case UNIX_DIR:
cc0bad75
JL
253 fattr->cf_mode |= S_IFDIR;
254 fattr->cf_dtype = DT_DIR;
75f12983
CH
255 break;
256 case UNIX_CHARDEV:
cc0bad75
JL
257 fattr->cf_mode |= S_IFCHR;
258 fattr->cf_dtype = DT_CHR;
259 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
260 le64_to_cpu(info->DevMinor) & MINORMASK);
75f12983
CH
261 break;
262 case UNIX_BLOCKDEV:
cc0bad75
JL
263 fattr->cf_mode |= S_IFBLK;
264 fattr->cf_dtype = DT_BLK;
265 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
266 le64_to_cpu(info->DevMinor) & MINORMASK);
75f12983
CH
267 break;
268 case UNIX_FIFO:
cc0bad75
JL
269 fattr->cf_mode |= S_IFIFO;
270 fattr->cf_dtype = DT_FIFO;
75f12983
CH
271 break;
272 case UNIX_SOCKET:
cc0bad75
JL
273 fattr->cf_mode |= S_IFSOCK;
274 fattr->cf_dtype = DT_SOCK;
75f12983
CH
275 break;
276 default:
277 /* safest to call it a file if we do not know */
cc0bad75
JL
278 fattr->cf_mode |= S_IFREG;
279 fattr->cf_dtype = DT_REG;
f96637be 280 cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
75f12983
CH
281 break;
282 }
283
46bbc25f
EB
284 fattr->cf_uid = cifs_sb->mnt_uid;
285 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
286 u64 id = le64_to_cpu(info->Uid);
4a2c8cf5
EB
287 if (id < ((uid_t)-1)) {
288 kuid_t uid = make_kuid(&init_user_ns, id);
289 if (uid_valid(uid))
290 fattr->cf_uid = uid;
291 }
46bbc25f
EB
292 }
293
294 fattr->cf_gid = cifs_sb->mnt_gid;
295 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
296 u64 id = le64_to_cpu(info->Gid);
4a2c8cf5
EB
297 if (id < ((gid_t)-1)) {
298 kgid_t gid = make_kgid(&init_user_ns, id);
299 if (gid_valid(gid))
300 fattr->cf_gid = gid;
301 }
46bbc25f 302 }
75f12983 303
cc0bad75 304 fattr->cf_nlink = le64_to_cpu(info->Nlinks);
75f12983
CH
305}
306
b9a3260f 307/*
cc0bad75
JL
308 * Fill a cifs_fattr struct with fake inode info.
309 *
310 * Needed to setup cifs_fattr data for the directory which is the
311 * junction to the new submount (ie to setup the fake directory
312 * which represents a DFS referral).
b9a3260f 313 */
f1230c97 314static void
cc0bad75 315cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb)
0e4bbde9 316{
cc0bad75 317 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
0e4bbde9 318
f96637be 319 cifs_dbg(FYI, "creating fake fattr for DFS referral\n");
cc0bad75
JL
320
321 memset(fattr, 0, sizeof(*fattr));
322 fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
323 fattr->cf_uid = cifs_sb->mnt_uid;
324 fattr->cf_gid = cifs_sb->mnt_gid;
325 fattr->cf_atime = CURRENT_TIME;
326 fattr->cf_ctime = CURRENT_TIME;
327 fattr->cf_mtime = CURRENT_TIME;
328 fattr->cf_nlink = 2;
329 fattr->cf_flags |= CIFS_FATTR_DFS_REFERRAL;
0e4bbde9
SF
330}
331
4ad65044
PS
332static int
333cifs_get_file_info_unix(struct file *filp)
abab095d
JL
334{
335 int rc;
6d5786a3 336 unsigned int xid;
abab095d
JL
337 FILE_UNIX_BASIC_INFO find_data;
338 struct cifs_fattr fattr;
496ad9aa 339 struct inode *inode = file_inode(filp);
abab095d 340 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
c21dfb69 341 struct cifsFileInfo *cfile = filp->private_data;
96daf2b0 342 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
abab095d 343
6d5786a3 344 xid = get_xid();
4b4de76e 345 rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
abab095d
JL
346 if (!rc) {
347 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
348 } else if (rc == -EREMOTE) {
349 cifs_create_dfs_fattr(&fattr, inode->i_sb);
350 rc = 0;
351 }
352
353 cifs_fattr_to_inode(inode, &fattr);
6d5786a3 354 free_xid(xid);
abab095d
JL
355 return rc;
356}
357
1da177e4 358int cifs_get_inode_info_unix(struct inode **pinode,
cc0bad75 359 const unsigned char *full_path,
6d5786a3 360 struct super_block *sb, unsigned int xid)
1da177e4 361{
cc0bad75 362 int rc;
0e4bbde9 363 FILE_UNIX_BASIC_INFO find_data;
cc0bad75 364 struct cifs_fattr fattr;
96daf2b0 365 struct cifs_tcon *tcon;
7ffec372 366 struct tcon_link *tlink;
1da177e4 367 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1da177e4 368
f96637be 369 cifs_dbg(FYI, "Getting info on %s\n", full_path);
7962670e 370
7ffec372
JL
371 tlink = cifs_sb_tlink(cifs_sb);
372 if (IS_ERR(tlink))
373 return PTR_ERR(tlink);
374 tcon = tlink_tcon(tlink);
375
1da177e4 376 /* could have done a find first instead but this returns more info */
cc0bad75 377 rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
bc8ebdc4 378 cifs_sb->local_nls, cifs_remap(cifs_sb));
7ffec372 379 cifs_put_tlink(tlink);
e911d0cc 380
cc0bad75
JL
381 if (!rc) {
382 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
383 } else if (rc == -EREMOTE) {
384 cifs_create_dfs_fattr(&fattr, sb);
385 rc = 0;
386 } else {
387 return rc;
388 }
1da177e4 389
1b12b9c1
SM
390 /* check for Minshall+French symlinks */
391 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
cb084b1a
SP
392 int tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
393 full_path);
1b12b9c1 394 if (tmprc)
cb084b1a 395 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1b12b9c1
SM
396 }
397
0e4bbde9 398 if (*pinode == NULL) {
cc0bad75 399 /* get new inode */
4065c802 400 cifs_fill_uniqueid(sb, &fattr);
cc0bad75
JL
401 *pinode = cifs_iget(sb, &fattr);
402 if (!*pinode)
0e4bbde9 403 rc = -ENOMEM;
cc0bad75
JL
404 } else {
405 /* we already have inode, update it */
7196ac11
NA
406
407 /* if uniqueid is different, return error */
408 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
409 CIFS_I(*pinode)->uniqueid != fattr.cf_uniqueid)) {
410 rc = -ESTALE;
411 goto cgiiu_exit;
412 }
413
414 /* if filetype is different, return error */
415 if (unlikely(((*pinode)->i_mode & S_IFMT) !=
416 (fattr.cf_mode & S_IFMT))) {
417 rc = -ESTALE;
418 goto cgiiu_exit;
419 }
420
cc0bad75 421 cifs_fattr_to_inode(*pinode, &fattr);
0e4bbde9 422 }
1da177e4 423
7196ac11 424cgiiu_exit:
1da177e4
LT
425 return rc;
426}
427
0b8f18e3 428static int
0360d605 429cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
6d5786a3 430 struct cifs_sb_info *cifs_sb, unsigned int xid)
d6e2f2a4
SF
431{
432 int rc;
db8b631d 433 __u32 oplock;
7ffec372 434 struct tcon_link *tlink;
96daf2b0 435 struct cifs_tcon *tcon;
d81b8a40
PS
436 struct cifs_fid fid;
437 struct cifs_open_parms oparms;
d4ffff1f 438 struct cifs_io_parms io_parms;
86c96b4b 439 char buf[24];
d6e2f2a4 440 unsigned int bytes_read;
fb8c4b14 441 char *pbuf;
0360d605 442 int buf_type = CIFS_NO_BUFFER;
d6e2f2a4
SF
443
444 pbuf = buf;
445
0b8f18e3
JL
446 fattr->cf_mode &= ~S_IFMT;
447
448 if (fattr->cf_eof == 0) {
449 fattr->cf_mode |= S_IFIFO;
450 fattr->cf_dtype = DT_FIFO;
d6e2f2a4 451 return 0;
0b8f18e3
JL
452 } else if (fattr->cf_eof < 8) {
453 fattr->cf_mode |= S_IFREG;
454 fattr->cf_dtype = DT_REG;
d6e2f2a4
SF
455 return -EINVAL; /* EOPNOTSUPP? */
456 }
50c2f753 457
7ffec372
JL
458 tlink = cifs_sb_tlink(cifs_sb);
459 if (IS_ERR(tlink))
460 return PTR_ERR(tlink);
461 tcon = tlink_tcon(tlink);
462
d81b8a40
PS
463 oparms.tcon = tcon;
464 oparms.cifs_sb = cifs_sb;
465 oparms.desired_access = GENERIC_READ;
466 oparms.create_options = CREATE_NOT_DIR;
467 oparms.disposition = FILE_OPEN;
468 oparms.path = path;
469 oparms.fid = &fid;
470 oparms.reconnect = false;
471
db8b631d
SF
472 if (tcon->ses->server->oplocks)
473 oplock = REQ_OPLOCK;
474 else
475 oplock = 0;
476 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, NULL);
0360d605 477 if (rc) {
db8b631d 478 cifs_dbg(FYI, "check sfu type of %s, open rc = %d\n", path, rc);
0360d605
PS
479 cifs_put_tlink(tlink);
480 return rc;
481 }
482
483 /* Read header */
d81b8a40 484 io_parms.netfid = fid.netfid;
0360d605
PS
485 io_parms.pid = current->tgid;
486 io_parms.tcon = tcon;
487 io_parms.offset = 0;
488 io_parms.length = 24;
489
db8b631d
SF
490 rc = tcon->ses->server->ops->sync_read(xid, &fid, &io_parms,
491 &bytes_read, &pbuf, &buf_type);
0360d605
PS
492 if ((rc == 0) && (bytes_read >= 8)) {
493 if (memcmp("IntxBLK", pbuf, 8) == 0) {
494 cifs_dbg(FYI, "Block device\n");
495 fattr->cf_mode |= S_IFBLK;
496 fattr->cf_dtype = DT_BLK;
497 if (bytes_read == 24) {
498 /* we have enough to decode dev num */
499 __u64 mjr; /* major */
500 __u64 mnr; /* minor */
501 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
502 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
503 fattr->cf_rdev = MKDEV(mjr, mnr);
86c96b4b 504 }
0360d605
PS
505 } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
506 cifs_dbg(FYI, "Char device\n");
507 fattr->cf_mode |= S_IFCHR;
508 fattr->cf_dtype = DT_CHR;
509 if (bytes_read == 24) {
510 /* we have enough to decode dev num */
511 __u64 mjr; /* major */
512 __u64 mnr; /* minor */
513 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
514 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
515 fattr->cf_rdev = MKDEV(mjr, mnr);
516 }
517 } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
518 cifs_dbg(FYI, "Symlink\n");
519 fattr->cf_mode |= S_IFLNK;
520 fattr->cf_dtype = DT_LNK;
3020a1f5 521 } else {
0360d605 522 fattr->cf_mode |= S_IFREG; /* file? */
0b8f18e3 523 fattr->cf_dtype = DT_REG;
0360d605 524 rc = -EOPNOTSUPP;
fb8c4b14 525 }
0360d605
PS
526 } else {
527 fattr->cf_mode |= S_IFREG; /* then it is a file */
528 fattr->cf_dtype = DT_REG;
529 rc = -EOPNOTSUPP; /* or some unknown SFU type */
d6e2f2a4 530 }
db8b631d
SF
531
532 tcon->ses->server->ops->close(xid, tcon, &fid);
7ffec372 533 cifs_put_tlink(tlink);
d6e2f2a4 534 return rc;
d6e2f2a4
SF
535}
536
9e294f1c
SF
537#define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
538
0b8f18e3
JL
539/*
540 * Fetch mode bits as provided by SFU.
541 *
542 * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
543 */
544static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
6d5786a3 545 struct cifs_sb_info *cifs_sb, unsigned int xid)
9e294f1c 546{
3020a1f5 547#ifdef CONFIG_CIFS_XATTR
9e294f1c
SF
548 ssize_t rc;
549 char ea_value[4];
550 __u32 mode;
7ffec372 551 struct tcon_link *tlink;
96daf2b0 552 struct cifs_tcon *tcon;
7ffec372
JL
553
554 tlink = cifs_sb_tlink(cifs_sb);
555 if (IS_ERR(tlink))
556 return PTR_ERR(tlink);
557 tcon = tlink_tcon(tlink);
9e294f1c 558
d979f3b0
SF
559 if (tcon->ses->server->ops->query_all_EAs == NULL) {
560 cifs_put_tlink(tlink);
561 return -EOPNOTSUPP;
562 }
563
564 rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
565 "SETFILEBITS", ea_value, 4 /* size of buf */,
566 cifs_sb->local_nls,
2baa2682 567 cifs_remap(cifs_sb));
7ffec372 568 cifs_put_tlink(tlink);
4523cc30 569 if (rc < 0)
9e294f1c
SF
570 return (int)rc;
571 else if (rc > 3) {
572 mode = le32_to_cpu(*((__le32 *)ea_value));
0b8f18e3 573 fattr->cf_mode &= ~SFBITS_MASK;
f96637be
JP
574 cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
575 mode, fattr->cf_mode);
0b8f18e3 576 fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
f96637be 577 cifs_dbg(FYI, "special mode bits 0%o\n", mode);
9e294f1c 578 }
0b8f18e3
JL
579
580 return 0;
3020a1f5
SF
581#else
582 return -EOPNOTSUPP;
583#endif
9e294f1c
SF
584}
585
0b8f18e3 586/* Fill a cifs_fattr struct with info from FILE_ALL_INFO */
f1230c97 587static void
0b8f18e3 588cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info,
eb85d94b
PS
589 struct cifs_sb_info *cifs_sb, bool adjust_tz,
590 bool symlink)
b9a3260f 591{
96daf2b0 592 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
0d424ad0 593
0b8f18e3
JL
594 memset(fattr, 0, sizeof(*fattr));
595 fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
596 if (info->DeletePending)
597 fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
598
599 if (info->LastAccessTime)
600 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
601 else
602 fattr->cf_atime = CURRENT_TIME;
603
604 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
605 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
606
607 if (adjust_tz) {
0d424ad0
JL
608 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
609 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
0b8f18e3
JL
610 }
611
612 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
613 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
20054bd6 614 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
0b8f18e3 615
74d290da 616 fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
eb85d94b
PS
617
618 if (symlink) {
619 fattr->cf_mode = S_IFLNK;
620 fattr->cf_dtype = DT_LNK;
621 } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
0b8f18e3
JL
622 fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
623 fattr->cf_dtype = DT_DIR;
6de2ce42
PS
624 /*
625 * Server can return wrong NumberOfLinks value for directories
626 * when Unix extensions are disabled - fake it.
627 */
74d290da
JM
628 if (!tcon->unix_ext)
629 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
0b8f18e3
JL
630 } else {
631 fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
632 fattr->cf_dtype = DT_REG;
0b8f18e3 633
d0c280d2
JL
634 /* clear write bits if ATTR_READONLY is set */
635 if (fattr->cf_cifsattrs & ATTR_READONLY)
636 fattr->cf_mode &= ~(S_IWUGO);
0b8f18e3 637
74d290da
JM
638 /*
639 * Don't accept zero nlink from non-unix servers unless
640 * delete is pending. Instead mark it as unknown.
641 */
642 if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
643 !info->DeletePending) {
644 cifs_dbg(1, "bogus file nlink value %u\n",
6658b9f7 645 fattr->cf_nlink);
74d290da 646 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
6658b9f7 647 }
6de2ce42 648 }
0b8f18e3
JL
649
650 fattr->cf_uid = cifs_sb->mnt_uid;
651 fattr->cf_gid = cifs_sb->mnt_gid;
b9a3260f
SF
652}
653
4ad65044
PS
654static int
655cifs_get_file_info(struct file *filp)
abab095d
JL
656{
657 int rc;
6d5786a3 658 unsigned int xid;
abab095d
JL
659 FILE_ALL_INFO find_data;
660 struct cifs_fattr fattr;
496ad9aa 661 struct inode *inode = file_inode(filp);
abab095d 662 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
c21dfb69 663 struct cifsFileInfo *cfile = filp->private_data;
96daf2b0 664 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
4ad65044
PS
665 struct TCP_Server_Info *server = tcon->ses->server;
666
667 if (!server->ops->query_file_info)
668 return -ENOSYS;
abab095d 669
6d5786a3 670 xid = get_xid();
4ad65044 671 rc = server->ops->query_file_info(xid, tcon, &cfile->fid, &find_data);
42274bb2
PS
672 switch (rc) {
673 case 0:
eb85d94b
PS
674 cifs_all_info_to_fattr(&fattr, &find_data, cifs_sb, false,
675 false);
42274bb2
PS
676 break;
677 case -EREMOTE:
678 cifs_create_dfs_fattr(&fattr, inode->i_sb);
679 rc = 0;
680 break;
681 case -EOPNOTSUPP:
682 case -EINVAL:
abab095d
JL
683 /*
684 * FIXME: legacy server -- fall back to path-based call?
ff215713
SF
685 * for now, just skip revalidating and mark inode for
686 * immediate reval.
687 */
abab095d
JL
688 rc = 0;
689 CIFS_I(inode)->time = 0;
42274bb2 690 default:
abab095d 691 goto cgfi_exit;
42274bb2 692 }
abab095d
JL
693
694 /*
695 * don't bother with SFU junk here -- just mark inode as needing
696 * revalidation.
697 */
abab095d
JL
698 fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
699 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
700 cifs_fattr_to_inode(inode, &fattr);
701cgfi_exit:
6d5786a3 702 free_xid(xid);
abab095d
JL
703 return rc;
704}
705
1208ef1f
PS
706int
707cifs_get_inode_info(struct inode **inode, const char *full_path,
708 FILE_ALL_INFO *data, struct super_block *sb, int xid,
42eacf9e 709 const struct cifs_fid *fid)
1da177e4 710{
c052e2b4
SP
711 bool validinum = false;
712 __u16 srchflgs;
713 int rc = 0, tmprc = ENOSYS;
1208ef1f
PS
714 struct cifs_tcon *tcon;
715 struct TCP_Server_Info *server;
7ffec372 716 struct tcon_link *tlink;
1da177e4 717 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1da177e4 718 char *buf = NULL;
1208ef1f 719 bool adjust_tz = false;
0b8f18e3 720 struct cifs_fattr fattr;
c052e2b4 721 struct cifs_search_info *srchinf = NULL;
eb85d94b 722 bool symlink = false;
1da177e4 723
7ffec372
JL
724 tlink = cifs_sb_tlink(cifs_sb);
725 if (IS_ERR(tlink))
726 return PTR_ERR(tlink);
1208ef1f
PS
727 tcon = tlink_tcon(tlink);
728 server = tcon->ses->server;
7ffec372 729
f96637be 730 cifs_dbg(FYI, "Getting info on %s\n", full_path);
1da177e4 731
1208ef1f 732 if ((data == NULL) && (*inode != NULL)) {
18cceb6a 733 if (CIFS_CACHE_READ(CIFS_I(*inode))) {
f96637be 734 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
7ffec372 735 goto cgii_exit;
1da177e4
LT
736 }
737 }
738
1208ef1f
PS
739 /* if inode info is not passed, get it from server */
740 if (data == NULL) {
741 if (!server->ops->query_path_info) {
742 rc = -ENOSYS;
743 goto cgii_exit;
744 }
1da177e4 745 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
7ffec372
JL
746 if (buf == NULL) {
747 rc = -ENOMEM;
748 goto cgii_exit;
749 }
1208ef1f
PS
750 data = (FILE_ALL_INFO *)buf;
751 rc = server->ops->query_path_info(xid, tcon, cifs_sb, full_path,
eb85d94b 752 data, &adjust_tz, &symlink);
1da177e4 753 }
0b8f18e3
JL
754
755 if (!rc) {
eb85d94b
PS
756 cifs_all_info_to_fattr(&fattr, data, cifs_sb, adjust_tz,
757 symlink);
0b8f18e3
JL
758 } else if (rc == -EREMOTE) {
759 cifs_create_dfs_fattr(&fattr, sb);
b9a3260f 760 rc = 0;
c052e2b4
SP
761 } else if (rc == -EACCES && backup_cred(cifs_sb)) {
762 srchinf = kzalloc(sizeof(struct cifs_search_info),
763 GFP_KERNEL);
764 if (srchinf == NULL) {
765 rc = -ENOMEM;
766 goto cgii_exit;
767 }
768
769 srchinf->endOfSearch = false;
770 srchinf->info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
771
772 srchflgs = CIFS_SEARCH_CLOSE_ALWAYS |
773 CIFS_SEARCH_CLOSE_AT_END |
774 CIFS_SEARCH_BACKUP_SEARCH;
775
776 rc = CIFSFindFirst(xid, tcon, full_path,
777 cifs_sb, NULL, srchflgs, srchinf, false);
778 if (!rc) {
779 data =
780 (FILE_ALL_INFO *)srchinf->srch_entries_start;
781
782 cifs_dir_info_to_fattr(&fattr,
783 (FILE_DIRECTORY_INFO *)data, cifs_sb);
784 fattr.cf_uniqueid = le64_to_cpu(
785 ((SEARCH_ID_FULL_DIR_INFO *)data)->UniqueId);
786 validinum = true;
787
788 cifs_buf_release(srchinf->ntwrk_buf_start);
789 }
790 kfree(srchinf);
4c5930e8
SF
791 if (rc)
792 goto cgii_exit;
c052e2b4 793 } else
7962670e 794 goto cgii_exit;
1da177e4 795
0b8f18e3
JL
796 /*
797 * If an inode wasn't passed in, then get the inode number
798 *
799 * Is an i_ino of zero legal? Can we use that to check if the server
800 * supports returning inode numbers? Are there other sanity checks we
801 * can use to ensure that the server is really filling in that field?
0b8f18e3 802 */
1208ef1f 803 if (*inode == NULL) {
b9a3260f 804 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
c052e2b4
SP
805 if (validinum == false) {
806 if (server->ops->get_srv_inum)
807 tmprc = server->ops->get_srv_inum(xid,
808 tcon, cifs_sb, full_path,
809 &fattr.cf_uniqueid, data);
810 if (tmprc) {
f96637be
JP
811 cifs_dbg(FYI, "GetSrvInodeNum rc %d\n",
812 tmprc);
c052e2b4
SP
813 fattr.cf_uniqueid = iunique(sb, ROOT_I);
814 cifs_autodisable_serverino(cifs_sb);
815 }
132ac7b7 816 }
c052e2b4 817 } else
0b8f18e3 818 fattr.cf_uniqueid = iunique(sb, ROOT_I);
a108471b
RL
819 } else {
820 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
821 validinum == false && server->ops->get_srv_inum) {
822 /*
823 * Pass a NULL tcon to ensure we don't make a round
824 * trip to the server. This only works for SMB2+.
825 */
826 tmprc = server->ops->get_srv_inum(xid,
827 NULL, cifs_sb, full_path,
828 &fattr.cf_uniqueid, data);
829 if (tmprc)
830 fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid;
831 } else
832 fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid;
833 }
b9a3260f 834
0b8f18e3
JL
835 /* query for SFU type info if supported and needed */
836 if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
837 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
838 tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
839 if (tmprc)
f96637be 840 cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
b9a3260f 841 }
1da177e4 842
79df1bae 843#ifdef CONFIG_CIFS_ACL
b9a3260f
SF
844 /* fill in 0777 bits from ACL */
845 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
1208ef1f 846 rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, full_path, fid);
78415d2d 847 if (rc) {
f96637be
JP
848 cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
849 __func__, rc);
78415d2d
SP
850 goto cgii_exit;
851 }
b9a3260f 852 }
79df1bae 853#endif /* CONFIG_CIFS_ACL */
b9a3260f 854
0b8f18e3
JL
855 /* fill in remaining high mode bits e.g. SUID, VTX */
856 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
857 cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
b9a3260f 858
1b12b9c1
SM
859 /* check for Minshall+French symlinks */
860 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
cb084b1a
SP
861 tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
862 full_path);
1b12b9c1 863 if (tmprc)
cb084b1a 864 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
1b12b9c1
SM
865 }
866
1208ef1f
PS
867 if (!*inode) {
868 *inode = cifs_iget(sb, &fattr);
869 if (!*inode)
0b8f18e3
JL
870 rc = -ENOMEM;
871 } else {
7196ac11
NA
872 /* we already have inode, update it */
873
a108471b
RL
874 /* if uniqueid is different, return error */
875 if (unlikely(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM &&
876 CIFS_I(*inode)->uniqueid != fattr.cf_uniqueid)) {
877 rc = -ESTALE;
878 goto cgii_exit;
879 }
880
7196ac11
NA
881 /* if filetype is different, return error */
882 if (unlikely(((*inode)->i_mode & S_IFMT) !=
883 (fattr.cf_mode & S_IFMT))) {
884 rc = -ESTALE;
885 goto cgii_exit;
886 }
887
1208ef1f 888 cifs_fattr_to_inode(*inode, &fattr);
0b8f18e3 889 }
b9a3260f 890
7962670e 891cgii_exit:
1da177e4 892 kfree(buf);
7ffec372 893 cifs_put_tlink(tlink);
1da177e4
LT
894 return rc;
895}
896
7f8ed420
SF
897static const struct inode_operations cifs_ipc_inode_ops = {
898 .lookup = cifs_lookup,
899};
900
cc0bad75
JL
901static int
902cifs_find_inode(struct inode *inode, void *opaque)
903{
904 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
905
f30b9c11 906 /* don't match inode with different uniqueid */
cc0bad75
JL
907 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
908 return 0;
909
20054bd6
JL
910 /* use createtime like an i_generation field */
911 if (CIFS_I(inode)->createtime != fattr->cf_createtime)
912 return 0;
913
f30b9c11
JL
914 /* don't match inode of different type */
915 if ((inode->i_mode & S_IFMT) != (fattr->cf_mode & S_IFMT))
916 return 0;
917
5acfec25 918 /* if it's not a directory or has no dentries, then flag it */
b3d9b7a3 919 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
3d694380 920 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
3d694380 921
cc0bad75
JL
922 return 1;
923}
924
925static int
926cifs_init_inode(struct inode *inode, void *opaque)
927{
928 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
929
930 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
20054bd6 931 CIFS_I(inode)->createtime = fattr->cf_createtime;
cc0bad75
JL
932 return 0;
933}
934
5acfec25
JL
935/*
936 * walk dentry list for an inode and report whether it has aliases that
937 * are hashed. We use this to determine if a directory inode can actually
938 * be used.
939 */
940static bool
941inode_has_hashed_dentries(struct inode *inode)
942{
943 struct dentry *dentry;
944
873feea0 945 spin_lock(&inode->i_lock);
946e51f2 946 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
5acfec25 947 if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
873feea0 948 spin_unlock(&inode->i_lock);
5acfec25
JL
949 return true;
950 }
951 }
873feea0 952 spin_unlock(&inode->i_lock);
5acfec25
JL
953 return false;
954}
955
cc0bad75
JL
956/* Given fattrs, get a corresponding inode */
957struct inode *
958cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
959{
960 unsigned long hash;
961 struct inode *inode;
962
3d694380 963retry_iget5_locked:
f96637be 964 cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
cc0bad75
JL
965
966 /* hash down to 32-bits on 32-bit arch */
967 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
968
969 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
cc0bad75 970 if (inode) {
5acfec25 971 /* was there a potentially problematic inode collision? */
3d694380 972 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
3d694380 973 fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
5acfec25
JL
974
975 if (inode_has_hashed_dentries(inode)) {
976 cifs_autodisable_serverino(CIFS_SB(sb));
977 iput(inode);
978 fattr->cf_uniqueid = iunique(sb, ROOT_I);
979 goto retry_iget5_locked;
980 }
3d694380
JL
981 }
982
cc0bad75
JL
983 cifs_fattr_to_inode(inode, fattr);
984 if (sb->s_flags & MS_NOATIME)
985 inode->i_flags |= S_NOATIME | S_NOCMTIME;
986 if (inode->i_state & I_NEW) {
987 inode->i_ino = hash;
0ccd4802 988#ifdef CONFIG_CIFS_FSCACHE
9451a9a5
SJ
989 /* initialize per-inode cache cookie pointer */
990 CIFS_I(inode)->fscache = NULL;
0ccd4802 991#endif
cc0bad75
JL
992 unlock_new_inode(inode);
993 }
994 }
995
996 return inode;
997}
998
1da177e4 999/* gets root inode */
9b6763e0 1000struct inode *cifs_root_iget(struct super_block *sb)
1da177e4 1001{
6d5786a3 1002 unsigned int xid;
0d424ad0 1003 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
cc0bad75 1004 struct inode *inode = NULL;
ce634ab2 1005 long rc;
96daf2b0 1006 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
a6b5058f
AA
1007 char *path = NULL;
1008 int len;
1009
1010 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH)
1011 && cifs_sb->prepath) {
1012 len = strlen(cifs_sb->prepath);
1013 path = kzalloc(len + 2 /* leading sep + null */, GFP_KERNEL);
1014 if (path == NULL)
1015 return ERR_PTR(-ENOMEM);
1016 path[0] = '/';
1017 memcpy(path+1, cifs_sb->prepath, len);
1018 } else {
1019 path = kstrdup("", GFP_KERNEL);
1020 if (path == NULL)
1021 return ERR_PTR(-ENOMEM);
1022 }
ce634ab2 1023
6d5786a3 1024 xid = get_xid();
b5b374ea 1025 if (tcon->unix_ext) {
a6b5058f 1026 rc = cifs_get_inode_info_unix(&inode, path, sb, xid);
b5b374ea
SF
1027 /* some servers mistakenly claim POSIX support */
1028 if (rc != -EOPNOTSUPP)
1029 goto iget_no_retry;
1030 cifs_dbg(VFS, "server does not support POSIX extensions");
1031 tcon->unix_ext = false;
1032 }
1033
a6b5058f
AA
1034 convert_delimiter(path, CIFS_DIR_SEP(cifs_sb));
1035 rc = cifs_get_inode_info(&inode, path, NULL, sb, xid, NULL);
0b8f18e3 1036
b5b374ea 1037iget_no_retry:
a7851ce7
OS
1038 if (!inode) {
1039 inode = ERR_PTR(rc);
1040 goto out;
1041 }
cc0bad75 1042
0ccd4802 1043#ifdef CONFIG_CIFS_FSCACHE
d03382ce 1044 /* populate tcon->resource_id */
0d424ad0 1045 tcon->resource_id = CIFS_I(inode)->uniqueid;
0ccd4802 1046#endif
d03382ce 1047
0d424ad0 1048 if (rc && tcon->ipc) {
f96637be 1049 cifs_dbg(FYI, "ipc connection - fake read inode\n");
b7ca6928 1050 spin_lock(&inode->i_lock);
7f8ed420 1051 inode->i_mode |= S_IFDIR;
bfe86848 1052 set_nlink(inode, 2);
7f8ed420
SF
1053 inode->i_op = &cifs_ipc_inode_ops;
1054 inode->i_fop = &simple_dir_operations;
1055 inode->i_uid = cifs_sb->mnt_uid;
1056 inode->i_gid = cifs_sb->mnt_gid;
b7ca6928 1057 spin_unlock(&inode->i_lock);
ad661334 1058 } else if (rc) {
ce634ab2 1059 iget_failed(inode);
a7851ce7 1060 inode = ERR_PTR(rc);
7f8ed420
SF
1061 }
1062
a7851ce7 1063out:
a6b5058f 1064 kfree(path);
6d5786a3 1065 /* can not call macro free_xid here since in a void func
ce634ab2
DH
1066 * TODO: This is no longer true
1067 */
6d5786a3 1068 _free_xid(xid);
ce634ab2 1069 return inode;
1da177e4
LT
1070}
1071
ed6875e0 1072int
6d5786a3 1073cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
ed6875e0 1074 char *full_path, __u32 dosattr)
388e57b2 1075{
388e57b2 1076 bool set_time = false;
388e57b2 1077 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
6bdf6dbd 1078 struct TCP_Server_Info *server;
388e57b2
SF
1079 FILE_BASIC_INFO info_buf;
1080
1adcb710
SF
1081 if (attrs == NULL)
1082 return -EINVAL;
1083
6bdf6dbd
PS
1084 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1085 if (!server->ops->set_file_info)
1086 return -ENOSYS;
1087
388e57b2
SF
1088 if (attrs->ia_valid & ATTR_ATIME) {
1089 set_time = true;
1090 info_buf.LastAccessTime =
1091 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1092 } else
1093 info_buf.LastAccessTime = 0;
1094
1095 if (attrs->ia_valid & ATTR_MTIME) {
1096 set_time = true;
1097 info_buf.LastWriteTime =
1098 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1099 } else
1100 info_buf.LastWriteTime = 0;
1101
1102 /*
1103 * Samba throws this field away, but windows may actually use it.
1104 * Do not set ctime unless other time stamps are changed explicitly
1105 * (i.e. by utimes()) since we would then have a mix of client and
1106 * server times.
1107 */
1108 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
f96637be 1109 cifs_dbg(FYI, "CIFS - CTIME changed\n");
388e57b2
SF
1110 info_buf.ChangeTime =
1111 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1112 } else
1113 info_buf.ChangeTime = 0;
1114
1115 info_buf.CreationTime = 0; /* don't change */
1116 info_buf.Attributes = cpu_to_le32(dosattr);
1117
6bdf6dbd 1118 return server->ops->set_file_info(inode, full_path, &info_buf, xid);
388e57b2
SF
1119}
1120
a12a1ac7 1121/*
ed6875e0 1122 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
a12a1ac7
JL
1123 * and rename it to a random name that hopefully won't conflict with
1124 * anything else.
1125 */
ed6875e0
PS
1126int
1127cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1128 const unsigned int xid)
a12a1ac7
JL
1129{
1130 int oplock = 0;
1131 int rc;
d81b8a40
PS
1132 struct cifs_fid fid;
1133 struct cifs_open_parms oparms;
2b0143b5 1134 struct inode *inode = d_inode(dentry);
a12a1ac7
JL
1135 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1136 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
7ffec372 1137 struct tcon_link *tlink;
96daf2b0 1138 struct cifs_tcon *tcon;
3270958b
SF
1139 __u32 dosattr, origattr;
1140 FILE_BASIC_INFO *info_buf = NULL;
a12a1ac7 1141
7ffec372
JL
1142 tlink = cifs_sb_tlink(cifs_sb);
1143 if (IS_ERR(tlink))
1144 return PTR_ERR(tlink);
1145 tcon = tlink_tcon(tlink);
1146
c483a984
SP
1147 /*
1148 * We cannot rename the file if the server doesn't support
1149 * CAP_INFOLEVEL_PASSTHRU
1150 */
1151 if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1152 rc = -EBUSY;
1153 goto out;
1154 }
1155
d81b8a40
PS
1156 oparms.tcon = tcon;
1157 oparms.cifs_sb = cifs_sb;
1158 oparms.desired_access = DELETE | FILE_WRITE_ATTRIBUTES;
1159 oparms.create_options = CREATE_NOT_DIR;
1160 oparms.disposition = FILE_OPEN;
1161 oparms.path = full_path;
1162 oparms.fid = &fid;
1163 oparms.reconnect = false;
1164
1165 rc = CIFS_open(xid, &oparms, &oplock, NULL);
a12a1ac7
JL
1166 if (rc != 0)
1167 goto out;
1168
3270958b
SF
1169 origattr = cifsInode->cifsAttrs;
1170 if (origattr == 0)
1171 origattr |= ATTR_NORMAL;
1172
1173 dosattr = origattr & ~ATTR_READONLY;
a12a1ac7
JL
1174 if (dosattr == 0)
1175 dosattr |= ATTR_NORMAL;
1176 dosattr |= ATTR_HIDDEN;
1177
3270958b
SF
1178 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1179 if (dosattr != origattr) {
1180 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1181 if (info_buf == NULL) {
1182 rc = -ENOMEM;
1183 goto out_close;
1184 }
1185 info_buf->Attributes = cpu_to_le32(dosattr);
d81b8a40 1186 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
3270958b
SF
1187 current->tgid);
1188 /* although we would like to mark the file hidden
1189 if that fails we will still try to rename it */
72d282dc 1190 if (!rc)
3270958b
SF
1191 cifsInode->cifsAttrs = dosattr;
1192 else
1193 dosattr = origattr; /* since not able to change them */
a12a1ac7 1194 }
a12a1ac7 1195
dd1db2de 1196 /* rename the file */
d81b8a40
PS
1197 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1198 cifs_sb->local_nls,
2baa2682 1199 cifs_remap(cifs_sb));
3270958b 1200 if (rc != 0) {
47c78f4a 1201 rc = -EBUSY;
3270958b
SF
1202 goto undo_setattr;
1203 }
6d22f098 1204
3270958b 1205 /* try to set DELETE_ON_CLOSE */
aff8d5ca 1206 if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
d81b8a40 1207 rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
3270958b
SF
1208 current->tgid);
1209 /*
1210 * some samba versions return -ENOENT when we try to set the
1211 * file disposition here. Likely a samba bug, but work around
1212 * it for now. This means that some cifsXXX files may hang
1213 * around after they shouldn't.
1214 *
1215 * BB: remove this hack after more servers have the fix
1216 */
1217 if (rc == -ENOENT)
1218 rc = 0;
1219 else if (rc != 0) {
47c78f4a 1220 rc = -EBUSY;
3270958b
SF
1221 goto undo_rename;
1222 }
aff8d5ca 1223 set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
3270958b 1224 }
7ce86d5a 1225
a12a1ac7 1226out_close:
d81b8a40 1227 CIFSSMBClose(xid, tcon, fid.netfid);
a12a1ac7 1228out:
3270958b 1229 kfree(info_buf);
7ffec372 1230 cifs_put_tlink(tlink);
a12a1ac7 1231 return rc;
3270958b
SF
1232
1233 /*
1234 * reset everything back to the original state. Don't bother
1235 * dealing with errors here since we can't do anything about
1236 * them anyway.
1237 */
1238undo_rename:
d81b8a40 1239 CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
2baa2682 1240 cifs_sb->local_nls, cifs_remap(cifs_sb));
3270958b
SF
1241undo_setattr:
1242 if (dosattr != origattr) {
1243 info_buf->Attributes = cpu_to_le32(origattr);
d81b8a40 1244 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
3270958b
SF
1245 current->tgid))
1246 cifsInode->cifsAttrs = origattr;
1247 }
1248
1249 goto out_close;
a12a1ac7
JL
1250}
1251
b7ca6928
SF
1252/* copied from fs/nfs/dir.c with small changes */
1253static void
1254cifs_drop_nlink(struct inode *inode)
1255{
1256 spin_lock(&inode->i_lock);
1257 if (inode->i_nlink > 0)
1258 drop_nlink(inode);
1259 spin_unlock(&inode->i_lock);
1260}
ff694527
SF
1261
1262/*
2b0143b5 1263 * If d_inode(dentry) is null (usually meaning the cached dentry
ff694527 1264 * is a negative dentry) then we would attempt a standard SMB delete, but
af901ca1
AGR
1265 * if that fails we can not attempt the fall back mechanisms on EACCESS
1266 * but will return the EACCESS to the caller. Note that the VFS does not call
ff694527
SF
1267 * unlink on negative dentries currently.
1268 */
5f0319a7 1269int cifs_unlink(struct inode *dir, struct dentry *dentry)
1da177e4
LT
1270{
1271 int rc = 0;
6d5786a3 1272 unsigned int xid;
1da177e4 1273 char *full_path = NULL;
2b0143b5 1274 struct inode *inode = d_inode(dentry);
ff694527 1275 struct cifsInodeInfo *cifs_inode;
5f0319a7
JL
1276 struct super_block *sb = dir->i_sb;
1277 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
7ffec372 1278 struct tcon_link *tlink;
96daf2b0 1279 struct cifs_tcon *tcon;
ed6875e0 1280 struct TCP_Server_Info *server;
6050247d
SF
1281 struct iattr *attrs = NULL;
1282 __u32 dosattr = 0, origattr = 0;
1da177e4 1283
f96637be 1284 cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
1da177e4 1285
7ffec372
JL
1286 tlink = cifs_sb_tlink(cifs_sb);
1287 if (IS_ERR(tlink))
1288 return PTR_ERR(tlink);
1289 tcon = tlink_tcon(tlink);
ed6875e0 1290 server = tcon->ses->server;
7ffec372 1291
6d5786a3 1292 xid = get_xid();
1da177e4 1293
5f0319a7
JL
1294 /* Unlink can be called from rename so we can not take the
1295 * sb->s_vfs_rename_mutex here */
1296 full_path = build_path_from_dentry(dentry);
1da177e4 1297 if (full_path == NULL) {
0f3bc09e 1298 rc = -ENOMEM;
7ffec372 1299 goto unlink_out;
1da177e4 1300 }
2d785a50 1301
29e20f9c
PS
1302 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1303 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
5f0319a7 1304 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
2d785a50 1305 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
2baa2682 1306 cifs_remap(cifs_sb));
f96637be 1307 cifs_dbg(FYI, "posix del rc %d\n", rc);
2d785a50
SF
1308 if ((rc == 0) || (rc == -ENOENT))
1309 goto psx_del_no_retry;
1310 }
1da177e4 1311
6050247d 1312retry_std_delete:
ed6875e0
PS
1313 if (!server->ops->unlink) {
1314 rc = -ENOSYS;
1315 goto psx_del_no_retry;
1316 }
1317
1318 rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
6050247d 1319
2d785a50 1320psx_del_no_retry:
1da177e4 1321 if (!rc) {
5f0319a7 1322 if (inode)
b7ca6928 1323 cifs_drop_nlink(inode);
1da177e4 1324 } else if (rc == -ENOENT) {
5f0319a7 1325 d_drop(dentry);
47c78f4a 1326 } else if (rc == -EBUSY) {
ed6875e0
PS
1327 if (server->ops->rename_pending_delete) {
1328 rc = server->ops->rename_pending_delete(full_path,
1329 dentry, xid);
1330 if (rc == 0)
1331 cifs_drop_nlink(inode);
1332 }
ff694527 1333 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
388e57b2
SF
1334 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1335 if (attrs == NULL) {
1336 rc = -ENOMEM;
1337 goto out_reval;
1da177e4 1338 }
388e57b2
SF
1339
1340 /* try to reset dos attributes */
ff694527
SF
1341 cifs_inode = CIFS_I(inode);
1342 origattr = cifs_inode->cifsAttrs;
6050247d
SF
1343 if (origattr == 0)
1344 origattr |= ATTR_NORMAL;
1345 dosattr = origattr & ~ATTR_READONLY;
388e57b2
SF
1346 if (dosattr == 0)
1347 dosattr |= ATTR_NORMAL;
1348 dosattr |= ATTR_HIDDEN;
1349
1350 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
388e57b2
SF
1351 if (rc != 0)
1352 goto out_reval;
6050247d
SF
1353
1354 goto retry_std_delete;
1da177e4 1355 }
6050247d
SF
1356
1357 /* undo the setattr if we errored out and it's needed */
1358 if (rc != 0 && dosattr != 0)
1359 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1360
388e57b2 1361out_reval:
4523cc30 1362 if (inode) {
ff694527
SF
1363 cifs_inode = CIFS_I(inode);
1364 cifs_inode->time = 0; /* will force revalidate to get info
5f0319a7
JL
1365 when needed */
1366 inode->i_ctime = current_fs_time(sb);
06bcfedd 1367 }
5f0319a7 1368 dir->i_ctime = dir->i_mtime = current_fs_time(sb);
ff694527 1369 cifs_inode = CIFS_I(dir);
6050247d 1370 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
7ffec372 1371unlink_out:
1da177e4 1372 kfree(full_path);
6050247d 1373 kfree(attrs);
6d5786a3 1374 free_xid(xid);
7ffec372 1375 cifs_put_tlink(tlink);
1da177e4
LT
1376 return rc;
1377}
1378
ff691e96 1379static int
101b92d9 1380cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
ff691e96
PS
1381 const char *full_path, struct cifs_sb_info *cifs_sb,
1382 struct cifs_tcon *tcon, const unsigned int xid)
1383{
1384 int rc = 0;
101b92d9 1385 struct inode *inode = NULL;
ff691e96
PS
1386
1387 if (tcon->unix_ext)
101b92d9 1388 rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
ff691e96
PS
1389 xid);
1390 else
101b92d9
JL
1391 rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
1392 xid, NULL);
1393
ff691e96
PS
1394 if (rc)
1395 return rc;
1396
ff691e96
PS
1397 /*
1398 * setting nlink not necessary except in cases where we failed to get it
101b92d9
JL
1399 * from the server or was set bogus. Also, since this is a brand new
1400 * inode, no need to grab the i_lock before setting the i_nlink.
ff691e96 1401 */
101b92d9
JL
1402 if (inode->i_nlink < 2)
1403 set_nlink(inode, 2);
ff691e96
PS
1404 mode &= ~current_umask();
1405 /* must turn on setgid bit if parent dir has it */
101b92d9 1406 if (parent->i_mode & S_ISGID)
ff691e96
PS
1407 mode |= S_ISGID;
1408
1409 if (tcon->unix_ext) {
1410 struct cifs_unix_set_info_args args = {
1411 .mode = mode,
1412 .ctime = NO_CHANGE_64,
1413 .atime = NO_CHANGE_64,
1414 .mtime = NO_CHANGE_64,
1415 .device = 0,
1416 };
1417 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
49418b2c 1418 args.uid = current_fsuid();
101b92d9 1419 if (parent->i_mode & S_ISGID)
49418b2c 1420 args.gid = parent->i_gid;
ff691e96 1421 else
49418b2c 1422 args.gid = current_fsgid();
ff691e96 1423 } else {
49418b2c
EB
1424 args.uid = INVALID_UID; /* no change */
1425 args.gid = INVALID_GID; /* no change */
ff691e96
PS
1426 }
1427 CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1428 cifs_sb->local_nls,
2baa2682 1429 cifs_remap(cifs_sb));
ff691e96 1430 } else {
f436720e 1431 struct TCP_Server_Info *server = tcon->ses->server;
ff691e96 1432 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
f436720e 1433 (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
101b92d9 1434 server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
f436720e 1435 tcon, xid);
101b92d9
JL
1436 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
1437 inode->i_mode = (mode | S_IFDIR);
1438
1439 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1440 inode->i_uid = current_fsuid();
1441 if (inode->i_mode & S_ISGID)
1442 inode->i_gid = parent->i_gid;
1443 else
1444 inode->i_gid = current_fsgid();
ff691e96
PS
1445 }
1446 }
101b92d9 1447 d_instantiate(dentry, inode);
ff691e96
PS
1448 return rc;
1449}
1450
1451static int
1452cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
1453 const char *full_path, struct cifs_sb_info *cifs_sb,
1454 struct cifs_tcon *tcon, const unsigned int xid)
1455{
1456 int rc = 0;
1457 u32 oplock = 0;
1458 FILE_UNIX_BASIC_INFO *info = NULL;
1459 struct inode *newinode = NULL;
1460 struct cifs_fattr fattr;
1461
1462 info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1463 if (info == NULL) {
1464 rc = -ENOMEM;
1465 goto posix_mkdir_out;
1466 }
1467
1468 mode &= ~current_umask();
1469 rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
1470 NULL /* netfid */, info, &oplock, full_path,
2baa2682 1471 cifs_sb->local_nls, cifs_remap(cifs_sb));
ff691e96
PS
1472 if (rc == -EOPNOTSUPP)
1473 goto posix_mkdir_out;
1474 else if (rc) {
f96637be 1475 cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
ff691e96
PS
1476 d_drop(dentry);
1477 goto posix_mkdir_out;
1478 }
1479
1480 if (info->Type == cpu_to_le32(-1))
1481 /* no return info, go query for it */
1482 goto posix_mkdir_get_info;
1483 /*
1484 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
1485 * need to set uid/gid.
1486 */
1487
1488 cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
1489 cifs_fill_uniqueid(inode->i_sb, &fattr);
1490 newinode = cifs_iget(inode->i_sb, &fattr);
1491 if (!newinode)
1492 goto posix_mkdir_get_info;
1493
1494 d_instantiate(dentry, newinode);
1495
1496#ifdef CONFIG_CIFS_DEBUG2
35c265e0
AV
1497 cifs_dbg(FYI, "instantiated dentry %p %pd to inode %p\n",
1498 dentry, dentry, newinode);
ff691e96
PS
1499
1500 if (newinode->i_nlink != 2)
f96637be
JP
1501 cifs_dbg(FYI, "unexpected number of links %d\n",
1502 newinode->i_nlink);
ff691e96
PS
1503#endif
1504
1505posix_mkdir_out:
1506 kfree(info);
1507 return rc;
1508posix_mkdir_get_info:
1509 rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
1510 xid);
1511 goto posix_mkdir_out;
1512}
1513
18bb1db3 1514int cifs_mkdir(struct inode *inode, struct dentry *direntry, umode_t mode)
1da177e4 1515{
ff691e96 1516 int rc = 0;
6d5786a3 1517 unsigned int xid;
1da177e4 1518 struct cifs_sb_info *cifs_sb;
7ffec372 1519 struct tcon_link *tlink;
29e20f9c 1520 struct cifs_tcon *tcon;
f436720e 1521 struct TCP_Server_Info *server;
ff691e96 1522 char *full_path;
1da177e4 1523
f96637be
JP
1524 cifs_dbg(FYI, "In cifs_mkdir, mode = 0x%hx inode = 0x%p\n",
1525 mode, inode);
1da177e4 1526
1da177e4 1527 cifs_sb = CIFS_SB(inode->i_sb);
7ffec372
JL
1528 tlink = cifs_sb_tlink(cifs_sb);
1529 if (IS_ERR(tlink))
1530 return PTR_ERR(tlink);
29e20f9c 1531 tcon = tlink_tcon(tlink);
7ffec372 1532
6d5786a3 1533 xid = get_xid();
1da177e4 1534
7f57356b 1535 full_path = build_path_from_dentry(direntry);
1da177e4 1536 if (full_path == NULL) {
0f3bc09e 1537 rc = -ENOMEM;
7ffec372 1538 goto mkdir_out;
1da177e4 1539 }
50c2f753 1540
29e20f9c
PS
1541 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1542 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
ff691e96
PS
1543 rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
1544 tcon, xid);
1545 if (rc != -EOPNOTSUPP)
2dd29d31 1546 goto mkdir_out;
fb8c4b14 1547 }
ff691e96 1548
f436720e
PS
1549 server = tcon->ses->server;
1550
1551 if (!server->ops->mkdir) {
1552 rc = -ENOSYS;
1553 goto mkdir_out;
1554 }
1555
1da177e4 1556 /* BB add setting the equivalent of mode via CreateX w/ACLs */
f436720e 1557 rc = server->ops->mkdir(xid, tcon, full_path, cifs_sb);
1da177e4 1558 if (rc) {
f96637be 1559 cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
1da177e4 1560 d_drop(direntry);
ff691e96 1561 goto mkdir_out;
1da177e4 1562 }
ff691e96
PS
1563
1564 rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
1565 xid);
fb8c4b14 1566mkdir_out:
6de2ce42
PS
1567 /*
1568 * Force revalidate to get parent dir info when needed since cached
1569 * attributes are invalid now.
1570 */
1571 CIFS_I(inode)->time = 0;
1da177e4 1572 kfree(full_path);
6d5786a3 1573 free_xid(xid);
7ffec372 1574 cifs_put_tlink(tlink);
1da177e4
LT
1575 return rc;
1576}
1577
1578int cifs_rmdir(struct inode *inode, struct dentry *direntry)
1579{
1580 int rc = 0;
6d5786a3 1581 unsigned int xid;
1da177e4 1582 struct cifs_sb_info *cifs_sb;
7ffec372 1583 struct tcon_link *tlink;
f958ca5d
PS
1584 struct cifs_tcon *tcon;
1585 struct TCP_Server_Info *server;
1da177e4
LT
1586 char *full_path = NULL;
1587 struct cifsInodeInfo *cifsInode;
1588
f96637be 1589 cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
1da177e4 1590
6d5786a3 1591 xid = get_xid();
1da177e4 1592
7f57356b 1593 full_path = build_path_from_dentry(direntry);
1da177e4 1594 if (full_path == NULL) {
0f3bc09e 1595 rc = -ENOMEM;
7ffec372 1596 goto rmdir_exit;
1da177e4
LT
1597 }
1598
7ffec372
JL
1599 cifs_sb = CIFS_SB(inode->i_sb);
1600 tlink = cifs_sb_tlink(cifs_sb);
1601 if (IS_ERR(tlink)) {
1602 rc = PTR_ERR(tlink);
1603 goto rmdir_exit;
1604 }
f958ca5d
PS
1605 tcon = tlink_tcon(tlink);
1606 server = tcon->ses->server;
1607
1608 if (!server->ops->rmdir) {
1609 rc = -ENOSYS;
1610 cifs_put_tlink(tlink);
1611 goto rmdir_exit;
1612 }
7ffec372 1613
f958ca5d 1614 rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
7ffec372 1615 cifs_put_tlink(tlink);
1da177e4
LT
1616
1617 if (!rc) {
2b0143b5
DH
1618 spin_lock(&d_inode(direntry)->i_lock);
1619 i_size_write(d_inode(direntry), 0);
1620 clear_nlink(d_inode(direntry));
1621 spin_unlock(&d_inode(direntry)->i_lock);
1da177e4
LT
1622 }
1623
2b0143b5 1624 cifsInode = CIFS_I(d_inode(direntry));
6de2ce42
PS
1625 /* force revalidate to go get info when needed */
1626 cifsInode->time = 0;
42c24544
SF
1627
1628 cifsInode = CIFS_I(inode);
6de2ce42
PS
1629 /*
1630 * Force revalidate to get parent dir info when needed since cached
1631 * attributes are invalid now.
1632 */
1633 cifsInode->time = 0;
42c24544 1634
2b0143b5 1635 d_inode(direntry)->i_ctime = inode->i_ctime = inode->i_mtime =
1da177e4
LT
1636 current_fs_time(inode->i_sb);
1637
7ffec372 1638rmdir_exit:
1da177e4 1639 kfree(full_path);
6d5786a3 1640 free_xid(xid);
1da177e4
LT
1641 return rc;
1642}
1643
ee2fd967 1644static int
8ceb9843
PS
1645cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
1646 const char *from_path, struct dentry *to_dentry,
1647 const char *to_path)
ee2fd967
SF
1648{
1649 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
7ffec372 1650 struct tcon_link *tlink;
8ceb9843
PS
1651 struct cifs_tcon *tcon;
1652 struct TCP_Server_Info *server;
d81b8a40
PS
1653 struct cifs_fid fid;
1654 struct cifs_open_parms oparms;
ee2fd967
SF
1655 int oplock, rc;
1656
7ffec372
JL
1657 tlink = cifs_sb_tlink(cifs_sb);
1658 if (IS_ERR(tlink))
1659 return PTR_ERR(tlink);
8ceb9843
PS
1660 tcon = tlink_tcon(tlink);
1661 server = tcon->ses->server;
1662
1663 if (!server->ops->rename)
1664 return -ENOSYS;
7ffec372 1665
ee2fd967 1666 /* try path-based rename first */
8ceb9843 1667 rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
ee2fd967
SF
1668
1669 /*
8ceb9843
PS
1670 * Don't bother with rename by filehandle unless file is busy and
1671 * source. Note that cross directory moves do not work with
ee2fd967
SF
1672 * rename by filehandle to various Windows servers.
1673 */
47c78f4a 1674 if (rc == 0 || rc != -EBUSY)
7ffec372 1675 goto do_rename_exit;
ee2fd967 1676
ed0e3ace
JL
1677 /* open-file renames don't work across directories */
1678 if (to_dentry->d_parent != from_dentry->d_parent)
7ffec372 1679 goto do_rename_exit;
ed0e3ace 1680
d81b8a40
PS
1681 oparms.tcon = tcon;
1682 oparms.cifs_sb = cifs_sb;
ee2fd967 1683 /* open the file to be renamed -- we need DELETE perms */
d81b8a40
PS
1684 oparms.desired_access = DELETE;
1685 oparms.create_options = CREATE_NOT_DIR;
1686 oparms.disposition = FILE_OPEN;
1687 oparms.path = from_path;
1688 oparms.fid = &fid;
1689 oparms.reconnect = false;
1690
1691 rc = CIFS_open(xid, &oparms, &oplock, NULL);
ee2fd967 1692 if (rc == 0) {
d81b8a40 1693 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
ee2fd967 1694 (const char *) to_dentry->d_name.name,
2baa2682 1695 cifs_sb->local_nls, cifs_remap(cifs_sb));
d81b8a40 1696 CIFSSMBClose(xid, tcon, fid.netfid);
ee2fd967 1697 }
7ffec372
JL
1698do_rename_exit:
1699 cifs_put_tlink(tlink);
ee2fd967
SF
1700 return rc;
1701}
1702
8ceb9843 1703int
7c33d597
MS
1704cifs_rename2(struct inode *source_dir, struct dentry *source_dentry,
1705 struct inode *target_dir, struct dentry *target_dentry,
1706 unsigned int flags)
1da177e4 1707{
8ceb9843
PS
1708 char *from_name = NULL;
1709 char *to_name = NULL;
639e7a91 1710 struct cifs_sb_info *cifs_sb;
7ffec372 1711 struct tcon_link *tlink;
96daf2b0 1712 struct cifs_tcon *tcon;
ee2fd967
SF
1713 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
1714 FILE_UNIX_BASIC_INFO *info_buf_target;
6d5786a3
PS
1715 unsigned int xid;
1716 int rc, tmprc;
1da177e4 1717
7c33d597
MS
1718 if (flags & ~RENAME_NOREPLACE)
1719 return -EINVAL;
1720
639e7a91 1721 cifs_sb = CIFS_SB(source_dir->i_sb);
7ffec372
JL
1722 tlink = cifs_sb_tlink(cifs_sb);
1723 if (IS_ERR(tlink))
1724 return PTR_ERR(tlink);
1725 tcon = tlink_tcon(tlink);
1da177e4 1726
6d5786a3 1727 xid = get_xid();
ee2fd967 1728
ee2fd967
SF
1729 /*
1730 * we already have the rename sem so we do not need to
1731 * grab it again here to protect the path integrity
1732 */
8ceb9843
PS
1733 from_name = build_path_from_dentry(source_dentry);
1734 if (from_name == NULL) {
ee2fd967
SF
1735 rc = -ENOMEM;
1736 goto cifs_rename_exit;
1737 }
1738
8ceb9843
PS
1739 to_name = build_path_from_dentry(target_dentry);
1740 if (to_name == NULL) {
1da177e4
LT
1741 rc = -ENOMEM;
1742 goto cifs_rename_exit;
1743 }
1744
8ceb9843
PS
1745 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
1746 to_name);
ee2fd967 1747
7c33d597
MS
1748 /*
1749 * No-replace is the natural behavior for CIFS, so skip unlink hacks.
1750 */
1751 if (flags & RENAME_NOREPLACE)
1752 goto cifs_rename_exit;
1753
14121bdc
JL
1754 if (rc == -EEXIST && tcon->unix_ext) {
1755 /*
8ceb9843
PS
1756 * Are src and dst hardlinks of same inode? We can only tell
1757 * with unix extensions enabled.
14121bdc
JL
1758 */
1759 info_buf_source =
1760 kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO),
1761 GFP_KERNEL);
1762 if (info_buf_source == NULL) {
1763 rc = -ENOMEM;
1764 goto cifs_rename_exit;
1765 }
1766
1767 info_buf_target = info_buf_source + 1;
8ceb9843
PS
1768 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
1769 info_buf_source,
1770 cifs_sb->local_nls,
2baa2682 1771 cifs_remap(cifs_sb));
8d281efb 1772 if (tmprc != 0)
14121bdc 1773 goto unlink_target;
ee2fd967 1774
8ceb9843
PS
1775 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
1776 info_buf_target,
1777 cifs_sb->local_nls,
2baa2682 1778 cifs_remap(cifs_sb));
14121bdc 1779
8d281efb 1780 if (tmprc == 0 && (info_buf_source->UniqueId ==
ae6884a9 1781 info_buf_target->UniqueId)) {
14121bdc 1782 /* same file, POSIX says that this is a noop */
ae6884a9 1783 rc = 0;
14121bdc 1784 goto cifs_rename_exit;
ae6884a9 1785 }
8ceb9843
PS
1786 }
1787 /*
1788 * else ... BB we could add the same check for Windows by
1789 * checking the UniqueId via FILE_INTERNAL_INFO
1790 */
14121bdc 1791
ee2fd967 1792unlink_target:
fc6f3943 1793 /* Try unlinking the target dentry if it's not negative */
2b0143b5 1794 if (d_really_is_positive(target_dentry) && (rc == -EACCES || rc == -EEXIST)) {
a07d3220
PS
1795 if (d_is_dir(target_dentry))
1796 tmprc = cifs_rmdir(target_dir, target_dentry);
1797 else
1798 tmprc = cifs_unlink(target_dir, target_dentry);
14121bdc
JL
1799 if (tmprc)
1800 goto cifs_rename_exit;
8ceb9843
PS
1801 rc = cifs_do_rename(xid, source_dentry, from_name,
1802 target_dentry, to_name);
1da177e4
LT
1803 }
1804
b46799a8
PS
1805 /* force revalidate to go get info when needed */
1806 CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
1807
1808 source_dir->i_ctime = source_dir->i_mtime = target_dir->i_ctime =
1809 target_dir->i_mtime = current_fs_time(source_dir->i_sb);
1810
1da177e4 1811cifs_rename_exit:
ee2fd967 1812 kfree(info_buf_source);
8ceb9843
PS
1813 kfree(from_name);
1814 kfree(to_name);
6d5786a3 1815 free_xid(xid);
7ffec372 1816 cifs_put_tlink(tlink);
1da177e4
LT
1817 return rc;
1818}
1819
df2cf170
JL
1820static bool
1821cifs_inode_needs_reval(struct inode *inode)
1da177e4 1822{
df2cf170 1823 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
6d20e840 1824 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1da177e4 1825
18cceb6a 1826 if (CIFS_CACHE_READ(cifs_i))
df2cf170 1827 return false;
1da177e4 1828
df2cf170
JL
1829 if (!lookupCacheEnabled)
1830 return true;
1da177e4 1831
df2cf170
JL
1832 if (cifs_i->time == 0)
1833 return true;
1da177e4 1834
a87c9ad9
JL
1835 if (!cifs_sb->actimeo)
1836 return true;
1837
6d20e840
SJ
1838 if (!time_in_range(jiffies, cifs_i->time,
1839 cifs_i->time + cifs_sb->actimeo))
df2cf170
JL
1840 return true;
1841
db19272e 1842 /* hardlinked files w/ noserverino get "special" treatment */
6d20e840 1843 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
db19272e
JL
1844 S_ISREG(inode->i_mode) && inode->i_nlink != 1)
1845 return true;
1846
df2cf170
JL
1847 return false;
1848}
1849
523fb8c8
SJ
1850/*
1851 * Zap the cache. Called when invalid_mapping flag is set.
1852 */
6feb9891 1853int
df2cf170
JL
1854cifs_invalidate_mapping(struct inode *inode)
1855{
6feb9891 1856 int rc = 0;
df2cf170 1857
df2cf170 1858 if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
257fb1f1 1859 rc = invalidate_inode_pages2(inode->i_mapping);
4f73c7d3 1860 if (rc)
f96637be
JP
1861 cifs_dbg(VFS, "%s: could not invalidate inode %p\n",
1862 __func__, inode);
df2cf170 1863 }
257fb1f1 1864
9451a9a5 1865 cifs_fscache_reset_inode_cookie(inode);
6feb9891 1866 return rc;
df2cf170
JL
1867}
1868
4f73c7d3
JL
1869/**
1870 * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
1871 * @word: long word containing the bit lock
1872 */
1873static int
dfd01f02 1874cifs_wait_bit_killable(struct wait_bit_key *key, int mode)
4f73c7d3 1875{
4f73c7d3 1876 freezable_schedule_unsafe();
dfd01f02
PZ
1877 if (signal_pending_state(mode, current))
1878 return -ERESTARTSYS;
4f73c7d3
JL
1879 return 0;
1880}
1881
e284e53f
JL
1882int
1883cifs_revalidate_mapping(struct inode *inode)
1884{
4f73c7d3
JL
1885 int rc;
1886 unsigned long *flags = &CIFS_I(inode)->flags;
1887
74316201
N
1888 rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
1889 TASK_KILLABLE);
4f73c7d3
JL
1890 if (rc)
1891 return rc;
1892
1893 if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
1894 rc = cifs_invalidate_mapping(inode);
1895 if (rc)
1896 set_bit(CIFS_INO_INVALID_MAPPING, flags);
1897 }
1898
1899 clear_bit_unlock(CIFS_INO_LOCK, flags);
b1cce803 1900 smp_mb__after_atomic();
4f73c7d3
JL
1901 wake_up_bit(flags, CIFS_INO_LOCK);
1902
1903 return rc;
1904}
1905
1906int
1907cifs_zap_mapping(struct inode *inode)
1908{
1909 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
1910 return cifs_revalidate_mapping(inode);
e284e53f
JL
1911}
1912
6feb9891 1913int cifs_revalidate_file_attr(struct file *filp)
abab095d
JL
1914{
1915 int rc = 0;
496ad9aa 1916 struct inode *inode = file_inode(filp);
ba00ba64 1917 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
abab095d
JL
1918
1919 if (!cifs_inode_needs_reval(inode))
6feb9891 1920 return rc;
abab095d 1921
13cfb733 1922 if (tlink_tcon(cfile->tlink)->unix_ext)
abab095d
JL
1923 rc = cifs_get_file_info_unix(filp);
1924 else
1925 rc = cifs_get_file_info(filp);
1926
abab095d
JL
1927 return rc;
1928}
1929
6feb9891 1930int cifs_revalidate_dentry_attr(struct dentry *dentry)
df2cf170 1931{
6d5786a3 1932 unsigned int xid;
df2cf170 1933 int rc = 0;
2b0143b5 1934 struct inode *inode = d_inode(dentry);
df2cf170 1935 struct super_block *sb = dentry->d_sb;
6feb9891 1936 char *full_path = NULL;
df2cf170
JL
1937
1938 if (inode == NULL)
1939 return -ENOENT;
1da177e4 1940
df2cf170 1941 if (!cifs_inode_needs_reval(inode))
6feb9891
PS
1942 return rc;
1943
6d5786a3 1944 xid = get_xid();
1da177e4
LT
1945
1946 /* can not safely grab the rename sem here if rename calls revalidate
1947 since that would deadlock */
df2cf170 1948 full_path = build_path_from_dentry(dentry);
1da177e4 1949 if (full_path == NULL) {
0f3bc09e 1950 rc = -ENOMEM;
6feb9891 1951 goto out;
1da177e4
LT
1952 }
1953
f96637be
JP
1954 cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
1955 full_path, inode, inode->i_count.counter,
a00be0e3 1956 dentry, cifs_get_time(dentry), jiffies);
1da177e4 1957
0d424ad0 1958 if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
df2cf170
JL
1959 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
1960 else
1961 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
1962 xid, NULL);
1da177e4 1963
6feb9891 1964out:
1da177e4 1965 kfree(full_path);
6d5786a3 1966 free_xid(xid);
1da177e4
LT
1967 return rc;
1968}
1969
6feb9891
PS
1970int cifs_revalidate_file(struct file *filp)
1971{
1972 int rc;
496ad9aa 1973 struct inode *inode = file_inode(filp);
6feb9891
PS
1974
1975 rc = cifs_revalidate_file_attr(filp);
1976 if (rc)
1977 return rc;
1978
e284e53f 1979 return cifs_revalidate_mapping(inode);
6feb9891
PS
1980}
1981
1982/* revalidate a dentry's inode attributes */
1983int cifs_revalidate_dentry(struct dentry *dentry)
1984{
1985 int rc;
2b0143b5 1986 struct inode *inode = d_inode(dentry);
6feb9891
PS
1987
1988 rc = cifs_revalidate_dentry_attr(dentry);
1989 if (rc)
1990 return rc;
1991
e284e53f 1992 return cifs_revalidate_mapping(inode);
6feb9891
PS
1993}
1994
1da177e4 1995int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1c456013 1996 struct kstat *stat)
1da177e4 1997{
3aa1c8c2 1998 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
96daf2b0 1999 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
2b0143b5 2000 struct inode *inode = d_inode(dentry);
6feb9891 2001 int rc;
3aa1c8c2 2002
6feb9891
PS
2003 /*
2004 * We need to be sure that all dirty pages are written and the server
2005 * has actual ctime, mtime and file length.
2006 */
18cceb6a 2007 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
6feb9891
PS
2008 inode->i_mapping->nrpages != 0) {
2009 rc = filemap_fdatawait(inode->i_mapping);
156ecb2d
SF
2010 if (rc) {
2011 mapping_set_error(inode->i_mapping, rc);
2012 return rc;
2013 }
6feb9891 2014 }
1c456013 2015
6feb9891
PS
2016 rc = cifs_revalidate_dentry_attr(dentry);
2017 if (rc)
2018 return rc;
2019
2020 generic_fillattr(inode, stat);
2021 stat->blksize = CIFS_MAX_MSGSIZE;
2022 stat->ino = CIFS_I(inode)->uniqueid;
2023
2024 /*
d3d1fce1
JL
2025 * If on a multiuser mount without unix extensions or cifsacl being
2026 * enabled, and the admin hasn't overridden them, set the ownership
2027 * to the fsuid/fsgid of the current process.
6feb9891
PS
2028 */
2029 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
d3d1fce1 2030 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
6feb9891
PS
2031 !tcon->unix_ext) {
2032 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
2033 stat->uid = current_fsuid();
2034 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
2035 stat->gid = current_fsgid();
5fe14c85 2036 }
6feb9891 2037 return rc;
1da177e4
LT
2038}
2039
2040static int cifs_truncate_page(struct address_space *mapping, loff_t from)
2041{
09cbfeaf
KS
2042 pgoff_t index = from >> PAGE_SHIFT;
2043 unsigned offset = from & (PAGE_SIZE - 1);
1da177e4 2044 struct page *page;
1da177e4
LT
2045 int rc = 0;
2046
2047 page = grab_cache_page(mapping, index);
2048 if (!page)
2049 return -ENOMEM;
2050
09cbfeaf 2051 zero_user_segment(page, offset, PAGE_SIZE);
1da177e4 2052 unlock_page(page);
09cbfeaf 2053 put_page(page);
1da177e4
LT
2054 return rc;
2055}
2056
1b947463 2057static void cifs_setsize(struct inode *inode, loff_t offset)
3677db10 2058{
ba6a46a0 2059 spin_lock(&inode->i_lock);
3677db10 2060 i_size_write(inode, offset);
ba6a46a0 2061 spin_unlock(&inode->i_lock);
1b947463 2062
7caef267 2063 truncate_pagecache(inode, offset);
3677db10
SF
2064}
2065
8efdbde6
JL
2066static int
2067cifs_set_file_size(struct inode *inode, struct iattr *attrs,
6d5786a3 2068 unsigned int xid, char *full_path)
8efdbde6
JL
2069{
2070 int rc;
2071 struct cifsFileInfo *open_file;
2072 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2073 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
7ffec372 2074 struct tcon_link *tlink = NULL;
d1433418
PS
2075 struct cifs_tcon *tcon = NULL;
2076 struct TCP_Server_Info *server;
8efdbde6
JL
2077
2078 /*
2079 * To avoid spurious oplock breaks from server, in the case of
2080 * inodes that we already have open, avoid doing path based
2081 * setting of file size if we can do it by handle.
2082 * This keeps our caching token (oplock) and avoids timeouts
2083 * when the local oplock break takes longer to flush
2084 * writebehind data than the SMB timeout for the SetPathInfo
2085 * request would allow
2086 */
6508d904 2087 open_file = find_writable_file(cifsInode, true);
8efdbde6 2088 if (open_file) {
d1433418
PS
2089 tcon = tlink_tcon(open_file->tlink);
2090 server = tcon->ses->server;
2091 if (server->ops->set_file_size)
2092 rc = server->ops->set_file_size(xid, tcon, open_file,
2093 attrs->ia_size, false);
2094 else
2095 rc = -ENOSYS;
6ab409b5 2096 cifsFileInfo_put(open_file);
f96637be 2097 cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
8efdbde6
JL
2098 } else
2099 rc = -EINVAL;
2100
d1433418
PS
2101 if (!rc)
2102 goto set_size_out;
2103
2104 if (tcon == NULL) {
2105 tlink = cifs_sb_tlink(cifs_sb);
2106 if (IS_ERR(tlink))
2107 return PTR_ERR(tlink);
2108 tcon = tlink_tcon(tlink);
2109 server = tcon->ses->server;
2110 }
ba00ba64 2111
d1433418
PS
2112 /*
2113 * Set file size by pathname rather than by handle either because no
2114 * valid, writeable file handle for it was found or because there was
2115 * an error setting it by handle.
2116 */
2117 if (server->ops->set_path_size)
2118 rc = server->ops->set_path_size(xid, tcon, full_path,
2119 attrs->ia_size, cifs_sb, false);
2120 else
2121 rc = -ENOSYS;
f96637be 2122 cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
d1433418 2123
d1433418
PS
2124 if (tlink)
2125 cifs_put_tlink(tlink);
8efdbde6 2126
d1433418 2127set_size_out:
8efdbde6 2128 if (rc == 0) {
fbec9ab9 2129 cifsInode->server_eof = attrs->ia_size;
1b947463 2130 cifs_setsize(inode, attrs->ia_size);
8efdbde6
JL
2131 cifs_truncate_page(inode->i_mapping, inode->i_size);
2132 }
2133
2134 return rc;
2135}
2136
3fe5c1dd
JL
2137static int
2138cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
2139{
2140 int rc;
6d5786a3 2141 unsigned int xid;
3fe5c1dd 2142 char *full_path = NULL;
2b0143b5 2143 struct inode *inode = d_inode(direntry);
3fe5c1dd
JL
2144 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2145 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
7ffec372 2146 struct tcon_link *tlink;
96daf2b0 2147 struct cifs_tcon *pTcon;
3fe5c1dd 2148 struct cifs_unix_set_info_args *args = NULL;
3bbeeb3c 2149 struct cifsFileInfo *open_file;
3fe5c1dd 2150
35c265e0
AV
2151 cifs_dbg(FYI, "setattr_unix on file %pd attrs->ia_valid=0x%x\n",
2152 direntry, attrs->ia_valid);
3fe5c1dd 2153
6d5786a3 2154 xid = get_xid();
3fe5c1dd 2155
db78b877
CH
2156 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2157 attrs->ia_valid |= ATTR_FORCE;
2158
31051c85 2159 rc = setattr_prepare(direntry, attrs);
db78b877
CH
2160 if (rc < 0)
2161 goto out;
3fe5c1dd
JL
2162
2163 full_path = build_path_from_dentry(direntry);
2164 if (full_path == NULL) {
2165 rc = -ENOMEM;
2166 goto out;
2167 }
2168
0f4d634c
JL
2169 /*
2170 * Attempt to flush data before changing attributes. We need to do
2171 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2172 * ownership or mode then we may also need to do this. Here, we take
2173 * the safe way out and just do the flush on all setattr requests. If
2174 * the flush returns error, store it to report later and continue.
2175 *
2176 * BB: This should be smarter. Why bother flushing pages that
2177 * will be truncated anyway? Also, should we error out here if
2178 * the flush returns error?
2179 */
2180 rc = filemap_write_and_wait(inode->i_mapping);
eb4b756b
JL
2181 mapping_set_error(inode->i_mapping, rc);
2182 rc = 0;
3fe5c1dd
JL
2183
2184 if (attrs->ia_valid & ATTR_SIZE) {
2185 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2186 if (rc != 0)
2187 goto out;
2188 }
2189
2190 /* skip mode change if it's just for clearing setuid/setgid */
2191 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2192 attrs->ia_valid &= ~ATTR_MODE;
2193
2194 args = kmalloc(sizeof(*args), GFP_KERNEL);
2195 if (args == NULL) {
2196 rc = -ENOMEM;
2197 goto out;
2198 }
2199
2200 /* set up the struct */
2201 if (attrs->ia_valid & ATTR_MODE)
2202 args->mode = attrs->ia_mode;
2203 else
2204 args->mode = NO_CHANGE_64;
2205
2206 if (attrs->ia_valid & ATTR_UID)
2207 args->uid = attrs->ia_uid;
2208 else
49418b2c 2209 args->uid = INVALID_UID; /* no change */
3fe5c1dd
JL
2210
2211 if (attrs->ia_valid & ATTR_GID)
2212 args->gid = attrs->ia_gid;
2213 else
49418b2c 2214 args->gid = INVALID_GID; /* no change */
3fe5c1dd
JL
2215
2216 if (attrs->ia_valid & ATTR_ATIME)
2217 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
2218 else
2219 args->atime = NO_CHANGE_64;
2220
2221 if (attrs->ia_valid & ATTR_MTIME)
2222 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
2223 else
2224 args->mtime = NO_CHANGE_64;
2225
2226 if (attrs->ia_valid & ATTR_CTIME)
2227 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
2228 else
2229 args->ctime = NO_CHANGE_64;
2230
2231 args->device = 0;
6508d904 2232 open_file = find_writable_file(cifsInode, true);
3bbeeb3c 2233 if (open_file) {
4b4de76e 2234 u16 nfid = open_file->fid.netfid;
3bbeeb3c 2235 u32 npid = open_file->pid;
13cfb733 2236 pTcon = tlink_tcon(open_file->tlink);
3bbeeb3c 2237 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
6ab409b5 2238 cifsFileInfo_put(open_file);
3bbeeb3c 2239 } else {
7ffec372
JL
2240 tlink = cifs_sb_tlink(cifs_sb);
2241 if (IS_ERR(tlink)) {
2242 rc = PTR_ERR(tlink);
2243 goto out;
2244 }
2245 pTcon = tlink_tcon(tlink);
3bbeeb3c 2246 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
01ea95e3 2247 cifs_sb->local_nls,
bc8ebdc4 2248 cifs_remap(cifs_sb));
7ffec372 2249 cifs_put_tlink(tlink);
3bbeeb3c 2250 }
3fe5c1dd 2251
1025774c
CH
2252 if (rc)
2253 goto out;
ccd4bb1b 2254
1025774c 2255 if ((attrs->ia_valid & ATTR_SIZE) &&
1b947463
CH
2256 attrs->ia_size != i_size_read(inode))
2257 truncate_setsize(inode, attrs->ia_size);
1025774c
CH
2258
2259 setattr_copy(inode, attrs);
2260 mark_inode_dirty(inode);
2261
2262 /* force revalidate when any of these times are set since some
2263 of the fs types (eg ext3, fat) do not have fine enough
2264 time granularity to match protocol, and we do not have a
2265 a way (yet) to query the server fs's time granularity (and
2266 whether it rounds times down).
2267 */
2268 if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2269 cifsInode->time = 0;
3fe5c1dd
JL
2270out:
2271 kfree(args);
2272 kfree(full_path);
6d5786a3 2273 free_xid(xid);
3fe5c1dd
JL
2274 return rc;
2275}
2276
0510eeb7
JL
2277static int
2278cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
1da177e4 2279{
6d5786a3 2280 unsigned int xid;
8abf2775
EB
2281 kuid_t uid = INVALID_UID;
2282 kgid_t gid = INVALID_GID;
2b0143b5 2283 struct inode *inode = d_inode(direntry);
3fe5c1dd 2284 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3fe5c1dd 2285 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1da177e4
LT
2286 char *full_path = NULL;
2287 int rc = -EACCES;
feb3e20c 2288 __u32 dosattr = 0;
4e1e7fb9 2289 __u64 mode = NO_CHANGE_64;
3fe5c1dd 2290
6d5786a3 2291 xid = get_xid();
1da177e4 2292
35c265e0
AV
2293 cifs_dbg(FYI, "setattr on file %pd attrs->iavalid 0x%x\n",
2294 direntry, attrs->ia_valid);
6473a559 2295
db78b877
CH
2296 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2297 attrs->ia_valid |= ATTR_FORCE;
2298
31051c85 2299 rc = setattr_prepare(direntry, attrs);
db78b877 2300 if (rc < 0) {
6d5786a3 2301 free_xid(xid);
db78b877 2302 return rc;
6473a559 2303 }
50c2f753 2304
7f57356b 2305 full_path = build_path_from_dentry(direntry);
1da177e4 2306 if (full_path == NULL) {
0f3bc09e 2307 rc = -ENOMEM;
6d5786a3 2308 free_xid(xid);
0f3bc09e 2309 return rc;
1da177e4 2310 }
1da177e4 2311
0f4d634c
JL
2312 /*
2313 * Attempt to flush data before changing attributes. We need to do
2314 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2315 * ownership or mode then we may also need to do this. Here, we take
2316 * the safe way out and just do the flush on all setattr requests. If
2317 * the flush returns error, store it to report later and continue.
2318 *
2319 * BB: This should be smarter. Why bother flushing pages that
2320 * will be truncated anyway? Also, should we error out here if
2321 * the flush returns error?
2322 */
2323 rc = filemap_write_and_wait(inode->i_mapping);
eb4b756b
JL
2324 mapping_set_error(inode->i_mapping, rc);
2325 rc = 0;
cea21805 2326
50531444 2327 if (attrs->ia_valid & ATTR_SIZE) {
8efdbde6
JL
2328 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2329 if (rc != 0)
e30dcf3a 2330 goto cifs_setattr_exit;
1da177e4 2331 }
4ca691a8 2332
a5ff3769
SP
2333 if (attrs->ia_valid & ATTR_UID)
2334 uid = attrs->ia_uid;
2335
2336 if (attrs->ia_valid & ATTR_GID)
2337 gid = attrs->ia_gid;
2338
2339#ifdef CONFIG_CIFS_ACL
2340 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
8abf2775 2341 if (uid_valid(uid) || gid_valid(gid)) {
a5ff3769
SP
2342 rc = id_mode_to_cifs_acl(inode, full_path, NO_CHANGE_64,
2343 uid, gid);
2344 if (rc) {
f96637be
JP
2345 cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
2346 __func__, rc);
a5ff3769
SP
2347 goto cifs_setattr_exit;
2348 }
2349 }
2350 } else
2351#endif /* CONFIG_CIFS_ACL */
3fe5c1dd 2352 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
4ca691a8 2353 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
1da177e4 2354
d32c4f26
JL
2355 /* skip mode change if it's just for clearing setuid/setgid */
2356 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2357 attrs->ia_valid &= ~ATTR_MODE;
2358
1da177e4 2359 if (attrs->ia_valid & ATTR_MODE) {
1da177e4 2360 mode = attrs->ia_mode;
cdbce9c8 2361 rc = 0;
79df1bae 2362#ifdef CONFIG_CIFS_ACL
78415d2d 2363 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
a5ff3769 2364 rc = id_mode_to_cifs_acl(inode, full_path, mode,
8abf2775 2365 INVALID_UID, INVALID_GID);
78415d2d 2366 if (rc) {
f96637be
JP
2367 cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
2368 __func__, rc);
78415d2d
SP
2369 goto cifs_setattr_exit;
2370 }
2371 } else
79df1bae 2372#endif /* CONFIG_CIFS_ACL */
5132861a
JL
2373 if (((mode & S_IWUGO) == 0) &&
2374 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
feb3e20c
JL
2375
2376 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
2377
5132861a
JL
2378 /* fix up mode if we're not using dynperm */
2379 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
2380 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
2381 } else if ((mode & S_IWUGO) &&
2382 (cifsInode->cifsAttrs & ATTR_READONLY)) {
feb3e20c
JL
2383
2384 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
2385 /* Attributes of 0 are ignored */
2386 if (dosattr == 0)
2387 dosattr |= ATTR_NORMAL;
5132861a
JL
2388
2389 /* reset local inode permissions to normal */
2390 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2391 attrs->ia_mode &= ~(S_IALLUGO);
2392 if (S_ISDIR(inode->i_mode))
2393 attrs->ia_mode |=
2394 cifs_sb->mnt_dir_mode;
2395 else
2396 attrs->ia_mode |=
2397 cifs_sb->mnt_file_mode;
2398 }
2399 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2400 /* ignore mode change - ATTR_READONLY hasn't changed */
2401 attrs->ia_valid &= ~ATTR_MODE;
1da177e4 2402 }
1da177e4
LT
2403 }
2404
feb3e20c
JL
2405 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
2406 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
2407 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
2408 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
1da177e4 2409
e30dcf3a
SF
2410 /* Even if error on time set, no sense failing the call if
2411 the server would set the time to a reasonable value anyway,
2412 and this check ensures that we are not being called from
2413 sys_utimes in which case we ought to fail the call back to
2414 the user when the server rejects the call */
fb8c4b14 2415 if ((rc) && (attrs->ia_valid &
feb3e20c 2416 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
e30dcf3a 2417 rc = 0;
1da177e4
LT
2418 }
2419
2420 /* do not need local check to inode_check_ok since the server does
2421 that */
1025774c
CH
2422 if (rc)
2423 goto cifs_setattr_exit;
2424
2425 if ((attrs->ia_valid & ATTR_SIZE) &&
1b947463
CH
2426 attrs->ia_size != i_size_read(inode))
2427 truncate_setsize(inode, attrs->ia_size);
1025774c
CH
2428
2429 setattr_copy(inode, attrs);
2430 mark_inode_dirty(inode);
1025774c 2431
e30dcf3a 2432cifs_setattr_exit:
1da177e4 2433 kfree(full_path);
6d5786a3 2434 free_xid(xid);
1da177e4
LT
2435 return rc;
2436}
2437
0510eeb7
JL
2438int
2439cifs_setattr(struct dentry *direntry, struct iattr *attrs)
2440{
fc64005c 2441 struct cifs_sb_info *cifs_sb = CIFS_SB(direntry->d_sb);
96daf2b0 2442 struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
0510eeb7
JL
2443
2444 if (pTcon->unix_ext)
2445 return cifs_setattr_unix(direntry, attrs);
2446
2447 return cifs_setattr_nounix(direntry, attrs);
2448
2449 /* BB: add cifs_setattr_legacy for really old servers */
2450}
2451
99ee4dbd 2452#if 0
1da177e4
LT
2453void cifs_delete_inode(struct inode *inode)
2454{
f96637be 2455 cifs_dbg(FYI, "In cifs_delete_inode, inode = 0x%p\n", inode);
1da177e4
LT
2456 /* may have to add back in if and when safe distributed caching of
2457 directories added e.g. via FindNotify */
2458}
99ee4dbd 2459#endif