blob: 753e7a3486de41fa6b55f758389a9550992d5399 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/inode.c
3 *
Steve Frenchf19159d2010-04-21 04:12:10 +00004 * Copyright (C) International Business Machines Corp., 2002,2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/stat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/pagemap.h>
Jeff Layton4f73c7d2014-04-30 09:31:47 -040025#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/div64.h>
27#include "cifsfs.h"
28#include "cifspdu.h"
29#include "cifsglob.h"
30#include "cifsproto.h"
31#include "cifs_debug.h"
32#include "cifs_fs_sb.h"
Suresh Jayaraman9451a9a2010-07-05 18:12:45 +053033#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Christoph Hellwig70eff552008-02-15 20:55:05 +000035
David Howells01c64fe2011-01-14 18:45:47 +000036static void cifs_set_ops(struct inode *inode)
Christoph Hellwig70eff552008-02-15 20:55:05 +000037{
38 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
39
40 switch (inode->i_mode & S_IFMT) {
41 case S_IFREG:
42 inode->i_op = &cifs_file_inode_ops;
43 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
44 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
45 inode->i_fop = &cifs_file_direct_nobrl_ops;
46 else
47 inode->i_fop = &cifs_file_direct_ops;
Pavel Shilovsky8be7e6b2010-12-12 13:11:13 +030048 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) {
49 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
50 inode->i_fop = &cifs_file_strict_nobrl_ops;
51 else
52 inode->i_fop = &cifs_file_strict_ops;
Christoph Hellwig70eff552008-02-15 20:55:05 +000053 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
54 inode->i_fop = &cifs_file_nobrl_ops;
55 else { /* not direct, send byte range locks */
56 inode->i_fop = &cifs_file_ops;
57 }
58
Christoph Hellwig70eff552008-02-15 20:55:05 +000059 /* check if server can support readpages */
Jeff Layton0d424ad2010-09-20 16:01:35 -070060 if (cifs_sb_master_tcon(cifs_sb)->ses->server->maxBuf <
Christoph Hellwig70eff552008-02-15 20:55:05 +000061 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)
62 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
63 else
64 inode->i_data.a_ops = &cifs_addr_ops;
65 break;
66 case S_IFDIR:
Steve Frenchbc5b6e22008-03-11 21:07:48 +000067#ifdef CONFIG_CIFS_DFS_UPCALL
David Howells01c64fe2011-01-14 18:45:47 +000068 if (IS_AUTOMOUNT(inode)) {
Igor Mammedov79626702008-03-09 03:44:18 +000069 inode->i_op = &cifs_dfs_referral_inode_operations;
70 } else {
Steve Frenchbc5b6e22008-03-11 21:07:48 +000071#else /* NO DFS support, treat as a directory */
72 {
73#endif
Igor Mammedov79626702008-03-09 03:44:18 +000074 inode->i_op = &cifs_dir_inode_ops;
75 inode->i_fop = &cifs_dir_ops;
76 }
Christoph Hellwig70eff552008-02-15 20:55:05 +000077 break;
78 case S_IFLNK:
79 inode->i_op = &cifs_symlink_inode_ops;
80 break;
81 default:
82 init_special_inode(inode, inode->i_mode, inode->i_rdev);
83 break;
84 }
85}
86
Jeff Laytondf2cf172010-02-12 07:44:16 -050087/* check inode attributes against fattr. If they don't match, tag the
88 * inode for cache invalidation
89 */
90static void
91cifs_revalidate_cache(struct inode *inode, struct cifs_fattr *fattr)
92{
93 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
94
Joe Perchesf96637b2013-05-04 22:12:25 -050095 cifs_dbg(FYI, "%s: revalidating inode %llu\n",
96 __func__, cifs_i->uniqueid);
Jeff Laytondf2cf172010-02-12 07:44:16 -050097
98 if (inode->i_state & I_NEW) {
Joe Perchesf96637b2013-05-04 22:12:25 -050099 cifs_dbg(FYI, "%s: inode %llu is new\n",
100 __func__, cifs_i->uniqueid);
Jeff Laytondf2cf172010-02-12 07:44:16 -0500101 return;
102 }
103
104 /* don't bother with revalidation if we have an oplock */
Pavel Shilovsky18cceb62013-09-05 13:01:06 +0400105 if (CIFS_CACHE_READ(cifs_i)) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500106 cifs_dbg(FYI, "%s: inode %llu is oplocked\n",
107 __func__, cifs_i->uniqueid);
Jeff Laytondf2cf172010-02-12 07:44:16 -0500108 return;
109 }
110
111 /* revalidate if mtime or size have changed */
112 if (timespec_equal(&inode->i_mtime, &fattr->cf_mtime) &&
113 cifs_i->server_eof == fattr->cf_eof) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500114 cifs_dbg(FYI, "%s: inode %llu is unchanged\n",
115 __func__, cifs_i->uniqueid);
Jeff Laytondf2cf172010-02-12 07:44:16 -0500116 return;
117 }
118
Joe Perchesf96637b2013-05-04 22:12:25 -0500119 cifs_dbg(FYI, "%s: invalidating inode %llu mapping\n",
120 __func__, cifs_i->uniqueid);
Jeff Laytonaff8d5c2014-04-30 09:31:45 -0400121 set_bit(CIFS_INO_INVALID_MAPPING, &cifs_i->flags);
Jeff Laytondf2cf172010-02-12 07:44:16 -0500122}
123
Jim McDonough74d290d2013-09-21 10:36:10 -0500124/*
125 * copy nlink to the inode, unless it wasn't provided. Provide
126 * sane values if we don't have an existing one and none was provided
127 */
128static void
129cifs_nlink_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
130{
131 /*
132 * if we're in a situation where we can't trust what we
133 * got from the server (readdir, some non-unix cases)
134 * fake reasonable values
135 */
136 if (fattr->cf_flags & CIFS_FATTR_UNKNOWN_NLINK) {
137 /* only provide fake values on a new inode */
138 if (inode->i_state & I_NEW) {
139 if (fattr->cf_cifsattrs & ATTR_DIRECTORY)
140 set_nlink(inode, 2);
141 else
142 set_nlink(inode, 1);
143 }
144 return;
145 }
146
147 /* we trust the server, so update it */
148 set_nlink(inode, fattr->cf_nlink);
149}
150
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400151/* populate an inode with info from a cifs_fattr struct */
152void
153cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
Christoph Hellwig75f12982008-02-25 20:25:21 +0000154{
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400155 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400156 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Christoph Hellwig75f12982008-02-25 20:25:21 +0000157
Jeff Laytondf2cf172010-02-12 07:44:16 -0500158 cifs_revalidate_cache(inode, fattr);
159
Steve Frenchb7ca6922012-08-03 08:43:01 -0500160 spin_lock(&inode->i_lock);
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400161 inode->i_atime = fattr->cf_atime;
162 inode->i_mtime = fattr->cf_mtime;
163 inode->i_ctime = fattr->cf_ctime;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400164 inode->i_rdev = fattr->cf_rdev;
Jim McDonough74d290d2013-09-21 10:36:10 -0500165 cifs_nlink_fattr_to_inode(inode, fattr);
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400166 inode->i_uid = fattr->cf_uid;
167 inode->i_gid = fattr->cf_gid;
168
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400169 /* if dynperm is set, don't clobber existing mode */
170 if (inode->i_state & I_NEW ||
171 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
172 inode->i_mode = fattr->cf_mode;
173
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400174 cifs_i->cifsAttrs = fattr->cf_cifsattrs;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400175
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400176 if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
177 cifs_i->time = 0;
178 else
179 cifs_i->time = jiffies;
180
Jeff Laytonaff8d5c2014-04-30 09:31:45 -0400181 if (fattr->cf_flags & CIFS_FATTR_DELETE_PENDING)
182 set_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
183 else
184 clear_bit(CIFS_INO_DELETE_PENDING, &cifs_i->flags);
Christoph Hellwig75f12982008-02-25 20:25:21 +0000185
Jeff Layton835a36c2010-02-10 16:21:33 -0500186 cifs_i->server_eof = fattr->cf_eof;
Christoph Hellwig75f12982008-02-25 20:25:21 +0000187 /*
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400188 * Can't safely change the file size here if the client is writing to
189 * it due to potential races.
Christoph Hellwig75f12982008-02-25 20:25:21 +0000190 */
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400191 if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
192 i_size_write(inode, fattr->cf_eof);
Christoph Hellwig75f12982008-02-25 20:25:21 +0000193
194 /*
195 * i_blocks is not related to (i_size / i_blksize),
196 * but instead 512 byte (2**9) size is required for
197 * calculating num blocks.
198 */
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400199 inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
Christoph Hellwig75f12982008-02-25 20:25:21 +0000200 }
201 spin_unlock(&inode->i_lock);
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400202
David Howells01c64fe2011-01-14 18:45:47 +0000203 if (fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL)
204 inode->i_flags |= S_AUTOMOUNT;
Jeff Laytonc2b93e02013-05-07 11:28:31 -0400205 if (inode->i_state & I_NEW)
206 cifs_set_ops(inode);
Christoph Hellwig75f12982008-02-25 20:25:21 +0000207}
208
Jeff Layton4065c802010-05-17 07:18:58 -0400209void
210cifs_fill_uniqueid(struct super_block *sb, struct cifs_fattr *fattr)
211{
212 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
213
214 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM)
215 return;
216
217 fattr->cf_uniqueid = iunique(sb, ROOT_I);
218}
219
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400220/* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
221void
222cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
223 struct cifs_sb_info *cifs_sb)
224{
225 memset(fattr, 0, sizeof(*fattr));
226 fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
227 fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
228 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
229
230 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
231 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
232 fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
233 fattr->cf_mode = le64_to_cpu(info->Permissions);
234
235 /*
236 * Since we set the inode type below we need to mask off
237 * to avoid strange results if bits set above.
238 */
239 fattr->cf_mode &= ~S_IFMT;
240 switch (le32_to_cpu(info->Type)) {
241 case UNIX_FILE:
242 fattr->cf_mode |= S_IFREG;
243 fattr->cf_dtype = DT_REG;
244 break;
245 case UNIX_SYMLINK:
246 fattr->cf_mode |= S_IFLNK;
247 fattr->cf_dtype = DT_LNK;
248 break;
249 case UNIX_DIR:
250 fattr->cf_mode |= S_IFDIR;
251 fattr->cf_dtype = DT_DIR;
252 break;
253 case UNIX_CHARDEV:
254 fattr->cf_mode |= S_IFCHR;
255 fattr->cf_dtype = DT_CHR;
256 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
257 le64_to_cpu(info->DevMinor) & MINORMASK);
258 break;
259 case UNIX_BLOCKDEV:
260 fattr->cf_mode |= S_IFBLK;
261 fattr->cf_dtype = DT_BLK;
262 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
263 le64_to_cpu(info->DevMinor) & MINORMASK);
264 break;
265 case UNIX_FIFO:
266 fattr->cf_mode |= S_IFIFO;
267 fattr->cf_dtype = DT_FIFO;
268 break;
269 case UNIX_SOCKET:
270 fattr->cf_mode |= S_IFSOCK;
271 fattr->cf_dtype = DT_SOCK;
272 break;
273 default:
274 /* safest to call it a file if we do not know */
275 fattr->cf_mode |= S_IFREG;
276 fattr->cf_dtype = DT_REG;
Joe Perchesf96637b2013-05-04 22:12:25 -0500277 cifs_dbg(FYI, "unknown type %d\n", le32_to_cpu(info->Type));
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400278 break;
279 }
280
Eric W. Biederman46bbc252013-02-05 23:55:44 -0800281 fattr->cf_uid = cifs_sb->mnt_uid;
282 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)) {
283 u64 id = le64_to_cpu(info->Uid);
Eric W. Biederman4a2c8cf2013-02-06 01:53:25 -0800284 if (id < ((uid_t)-1)) {
285 kuid_t uid = make_kuid(&init_user_ns, id);
286 if (uid_valid(uid))
287 fattr->cf_uid = uid;
288 }
Eric W. Biederman46bbc252013-02-05 23:55:44 -0800289 }
290
291 fattr->cf_gid = cifs_sb->mnt_gid;
292 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)) {
293 u64 id = le64_to_cpu(info->Gid);
Eric W. Biederman4a2c8cf2013-02-06 01:53:25 -0800294 if (id < ((gid_t)-1)) {
295 kgid_t gid = make_kgid(&init_user_ns, id);
296 if (gid_valid(gid))
297 fattr->cf_gid = gid;
298 }
Eric W. Biederman46bbc252013-02-05 23:55:44 -0800299 }
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400300
301 fattr->cf_nlink = le64_to_cpu(info->Nlinks);
302}
Steve Frenchb9a32602008-05-20 21:52:32 +0000303
304/*
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400305 * Fill a cifs_fattr struct with fake inode info.
306 *
307 * Needed to setup cifs_fattr data for the directory which is the
308 * junction to the new submount (ie to setup the fake directory
309 * which represents a DFS referral).
Steve Frenchb9a32602008-05-20 21:52:32 +0000310 */
Steve Frenchf1230c92009-07-22 23:13:01 +0000311static void
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400312cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb)
Steve French0e4bbde2008-05-20 19:50:46 +0000313{
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400314 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
Steve French0e4bbde2008-05-20 19:50:46 +0000315
Joe Perchesf96637b2013-05-04 22:12:25 -0500316 cifs_dbg(FYI, "creating fake fattr for DFS referral\n");
Steve French0e4bbde2008-05-20 19:50:46 +0000317
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400318 memset(fattr, 0, sizeof(*fattr));
319 fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
320 fattr->cf_uid = cifs_sb->mnt_uid;
321 fattr->cf_gid = cifs_sb->mnt_gid;
322 fattr->cf_atime = CURRENT_TIME;
323 fattr->cf_ctime = CURRENT_TIME;
324 fattr->cf_mtime = CURRENT_TIME;
325 fattr->cf_nlink = 2;
326 fattr->cf_flags |= CIFS_FATTR_DFS_REFERRAL;
Steve French0e4bbde2008-05-20 19:50:46 +0000327}
328
Pavel Shilovsky4ad65042012-09-18 16:20:26 -0700329static int
330cifs_get_file_info_unix(struct file *filp)
Jeff Laytonabab0952010-02-12 07:44:18 -0500331{
332 int rc;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400333 unsigned int xid;
Jeff Laytonabab0952010-02-12 07:44:18 -0500334 FILE_UNIX_BASIC_INFO find_data;
335 struct cifs_fattr fattr;
Al Viro496ad9a2013-01-23 17:07:38 -0500336 struct inode *inode = file_inode(filp);
Jeff Laytonabab0952010-02-12 07:44:18 -0500337 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Joe Perchesc21dfb62010-07-12 13:50:14 -0700338 struct cifsFileInfo *cfile = filp->private_data;
Steve French96daf2b2011-05-27 04:34:02 +0000339 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
Jeff Laytonabab0952010-02-12 07:44:18 -0500340
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400341 xid = get_xid();
Pavel Shilovsky4b4de762012-09-18 16:20:26 -0700342 rc = CIFSSMBUnixQFileInfo(xid, tcon, cfile->fid.netfid, &find_data);
Jeff Laytonabab0952010-02-12 07:44:18 -0500343 if (!rc) {
344 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
345 } else if (rc == -EREMOTE) {
346 cifs_create_dfs_fattr(&fattr, inode->i_sb);
347 rc = 0;
348 }
349
350 cifs_fattr_to_inode(inode, &fattr);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400351 free_xid(xid);
Jeff Laytonabab0952010-02-12 07:44:18 -0500352 return rc;
353}
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355int cifs_get_inode_info_unix(struct inode **pinode,
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400356 const unsigned char *full_path,
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400357 struct super_block *sb, unsigned int xid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400359 int rc;
Steve French0e4bbde2008-05-20 19:50:46 +0000360 FILE_UNIX_BASIC_INFO find_data;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400361 struct cifs_fattr fattr;
Steve French96daf2b2011-05-27 04:34:02 +0000362 struct cifs_tcon *tcon;
Jeff Layton7ffec372010-09-29 19:51:11 -0400363 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Joe Perchesf96637b2013-05-04 22:12:25 -0500366 cifs_dbg(FYI, "Getting info on %s\n", full_path);
Igor Mammedov79626702008-03-09 03:44:18 +0000367
Jeff Layton7ffec372010-09-29 19:51:11 -0400368 tlink = cifs_sb_tlink(cifs_sb);
369 if (IS_ERR(tlink))
370 return PTR_ERR(tlink);
371 tcon = tlink_tcon(tlink);
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /* could have done a find first instead but this returns more info */
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400374 rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
Steve French737b7582005-04-28 22:41:06 -0700375 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
376 CIFS_MOUNT_MAP_SPECIAL_CHR);
Jeff Layton7ffec372010-09-29 19:51:11 -0400377 cifs_put_tlink(tlink);
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400378
379 if (!rc) {
380 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
381 } else if (rc == -EREMOTE) {
382 cifs_create_dfs_fattr(&fattr, sb);
Jeff Laytone911d0c2008-07-12 13:47:59 -0700383 rc = 0;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400384 } else {
385 return rc;
Steve French0e4bbde2008-05-20 19:50:46 +0000386 }
387
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200388 /* check for Minshall+French symlinks */
389 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
Sachin Prabhucb084b12013-11-25 17:09:50 +0000390 int tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
391 full_path);
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200392 if (tmprc)
Sachin Prabhucb084b12013-11-25 17:09:50 +0000393 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200394 }
395
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400396 if (*pinode == NULL) {
397 /* get new inode */
Jeff Layton4065c802010-05-17 07:18:58 -0400398 cifs_fill_uniqueid(sb, &fattr);
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400399 *pinode = cifs_iget(sb, &fattr);
400 if (!*pinode)
401 rc = -ENOMEM;
402 } else {
403 /* we already have inode, update it */
404 cifs_fattr_to_inode(*pinode, &fattr);
405 }
Steve French0e4bbde2008-05-20 19:50:46 +0000406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return rc;
408}
409
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400410static int
Pavel Shilovsky0360d602014-01-16 15:53:35 +0400411cifs_sfu_type(struct cifs_fattr *fattr, const char *path,
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400412 struct cifs_sb_info *cifs_sb, unsigned int xid)
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800413{
414 int rc;
Steve French4b18f2a2008-04-29 00:06:05 +0000415 int oplock = 0;
Jeff Layton7ffec372010-09-29 19:51:11 -0400416 struct tcon_link *tlink;
Steve French96daf2b2011-05-27 04:34:02 +0000417 struct cifs_tcon *tcon;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400418 struct cifs_fid fid;
419 struct cifs_open_parms oparms;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +0000420 struct cifs_io_parms io_parms;
Steve French86c96b42005-11-18 20:25:31 -0800421 char buf[24];
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800422 unsigned int bytes_read;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000423 char *pbuf;
Pavel Shilovsky0360d602014-01-16 15:53:35 +0400424 int buf_type = CIFS_NO_BUFFER;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800425
426 pbuf = buf;
427
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400428 fattr->cf_mode &= ~S_IFMT;
429
430 if (fattr->cf_eof == 0) {
431 fattr->cf_mode |= S_IFIFO;
432 fattr->cf_dtype = DT_FIFO;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800433 return 0;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400434 } else if (fattr->cf_eof < 8) {
435 fattr->cf_mode |= S_IFREG;
436 fattr->cf_dtype = DT_REG;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800437 return -EINVAL; /* EOPNOTSUPP? */
438 }
Steve French50c2f752007-07-13 00:33:32 +0000439
Jeff Layton7ffec372010-09-29 19:51:11 -0400440 tlink = cifs_sb_tlink(cifs_sb);
441 if (IS_ERR(tlink))
442 return PTR_ERR(tlink);
443 tcon = tlink_tcon(tlink);
444
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400445 oparms.tcon = tcon;
446 oparms.cifs_sb = cifs_sb;
447 oparms.desired_access = GENERIC_READ;
448 oparms.create_options = CREATE_NOT_DIR;
449 oparms.disposition = FILE_OPEN;
450 oparms.path = path;
451 oparms.fid = &fid;
452 oparms.reconnect = false;
453
454 rc = CIFS_open(xid, &oparms, &oplock, NULL);
Pavel Shilovsky0360d602014-01-16 15:53:35 +0400455 if (rc) {
456 cifs_put_tlink(tlink);
457 return rc;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800458 }
Pavel Shilovsky0360d602014-01-16 15:53:35 +0400459
460 /* Read header */
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400461 io_parms.netfid = fid.netfid;
Pavel Shilovsky0360d602014-01-16 15:53:35 +0400462 io_parms.pid = current->tgid;
463 io_parms.tcon = tcon;
464 io_parms.offset = 0;
465 io_parms.length = 24;
466
467 rc = CIFSSMBRead(xid, &io_parms, &bytes_read, &pbuf, &buf_type);
468 if ((rc == 0) && (bytes_read >= 8)) {
469 if (memcmp("IntxBLK", pbuf, 8) == 0) {
470 cifs_dbg(FYI, "Block device\n");
471 fattr->cf_mode |= S_IFBLK;
472 fattr->cf_dtype = DT_BLK;
473 if (bytes_read == 24) {
474 /* we have enough to decode dev num */
475 __u64 mjr; /* major */
476 __u64 mnr; /* minor */
477 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
478 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
479 fattr->cf_rdev = MKDEV(mjr, mnr);
480 }
481 } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
482 cifs_dbg(FYI, "Char device\n");
483 fattr->cf_mode |= S_IFCHR;
484 fattr->cf_dtype = DT_CHR;
485 if (bytes_read == 24) {
486 /* we have enough to decode dev num */
487 __u64 mjr; /* major */
488 __u64 mnr; /* minor */
489 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
490 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
491 fattr->cf_rdev = MKDEV(mjr, mnr);
492 }
493 } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
494 cifs_dbg(FYI, "Symlink\n");
495 fattr->cf_mode |= S_IFLNK;
496 fattr->cf_dtype = DT_LNK;
497 } else {
498 fattr->cf_mode |= S_IFREG; /* file? */
499 fattr->cf_dtype = DT_REG;
500 rc = -EOPNOTSUPP;
501 }
502 } else {
503 fattr->cf_mode |= S_IFREG; /* then it is a file */
504 fattr->cf_dtype = DT_REG;
505 rc = -EOPNOTSUPP; /* or some unknown SFU type */
506 }
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +0400507 CIFSSMBClose(xid, tcon, fid.netfid);
Jeff Layton7ffec372010-09-29 19:51:11 -0400508 cifs_put_tlink(tlink);
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800509 return rc;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800510}
511
Steve French9e294f12005-11-17 16:59:21 -0800512#define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
513
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400514/*
515 * Fetch mode bits as provided by SFU.
516 *
517 * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
518 */
519static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400520 struct cifs_sb_info *cifs_sb, unsigned int xid)
Steve French9e294f12005-11-17 16:59:21 -0800521{
Steve French3020a1f2005-11-18 11:31:10 -0800522#ifdef CONFIG_CIFS_XATTR
Steve French9e294f12005-11-17 16:59:21 -0800523 ssize_t rc;
524 char ea_value[4];
525 __u32 mode;
Jeff Layton7ffec372010-09-29 19:51:11 -0400526 struct tcon_link *tlink;
Steve French96daf2b2011-05-27 04:34:02 +0000527 struct cifs_tcon *tcon;
Steve French9e294f12005-11-17 16:59:21 -0800528
Jeff Layton7ffec372010-09-29 19:51:11 -0400529 tlink = cifs_sb_tlink(cifs_sb);
530 if (IS_ERR(tlink))
531 return PTR_ERR(tlink);
532 tcon = tlink_tcon(tlink);
533
Steve Frenchd979f3b2014-02-01 23:27:18 -0600534 if (tcon->ses->server->ops->query_all_EAs == NULL) {
535 cifs_put_tlink(tlink);
536 return -EOPNOTSUPP;
537 }
538
539 rc = tcon->ses->server->ops->query_all_EAs(xid, tcon, path,
540 "SETFILEBITS", ea_value, 4 /* size of buf */,
541 cifs_sb->local_nls,
542 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Jeff Layton7ffec372010-09-29 19:51:11 -0400543 cifs_put_tlink(tlink);
Steve French4523cc32007-04-30 20:13:06 +0000544 if (rc < 0)
Steve French9e294f12005-11-17 16:59:21 -0800545 return (int)rc;
546 else if (rc > 3) {
547 mode = le32_to_cpu(*((__le32 *)ea_value));
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400548 fattr->cf_mode &= ~SFBITS_MASK;
Joe Perchesf96637b2013-05-04 22:12:25 -0500549 cifs_dbg(FYI, "special bits 0%o org mode 0%o\n",
550 mode, fattr->cf_mode);
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400551 fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
Joe Perchesf96637b2013-05-04 22:12:25 -0500552 cifs_dbg(FYI, "special mode bits 0%o\n", mode);
Steve French9e294f12005-11-17 16:59:21 -0800553 }
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400554
555 return 0;
Steve French3020a1f2005-11-18 11:31:10 -0800556#else
557 return -EOPNOTSUPP;
558#endif
Steve French9e294f12005-11-17 16:59:21 -0800559}
560
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400561/* Fill a cifs_fattr struct with info from FILE_ALL_INFO */
Steve Frenchf1230c92009-07-22 23:13:01 +0000562static void
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400563cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info,
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400564 struct cifs_sb_info *cifs_sb, bool adjust_tz,
565 bool symlink)
Steve Frenchb9a32602008-05-20 21:52:32 +0000566{
Steve French96daf2b2011-05-27 04:34:02 +0000567 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
Jeff Layton0d424ad2010-09-20 16:01:35 -0700568
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400569 memset(fattr, 0, sizeof(*fattr));
570 fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
571 if (info->DeletePending)
572 fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
Steve Frenchb9a32602008-05-20 21:52:32 +0000573
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400574 if (info->LastAccessTime)
575 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
576 else
577 fattr->cf_atime = CURRENT_TIME;
578
579 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
580 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
581
582 if (adjust_tz) {
Jeff Layton0d424ad2010-09-20 16:01:35 -0700583 fattr->cf_ctime.tv_sec += tcon->ses->server->timeAdj;
584 fattr->cf_mtime.tv_sec += tcon->ses->server->timeAdj;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400585 }
586
587 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
588 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
Jeff Layton20054bd2011-01-07 11:30:27 -0500589 fattr->cf_createtime = le64_to_cpu(info->CreationTime);
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400590
Jim McDonough74d290d2013-09-21 10:36:10 -0500591 fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400592
593 if (symlink) {
594 fattr->cf_mode = S_IFLNK;
595 fattr->cf_dtype = DT_LNK;
596 } else if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400597 fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
598 fattr->cf_dtype = DT_DIR;
Pavel Shilovsky6de2ce42012-02-17 16:13:30 +0300599 /*
600 * Server can return wrong NumberOfLinks value for directories
601 * when Unix extensions are disabled - fake it.
602 */
Jim McDonough74d290d2013-09-21 10:36:10 -0500603 if (!tcon->unix_ext)
604 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400605 } else {
606 fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
607 fattr->cf_dtype = DT_REG;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400608
Jeff Laytond0c280d2009-07-09 01:46:44 -0400609 /* clear write bits if ATTR_READONLY is set */
610 if (fattr->cf_cifsattrs & ATTR_READONLY)
611 fattr->cf_mode &= ~(S_IWUGO);
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400612
Jim McDonough74d290d2013-09-21 10:36:10 -0500613 /*
614 * Don't accept zero nlink from non-unix servers unless
615 * delete is pending. Instead mark it as unknown.
616 */
617 if ((fattr->cf_nlink < 1) && !tcon->unix_ext &&
618 !info->DeletePending) {
619 cifs_dbg(1, "bogus file nlink value %u\n",
Steve French6658b9f2013-07-04 14:38:48 -0500620 fattr->cf_nlink);
Jim McDonough74d290d2013-09-21 10:36:10 -0500621 fattr->cf_flags |= CIFS_FATTR_UNKNOWN_NLINK;
Steve French6658b9f2013-07-04 14:38:48 -0500622 }
Pavel Shilovsky6de2ce42012-02-17 16:13:30 +0300623 }
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400624
625 fattr->cf_uid = cifs_sb->mnt_uid;
626 fattr->cf_gid = cifs_sb->mnt_gid;
Steve Frenchb9a32602008-05-20 21:52:32 +0000627}
628
Pavel Shilovsky4ad65042012-09-18 16:20:26 -0700629static int
630cifs_get_file_info(struct file *filp)
Jeff Laytonabab0952010-02-12 07:44:18 -0500631{
632 int rc;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400633 unsigned int xid;
Jeff Laytonabab0952010-02-12 07:44:18 -0500634 FILE_ALL_INFO find_data;
635 struct cifs_fattr fattr;
Al Viro496ad9a2013-01-23 17:07:38 -0500636 struct inode *inode = file_inode(filp);
Jeff Laytonabab0952010-02-12 07:44:18 -0500637 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Joe Perchesc21dfb62010-07-12 13:50:14 -0700638 struct cifsFileInfo *cfile = filp->private_data;
Steve French96daf2b2011-05-27 04:34:02 +0000639 struct cifs_tcon *tcon = tlink_tcon(cfile->tlink);
Pavel Shilovsky4ad65042012-09-18 16:20:26 -0700640 struct TCP_Server_Info *server = tcon->ses->server;
641
642 if (!server->ops->query_file_info)
643 return -ENOSYS;
Jeff Laytonabab0952010-02-12 07:44:18 -0500644
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400645 xid = get_xid();
Pavel Shilovsky4ad65042012-09-18 16:20:26 -0700646 rc = server->ops->query_file_info(xid, tcon, &cfile->fid, &find_data);
Pavel Shilovsky42274bb2011-10-22 14:37:50 +0400647 switch (rc) {
648 case 0:
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400649 cifs_all_info_to_fattr(&fattr, &find_data, cifs_sb, false,
650 false);
Pavel Shilovsky42274bb2011-10-22 14:37:50 +0400651 break;
652 case -EREMOTE:
653 cifs_create_dfs_fattr(&fattr, inode->i_sb);
654 rc = 0;
655 break;
656 case -EOPNOTSUPP:
657 case -EINVAL:
Jeff Laytonabab0952010-02-12 07:44:18 -0500658 /*
659 * FIXME: legacy server -- fall back to path-based call?
Steve Frenchff2157132010-03-09 20:30:42 +0000660 * for now, just skip revalidating and mark inode for
661 * immediate reval.
662 */
Jeff Laytonabab0952010-02-12 07:44:18 -0500663 rc = 0;
664 CIFS_I(inode)->time = 0;
Pavel Shilovsky42274bb2011-10-22 14:37:50 +0400665 default:
Jeff Laytonabab0952010-02-12 07:44:18 -0500666 goto cgfi_exit;
Pavel Shilovsky42274bb2011-10-22 14:37:50 +0400667 }
Jeff Laytonabab0952010-02-12 07:44:18 -0500668
669 /*
670 * don't bother with SFU junk here -- just mark inode as needing
671 * revalidation.
672 */
Jeff Laytonabab0952010-02-12 07:44:18 -0500673 fattr.cf_uniqueid = CIFS_I(inode)->uniqueid;
674 fattr.cf_flags |= CIFS_FATTR_NEED_REVAL;
675 cifs_fattr_to_inode(inode, &fattr);
676cgfi_exit:
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400677 free_xid(xid);
Jeff Laytonabab0952010-02-12 07:44:18 -0500678 return rc;
679}
680
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400681int
682cifs_get_inode_info(struct inode **inode, const char *full_path,
683 FILE_ALL_INFO *data, struct super_block *sb, int xid,
Steve French42eacf92014-02-10 14:08:16 -0600684 const struct cifs_fid *fid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -0500686 bool validinum = false;
687 __u16 srchflgs;
688 int rc = 0, tmprc = ENOSYS;
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400689 struct cifs_tcon *tcon;
690 struct TCP_Server_Info *server;
Jeff Layton7ffec372010-09-29 19:51:11 -0400691 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 char *buf = NULL;
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400694 bool adjust_tz = false;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400695 struct cifs_fattr fattr;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -0500696 struct cifs_search_info *srchinf = NULL;
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400697 bool symlink = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Jeff Layton7ffec372010-09-29 19:51:11 -0400699 tlink = cifs_sb_tlink(cifs_sb);
700 if (IS_ERR(tlink))
701 return PTR_ERR(tlink);
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400702 tcon = tlink_tcon(tlink);
703 server = tcon->ses->server;
Jeff Layton7ffec372010-09-29 19:51:11 -0400704
Joe Perchesf96637b2013-05-04 22:12:25 -0500705 cifs_dbg(FYI, "Getting info on %s\n", full_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400707 if ((data == NULL) && (*inode != NULL)) {
Pavel Shilovsky18cceb62013-09-05 13:01:06 +0400708 if (CIFS_CACHE_READ(CIFS_I(*inode))) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500709 cifs_dbg(FYI, "No need to revalidate cached inode sizes\n");
Jeff Layton7ffec372010-09-29 19:51:11 -0400710 goto cgii_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 }
712 }
713
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400714 /* if inode info is not passed, get it from server */
715 if (data == NULL) {
716 if (!server->ops->query_path_info) {
717 rc = -ENOSYS;
718 goto cgii_exit;
719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
Jeff Layton7ffec372010-09-29 19:51:11 -0400721 if (buf == NULL) {
722 rc = -ENOMEM;
723 goto cgii_exit;
724 }
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400725 data = (FILE_ALL_INFO *)buf;
726 rc = server->ops->query_path_info(xid, tcon, cifs_sb, full_path,
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400727 data, &adjust_tz, &symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400729
730 if (!rc) {
Pavel Shilovskyeb85d94b2013-10-23 17:49:47 +0400731 cifs_all_info_to_fattr(&fattr, data, cifs_sb, adjust_tz,
732 symlink);
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400733 } else if (rc == -EREMOTE) {
734 cifs_create_dfs_fattr(&fattr, sb);
Steve Frenchb9a32602008-05-20 21:52:32 +0000735 rc = 0;
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -0500736 } else if (rc == -EACCES && backup_cred(cifs_sb)) {
737 srchinf = kzalloc(sizeof(struct cifs_search_info),
738 GFP_KERNEL);
739 if (srchinf == NULL) {
740 rc = -ENOMEM;
741 goto cgii_exit;
742 }
743
744 srchinf->endOfSearch = false;
745 srchinf->info_level = SMB_FIND_FILE_ID_FULL_DIR_INFO;
746
747 srchflgs = CIFS_SEARCH_CLOSE_ALWAYS |
748 CIFS_SEARCH_CLOSE_AT_END |
749 CIFS_SEARCH_BACKUP_SEARCH;
750
751 rc = CIFSFindFirst(xid, tcon, full_path,
752 cifs_sb, NULL, srchflgs, srchinf, false);
753 if (!rc) {
754 data =
755 (FILE_ALL_INFO *)srchinf->srch_entries_start;
756
757 cifs_dir_info_to_fattr(&fattr,
758 (FILE_DIRECTORY_INFO *)data, cifs_sb);
759 fattr.cf_uniqueid = le64_to_cpu(
760 ((SEARCH_ID_FULL_DIR_INFO *)data)->UniqueId);
761 validinum = true;
762
763 cifs_buf_release(srchinf->ntwrk_buf_start);
764 }
765 kfree(srchinf);
766 } else
Igor Mammedov79626702008-03-09 03:44:18 +0000767 goto cgii_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400769 /*
770 * If an inode wasn't passed in, then get the inode number
771 *
772 * Is an i_ino of zero legal? Can we use that to check if the server
773 * supports returning inode numbers? Are there other sanity checks we
774 * can use to ensure that the server is really filling in that field?
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400775 */
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400776 if (*inode == NULL) {
Steve Frenchb9a32602008-05-20 21:52:32 +0000777 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -0500778 if (validinum == false) {
779 if (server->ops->get_srv_inum)
780 tmprc = server->ops->get_srv_inum(xid,
781 tcon, cifs_sb, full_path,
782 &fattr.cf_uniqueid, data);
783 if (tmprc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500784 cifs_dbg(FYI, "GetSrvInodeNum rc %d\n",
785 tmprc);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -0500786 fattr.cf_uniqueid = iunique(sb, ROOT_I);
787 cifs_autodisable_serverino(cifs_sb);
788 }
Jeff Layton132ac7b2009-02-10 07:33:57 -0500789 }
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -0500790 } else
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400791 fattr.cf_uniqueid = iunique(sb, ROOT_I);
Shirish Pargaonkarc052e2b2012-09-28 12:21:14 -0500792 } else
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400793 fattr.cf_uniqueid = CIFS_I(*inode)->uniqueid;
Steve Frenchb9a32602008-05-20 21:52:32 +0000794
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400795 /* query for SFU type info if supported and needed */
796 if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
797 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
798 tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
799 if (tmprc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500800 cifs_dbg(FYI, "cifs_sfu_type failed: %d\n", tmprc);
Steve Frenchb9a32602008-05-20 21:52:32 +0000801 }
Steve Frenchb9a32602008-05-20 21:52:32 +0000802
Jeff Layton79df1ba2010-12-06 12:52:08 -0500803#ifdef CONFIG_CIFS_ACL
Steve Frenchb9a32602008-05-20 21:52:32 +0000804 /* fill in 0777 bits from ACL */
805 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400806 rc = cifs_acl_to_fattr(cifs_sb, &fattr, *inode, full_path, fid);
Shirish Pargaonkar78415d22010-11-27 11:37:26 -0600807 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500808 cifs_dbg(FYI, "%s: Getting ACL failed with error: %d\n",
809 __func__, rc);
Shirish Pargaonkar78415d22010-11-27 11:37:26 -0600810 goto cgii_exit;
811 }
Steve Frenchb9a32602008-05-20 21:52:32 +0000812 }
Jeff Layton79df1ba2010-12-06 12:52:08 -0500813#endif /* CONFIG_CIFS_ACL */
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400814
815 /* fill in remaining high mode bits e.g. SUID, VTX */
816 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
817 cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
818
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200819 /* check for Minshall+French symlinks */
820 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MF_SYMLINKS) {
Sachin Prabhucb084b12013-11-25 17:09:50 +0000821 tmprc = check_mf_symlink(xid, tcon, cifs_sb, &fattr,
822 full_path);
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200823 if (tmprc)
Sachin Prabhucb084b12013-11-25 17:09:50 +0000824 cifs_dbg(FYI, "check_mf_symlink: %d\n", tmprc);
Stefan Metzmacher1b12b9c2010-08-05 21:19:56 +0200825 }
826
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400827 if (!*inode) {
828 *inode = cifs_iget(sb, &fattr);
829 if (!*inode)
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400830 rc = -ENOMEM;
831 } else {
Pavel Shilovsky1208ef12012-05-27 17:34:43 +0400832 cifs_fattr_to_inode(*inode, &fattr);
Steve Frenchb9a32602008-05-20 21:52:32 +0000833 }
834
Igor Mammedov79626702008-03-09 03:44:18 +0000835cgii_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 kfree(buf);
Jeff Layton7ffec372010-09-29 19:51:11 -0400837 cifs_put_tlink(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return rc;
839}
840
Steve French7f8ed422007-09-28 22:28:55 +0000841static const struct inode_operations cifs_ipc_inode_ops = {
842 .lookup = cifs_lookup,
843};
844
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400845static int
846cifs_find_inode(struct inode *inode, void *opaque)
847{
848 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
849
Jeff Laytonf30b9c12010-07-19 18:00:17 -0400850 /* don't match inode with different uniqueid */
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400851 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
852 return 0;
853
Jeff Layton20054bd2011-01-07 11:30:27 -0500854 /* use createtime like an i_generation field */
855 if (CIFS_I(inode)->createtime != fattr->cf_createtime)
856 return 0;
857
Jeff Laytonf30b9c12010-07-19 18:00:17 -0400858 /* don't match inode of different type */
859 if ((inode->i_mode & S_IFMT) != (fattr->cf_mode & S_IFMT))
860 return 0;
861
Jeff Layton5acfec22010-08-02 17:43:54 -0400862 /* if it's not a directory or has no dentries, then flag it */
Al Virob3d9b7a2012-06-09 13:51:19 -0400863 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry))
Jeff Layton3d694382010-05-11 14:59:55 -0400864 fattr->cf_flags |= CIFS_FATTR_INO_COLLISION;
Jeff Layton3d694382010-05-11 14:59:55 -0400865
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400866 return 1;
867}
868
869static int
870cifs_init_inode(struct inode *inode, void *opaque)
871{
872 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
873
874 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
Jeff Layton20054bd2011-01-07 11:30:27 -0500875 CIFS_I(inode)->createtime = fattr->cf_createtime;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400876 return 0;
877}
878
Jeff Layton5acfec22010-08-02 17:43:54 -0400879/*
880 * walk dentry list for an inode and report whether it has aliases that
881 * are hashed. We use this to determine if a directory inode can actually
882 * be used.
883 */
884static bool
885inode_has_hashed_dentries(struct inode *inode)
886{
887 struct dentry *dentry;
888
Nick Piggin873feea2011-01-07 17:50:06 +1100889 spin_lock(&inode->i_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800890 hlist_for_each_entry(dentry, &inode->i_dentry, d_alias) {
Jeff Layton5acfec22010-08-02 17:43:54 -0400891 if (!d_unhashed(dentry) || IS_ROOT(dentry)) {
Nick Piggin873feea2011-01-07 17:50:06 +1100892 spin_unlock(&inode->i_lock);
Jeff Layton5acfec22010-08-02 17:43:54 -0400893 return true;
894 }
895 }
Nick Piggin873feea2011-01-07 17:50:06 +1100896 spin_unlock(&inode->i_lock);
Jeff Layton5acfec22010-08-02 17:43:54 -0400897 return false;
898}
899
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400900/* Given fattrs, get a corresponding inode */
901struct inode *
902cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
903{
904 unsigned long hash;
905 struct inode *inode;
906
Jeff Layton3d694382010-05-11 14:59:55 -0400907retry_iget5_locked:
Joe Perchesf96637b2013-05-04 22:12:25 -0500908 cifs_dbg(FYI, "looking for uniqueid=%llu\n", fattr->cf_uniqueid);
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400909
910 /* hash down to 32-bits on 32-bit arch */
911 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
912
913 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400914 if (inode) {
Jeff Layton5acfec22010-08-02 17:43:54 -0400915 /* was there a potentially problematic inode collision? */
Jeff Layton3d694382010-05-11 14:59:55 -0400916 if (fattr->cf_flags & CIFS_FATTR_INO_COLLISION) {
Jeff Layton3d694382010-05-11 14:59:55 -0400917 fattr->cf_flags &= ~CIFS_FATTR_INO_COLLISION;
Jeff Layton5acfec22010-08-02 17:43:54 -0400918
919 if (inode_has_hashed_dentries(inode)) {
920 cifs_autodisable_serverino(CIFS_SB(sb));
921 iput(inode);
922 fattr->cf_uniqueid = iunique(sb, ROOT_I);
923 goto retry_iget5_locked;
924 }
Jeff Layton3d694382010-05-11 14:59:55 -0400925 }
926
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400927 cifs_fattr_to_inode(inode, fattr);
928 if (sb->s_flags & MS_NOATIME)
929 inode->i_flags |= S_NOATIME | S_NOCMTIME;
930 if (inode->i_state & I_NEW) {
931 inode->i_ino = hash;
Jeff Layton522440e2010-09-29 09:49:54 -0400932 if (S_ISREG(inode->i_mode))
933 inode->i_data.backing_dev_info = sb->s_bdi;
Steve French0ccd4802010-07-16 04:31:02 +0000934#ifdef CONFIG_CIFS_FSCACHE
Suresh Jayaraman9451a9a2010-07-05 18:12:45 +0530935 /* initialize per-inode cache cookie pointer */
936 CIFS_I(inode)->fscache = NULL;
Steve French0ccd4802010-07-16 04:31:02 +0000937#endif
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400938 unlock_new_inode(inode);
939 }
940 }
941
942 return inode;
943}
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945/* gets root inode */
Shirish Pargaonkar9b6763e2011-02-21 23:56:59 -0600946struct inode *cifs_root_iget(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400948 unsigned int xid;
Jeff Layton0d424ad2010-09-20 16:01:35 -0700949 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400950 struct inode *inode = NULL;
David Howellsce634ab2008-02-07 00:15:33 -0800951 long rc;
Steve French96daf2b2011-05-27 04:34:02 +0000952 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
David Howellsce634ab2008-02-07 00:15:33 -0800953
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400954 xid = get_xid();
Jeff Layton0d424ad2010-09-20 16:01:35 -0700955 if (tcon->unix_ext)
Steve Frenchf87d39d2011-05-27 03:50:55 +0000956 rc = cifs_get_inode_info_unix(&inode, "", sb, xid);
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400957 else
Steve Frenchf87d39d2011-05-27 03:50:55 +0000958 rc = cifs_get_inode_info(&inode, "", NULL, sb, xid, NULL);
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400959
Oskar Schirmera7851ce2010-11-10 21:06:13 +0000960 if (!inode) {
961 inode = ERR_PTR(rc);
962 goto out;
963 }
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400964
Steve French0ccd4802010-07-16 04:31:02 +0000965#ifdef CONFIG_CIFS_FSCACHE
Suresh Jayaramand03382c2010-07-05 18:12:27 +0530966 /* populate tcon->resource_id */
Jeff Layton0d424ad2010-09-20 16:01:35 -0700967 tcon->resource_id = CIFS_I(inode)->uniqueid;
Steve French0ccd4802010-07-16 04:31:02 +0000968#endif
Suresh Jayaramand03382c2010-07-05 18:12:27 +0530969
Jeff Layton0d424ad2010-09-20 16:01:35 -0700970 if (rc && tcon->ipc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500971 cifs_dbg(FYI, "ipc connection - fake read inode\n");
Steve Frenchb7ca6922012-08-03 08:43:01 -0500972 spin_lock(&inode->i_lock);
Steve French7f8ed422007-09-28 22:28:55 +0000973 inode->i_mode |= S_IFDIR;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200974 set_nlink(inode, 2);
Steve French7f8ed422007-09-28 22:28:55 +0000975 inode->i_op = &cifs_ipc_inode_ops;
976 inode->i_fop = &simple_dir_operations;
977 inode->i_uid = cifs_sb->mnt_uid;
978 inode->i_gid = cifs_sb->mnt_gid;
Steve Frenchb7ca6922012-08-03 08:43:01 -0500979 spin_unlock(&inode->i_lock);
Steve Frenchad661332008-08-12 14:14:40 +0000980 } else if (rc) {
David Howellsce634ab2008-02-07 00:15:33 -0800981 iget_failed(inode);
Oskar Schirmera7851ce2010-11-10 21:06:13 +0000982 inode = ERR_PTR(rc);
Steve French7f8ed422007-09-28 22:28:55 +0000983 }
984
Oskar Schirmera7851ce2010-11-10 21:06:13 +0000985out:
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400986 /* can not call macro free_xid here since in a void func
David Howellsce634ab2008-02-07 00:15:33 -0800987 * TODO: This is no longer true
988 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400989 _free_xid(xid);
David Howellsce634ab2008-02-07 00:15:33 -0800990 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700993int
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +0400994cifs_set_file_info(struct inode *inode, struct iattr *attrs, unsigned int xid,
Pavel Shilovskyed6875e2012-09-18 16:20:25 -0700995 char *full_path, __u32 dosattr)
Steve French388e57b2008-09-16 23:50:58 +0000996{
Steve French388e57b2008-09-16 23:50:58 +0000997 bool set_time = false;
Steve French388e57b2008-09-16 23:50:58 +0000998 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Pavel Shilovsky6bdf6db2012-09-18 16:20:32 -0700999 struct TCP_Server_Info *server;
Steve French388e57b2008-09-16 23:50:58 +00001000 FILE_BASIC_INFO info_buf;
1001
Steve French1adcb712009-02-25 14:19:56 +00001002 if (attrs == NULL)
1003 return -EINVAL;
1004
Pavel Shilovsky6bdf6db2012-09-18 16:20:32 -07001005 server = cifs_sb_master_tcon(cifs_sb)->ses->server;
1006 if (!server->ops->set_file_info)
1007 return -ENOSYS;
1008
Steve French388e57b2008-09-16 23:50:58 +00001009 if (attrs->ia_valid & ATTR_ATIME) {
1010 set_time = true;
1011 info_buf.LastAccessTime =
1012 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1013 } else
1014 info_buf.LastAccessTime = 0;
1015
1016 if (attrs->ia_valid & ATTR_MTIME) {
1017 set_time = true;
1018 info_buf.LastWriteTime =
1019 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1020 } else
1021 info_buf.LastWriteTime = 0;
1022
1023 /*
1024 * Samba throws this field away, but windows may actually use it.
1025 * Do not set ctime unless other time stamps are changed explicitly
1026 * (i.e. by utimes()) since we would then have a mix of client and
1027 * server times.
1028 */
1029 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001030 cifs_dbg(FYI, "CIFS - CTIME changed\n");
Steve French388e57b2008-09-16 23:50:58 +00001031 info_buf.ChangeTime =
1032 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1033 } else
1034 info_buf.ChangeTime = 0;
1035
1036 info_buf.CreationTime = 0; /* don't change */
1037 info_buf.Attributes = cpu_to_le32(dosattr);
1038
Pavel Shilovsky6bdf6db2012-09-18 16:20:32 -07001039 return server->ops->set_file_info(inode, full_path, &info_buf, xid);
Steve French388e57b2008-09-16 23:50:58 +00001040}
1041
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001042/*
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001043 * Open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001044 * and rename it to a random name that hopefully won't conflict with
1045 * anything else.
1046 */
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001047int
1048cifs_rename_pending_delete(const char *full_path, struct dentry *dentry,
1049 const unsigned int xid)
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001050{
1051 int oplock = 0;
1052 int rc;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001053 struct cifs_fid fid;
1054 struct cifs_open_parms oparms;
Steve French32709582008-10-20 00:44:19 +00001055 struct inode *inode = dentry->d_inode;
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001056 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1057 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Jeff Layton7ffec372010-09-29 19:51:11 -04001058 struct tcon_link *tlink;
Steve French96daf2b2011-05-27 04:34:02 +00001059 struct cifs_tcon *tcon;
Steve French32709582008-10-20 00:44:19 +00001060 __u32 dosattr, origattr;
1061 FILE_BASIC_INFO *info_buf = NULL;
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001062
Jeff Layton7ffec372010-09-29 19:51:11 -04001063 tlink = cifs_sb_tlink(cifs_sb);
1064 if (IS_ERR(tlink))
1065 return PTR_ERR(tlink);
1066 tcon = tlink_tcon(tlink);
1067
Sachin Prabhuc483a982013-03-05 19:25:56 +00001068 /*
1069 * We cannot rename the file if the server doesn't support
1070 * CAP_INFOLEVEL_PASSTHRU
1071 */
1072 if (!(tcon->ses->capabilities & CAP_INFOLEVEL_PASSTHRU)) {
1073 rc = -EBUSY;
1074 goto out;
1075 }
1076
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001077 oparms.tcon = tcon;
1078 oparms.cifs_sb = cifs_sb;
1079 oparms.desired_access = DELETE | FILE_WRITE_ATTRIBUTES;
1080 oparms.create_options = CREATE_NOT_DIR;
1081 oparms.disposition = FILE_OPEN;
1082 oparms.path = full_path;
1083 oparms.fid = &fid;
1084 oparms.reconnect = false;
1085
1086 rc = CIFS_open(xid, &oparms, &oplock, NULL);
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001087 if (rc != 0)
1088 goto out;
1089
Steve French32709582008-10-20 00:44:19 +00001090 origattr = cifsInode->cifsAttrs;
1091 if (origattr == 0)
1092 origattr |= ATTR_NORMAL;
1093
1094 dosattr = origattr & ~ATTR_READONLY;
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001095 if (dosattr == 0)
1096 dosattr |= ATTR_NORMAL;
1097 dosattr |= ATTR_HIDDEN;
1098
Steve French32709582008-10-20 00:44:19 +00001099 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
1100 if (dosattr != origattr) {
1101 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
1102 if (info_buf == NULL) {
1103 rc = -ENOMEM;
1104 goto out_close;
1105 }
1106 info_buf->Attributes = cpu_to_le32(dosattr);
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001107 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
Steve French32709582008-10-20 00:44:19 +00001108 current->tgid);
1109 /* although we would like to mark the file hidden
1110 if that fails we will still try to rename it */
Sachin Prabhu72d282d2013-03-05 19:25:55 +00001111 if (!rc)
Steve French32709582008-10-20 00:44:19 +00001112 cifsInode->cifsAttrs = dosattr;
1113 else
1114 dosattr = origattr; /* since not able to change them */
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001115 }
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001116
Jeff Laytondd1db2d2008-10-16 19:27:12 -04001117 /* rename the file */
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001118 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, NULL,
1119 cifs_sb->local_nls,
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001120 cifs_sb->mnt_cifs_flags &
1121 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve French32709582008-10-20 00:44:19 +00001122 if (rc != 0) {
Sachin Prabhu47c78f42013-03-11 13:08:49 +00001123 rc = -EBUSY;
Steve French32709582008-10-20 00:44:19 +00001124 goto undo_setattr;
1125 }
Jeff Layton6d22f092008-09-23 11:48:35 -04001126
Steve French32709582008-10-20 00:44:19 +00001127 /* try to set DELETE_ON_CLOSE */
Jeff Laytonaff8d5c2014-04-30 09:31:45 -04001128 if (!test_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags)) {
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001129 rc = CIFSSMBSetFileDisposition(xid, tcon, true, fid.netfid,
Steve French32709582008-10-20 00:44:19 +00001130 current->tgid);
1131 /*
1132 * some samba versions return -ENOENT when we try to set the
1133 * file disposition here. Likely a samba bug, but work around
1134 * it for now. This means that some cifsXXX files may hang
1135 * around after they shouldn't.
1136 *
1137 * BB: remove this hack after more servers have the fix
1138 */
1139 if (rc == -ENOENT)
1140 rc = 0;
1141 else if (rc != 0) {
Sachin Prabhu47c78f42013-03-11 13:08:49 +00001142 rc = -EBUSY;
Steve French32709582008-10-20 00:44:19 +00001143 goto undo_rename;
1144 }
Jeff Laytonaff8d5c2014-04-30 09:31:45 -04001145 set_bit(CIFS_INO_DELETE_PENDING, &cifsInode->flags);
Steve French32709582008-10-20 00:44:19 +00001146 }
Jeff Layton7ce86d52008-09-24 11:32:59 -04001147
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001148out_close:
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001149 CIFSSMBClose(xid, tcon, fid.netfid);
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001150out:
Steve French32709582008-10-20 00:44:19 +00001151 kfree(info_buf);
Jeff Layton7ffec372010-09-29 19:51:11 -04001152 cifs_put_tlink(tlink);
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001153 return rc;
Steve French32709582008-10-20 00:44:19 +00001154
1155 /*
1156 * reset everything back to the original state. Don't bother
1157 * dealing with errors here since we can't do anything about
1158 * them anyway.
1159 */
1160undo_rename:
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001161 CIFSSMBRenameOpenFile(xid, tcon, fid.netfid, dentry->d_name.name,
Steve French32709582008-10-20 00:44:19 +00001162 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
1163 CIFS_MOUNT_MAP_SPECIAL_CHR);
1164undo_setattr:
1165 if (dosattr != origattr) {
1166 info_buf->Attributes = cpu_to_le32(origattr);
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001167 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, fid.netfid,
Steve French32709582008-10-20 00:44:19 +00001168 current->tgid))
1169 cifsInode->cifsAttrs = origattr;
1170 }
1171
1172 goto out_close;
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001173}
1174
Steve Frenchb7ca6922012-08-03 08:43:01 -05001175/* copied from fs/nfs/dir.c with small changes */
1176static void
1177cifs_drop_nlink(struct inode *inode)
1178{
1179 spin_lock(&inode->i_lock);
1180 if (inode->i_nlink > 0)
1181 drop_nlink(inode);
1182 spin_unlock(&inode->i_lock);
1183}
Steve Frenchff694522009-04-20 19:45:13 +00001184
1185/*
1186 * If dentry->d_inode is null (usually meaning the cached dentry
1187 * is a negative dentry) then we would attempt a standard SMB delete, but
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02001188 * if that fails we can not attempt the fall back mechanisms on EACCESS
1189 * but will return the EACCESS to the caller. Note that the VFS does not call
Steve Frenchff694522009-04-20 19:45:13 +00001190 * unlink on negative dentries currently.
1191 */
Jeff Layton5f0319a2008-09-16 14:05:16 -04001192int cifs_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
1194 int rc = 0;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001195 unsigned int xid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 char *full_path = NULL;
Jeff Layton5f0319a2008-09-16 14:05:16 -04001197 struct inode *inode = dentry->d_inode;
Steve Frenchff694522009-04-20 19:45:13 +00001198 struct cifsInodeInfo *cifs_inode;
Jeff Layton5f0319a2008-09-16 14:05:16 -04001199 struct super_block *sb = dir->i_sb;
1200 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
Jeff Layton7ffec372010-09-29 19:51:11 -04001201 struct tcon_link *tlink;
Steve French96daf2b2011-05-27 04:34:02 +00001202 struct cifs_tcon *tcon;
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001203 struct TCP_Server_Info *server;
Steve French60502472008-10-07 18:42:52 +00001204 struct iattr *attrs = NULL;
1205 __u32 dosattr = 0, origattr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Joe Perchesf96637b2013-05-04 22:12:25 -05001207 cifs_dbg(FYI, "cifs_unlink, dir=0x%p, dentry=0x%p\n", dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Jeff Layton7ffec372010-09-29 19:51:11 -04001209 tlink = cifs_sb_tlink(cifs_sb);
1210 if (IS_ERR(tlink))
1211 return PTR_ERR(tlink);
1212 tcon = tlink_tcon(tlink);
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001213 server = tcon->ses->server;
Jeff Layton7ffec372010-09-29 19:51:11 -04001214
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001215 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Jeff Layton5f0319a2008-09-16 14:05:16 -04001217 /* Unlink can be called from rename so we can not take the
1218 * sb->s_vfs_rename_mutex here */
1219 full_path = build_path_from_dentry(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (full_path == NULL) {
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05301221 rc = -ENOMEM;
Jeff Layton7ffec372010-09-29 19:51:11 -04001222 goto unlink_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
Steve French2d785a52007-07-15 01:48:57 +00001224
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04001225 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1226 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
Jeff Layton5f0319a2008-09-16 14:05:16 -04001227 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
Steve French2d785a52007-07-15 01:48:57 +00001228 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
1229 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Joe Perchesf96637b2013-05-04 22:12:25 -05001230 cifs_dbg(FYI, "posix del rc %d\n", rc);
Steve French2d785a52007-07-15 01:48:57 +00001231 if ((rc == 0) || (rc == -ENOENT))
1232 goto psx_del_no_retry;
1233 }
1234
Steve French60502472008-10-07 18:42:52 +00001235retry_std_delete:
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001236 if (!server->ops->unlink) {
1237 rc = -ENOSYS;
1238 goto psx_del_no_retry;
1239 }
1240
1241 rc = server->ops->unlink(xid, tcon, full_path, cifs_sb);
Steve French60502472008-10-07 18:42:52 +00001242
Steve French2d785a52007-07-15 01:48:57 +00001243psx_del_no_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (!rc) {
Jeff Layton5f0319a2008-09-16 14:05:16 -04001245 if (inode)
Steve Frenchb7ca6922012-08-03 08:43:01 -05001246 cifs_drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 } else if (rc == -ENOENT) {
Jeff Layton5f0319a2008-09-16 14:05:16 -04001248 d_drop(dentry);
Sachin Prabhu47c78f42013-03-11 13:08:49 +00001249 } else if (rc == -EBUSY) {
Pavel Shilovskyed6875e2012-09-18 16:20:25 -07001250 if (server->ops->rename_pending_delete) {
1251 rc = server->ops->rename_pending_delete(full_path,
1252 dentry, xid);
1253 if (rc == 0)
1254 cifs_drop_nlink(inode);
1255 }
Steve Frenchff694522009-04-20 19:45:13 +00001256 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
Steve French388e57b2008-09-16 23:50:58 +00001257 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1258 if (attrs == NULL) {
1259 rc = -ENOMEM;
1260 goto out_reval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 }
Steve French388e57b2008-09-16 23:50:58 +00001262
1263 /* try to reset dos attributes */
Steve Frenchff694522009-04-20 19:45:13 +00001264 cifs_inode = CIFS_I(inode);
1265 origattr = cifs_inode->cifsAttrs;
Steve French60502472008-10-07 18:42:52 +00001266 if (origattr == 0)
1267 origattr |= ATTR_NORMAL;
1268 dosattr = origattr & ~ATTR_READONLY;
Steve French388e57b2008-09-16 23:50:58 +00001269 if (dosattr == 0)
1270 dosattr |= ATTR_NORMAL;
1271 dosattr |= ATTR_HIDDEN;
1272
1273 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
Steve French388e57b2008-09-16 23:50:58 +00001274 if (rc != 0)
1275 goto out_reval;
Steve French60502472008-10-07 18:42:52 +00001276
1277 goto retry_std_delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
Steve French60502472008-10-07 18:42:52 +00001279
1280 /* undo the setattr if we errored out and it's needed */
1281 if (rc != 0 && dosattr != 0)
1282 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1283
Steve French388e57b2008-09-16 23:50:58 +00001284out_reval:
Jeff Layton5f0319a2008-09-16 14:05:16 -04001285 if (inode) {
Steve Frenchff694522009-04-20 19:45:13 +00001286 cifs_inode = CIFS_I(inode);
1287 cifs_inode->time = 0; /* will force revalidate to get info
Steve Frenchb2aeb9d2005-05-17 13:16:18 -05001288 when needed */
Jeff Layton5f0319a2008-09-16 14:05:16 -04001289 inode->i_ctime = current_fs_time(sb);
Steve Frenchb2aeb9d2005-05-17 13:16:18 -05001290 }
Jeff Layton5f0319a2008-09-16 14:05:16 -04001291 dir->i_ctime = dir->i_mtime = current_fs_time(sb);
Steve Frenchff694522009-04-20 19:45:13 +00001292 cifs_inode = CIFS_I(dir);
Steve French60502472008-10-07 18:42:52 +00001293 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
Jeff Layton7ffec372010-09-29 19:51:11 -04001294unlink_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 kfree(full_path);
Steve French60502472008-10-07 18:42:52 +00001296 kfree(attrs);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001297 free_xid(xid);
Jeff Layton7ffec372010-09-29 19:51:11 -04001298 cifs_put_tlink(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return rc;
1300}
1301
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001302static int
Jeff Layton101b92d2012-09-19 06:22:45 -07001303cifs_mkdir_qinfo(struct inode *parent, struct dentry *dentry, umode_t mode,
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001304 const char *full_path, struct cifs_sb_info *cifs_sb,
1305 struct cifs_tcon *tcon, const unsigned int xid)
1306{
1307 int rc = 0;
Jeff Layton101b92d2012-09-19 06:22:45 -07001308 struct inode *inode = NULL;
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001309
1310 if (tcon->unix_ext)
Jeff Layton101b92d2012-09-19 06:22:45 -07001311 rc = cifs_get_inode_info_unix(&inode, full_path, parent->i_sb,
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001312 xid);
1313 else
Jeff Layton101b92d2012-09-19 06:22:45 -07001314 rc = cifs_get_inode_info(&inode, full_path, NULL, parent->i_sb,
1315 xid, NULL);
1316
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001317 if (rc)
1318 return rc;
1319
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001320 /*
1321 * setting nlink not necessary except in cases where we failed to get it
Jeff Layton101b92d2012-09-19 06:22:45 -07001322 * from the server or was set bogus. Also, since this is a brand new
1323 * inode, no need to grab the i_lock before setting the i_nlink.
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001324 */
Jeff Layton101b92d2012-09-19 06:22:45 -07001325 if (inode->i_nlink < 2)
1326 set_nlink(inode, 2);
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001327 mode &= ~current_umask();
1328 /* must turn on setgid bit if parent dir has it */
Jeff Layton101b92d2012-09-19 06:22:45 -07001329 if (parent->i_mode & S_ISGID)
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001330 mode |= S_ISGID;
1331
1332 if (tcon->unix_ext) {
1333 struct cifs_unix_set_info_args args = {
1334 .mode = mode,
1335 .ctime = NO_CHANGE_64,
1336 .atime = NO_CHANGE_64,
1337 .mtime = NO_CHANGE_64,
1338 .device = 0,
1339 };
1340 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
Eric W. Biederman49418b22013-02-06 00:57:56 -08001341 args.uid = current_fsuid();
Jeff Layton101b92d2012-09-19 06:22:45 -07001342 if (parent->i_mode & S_ISGID)
Eric W. Biederman49418b22013-02-06 00:57:56 -08001343 args.gid = parent->i_gid;
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001344 else
Eric W. Biederman49418b22013-02-06 00:57:56 -08001345 args.gid = current_fsgid();
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001346 } else {
Eric W. Biederman49418b22013-02-06 00:57:56 -08001347 args.uid = INVALID_UID; /* no change */
1348 args.gid = INVALID_GID; /* no change */
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001349 }
1350 CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args,
1351 cifs_sb->local_nls,
1352 cifs_sb->mnt_cifs_flags &
1353 CIFS_MOUNT_MAP_SPECIAL_CHR);
1354 } else {
Pavel Shilovskyf4367202012-03-17 11:41:12 +03001355 struct TCP_Server_Info *server = tcon->ses->server;
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001356 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
Pavel Shilovskyf4367202012-03-17 11:41:12 +03001357 (mode & S_IWUGO) == 0 && server->ops->mkdir_setinfo)
Jeff Layton101b92d2012-09-19 06:22:45 -07001358 server->ops->mkdir_setinfo(inode, full_path, cifs_sb,
Pavel Shilovskyf4367202012-03-17 11:41:12 +03001359 tcon, xid);
Jeff Layton101b92d2012-09-19 06:22:45 -07001360 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)
1361 inode->i_mode = (mode | S_IFDIR);
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001362
Jeff Layton101b92d2012-09-19 06:22:45 -07001363 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
1364 inode->i_uid = current_fsuid();
1365 if (inode->i_mode & S_ISGID)
1366 inode->i_gid = parent->i_gid;
1367 else
1368 inode->i_gid = current_fsgid();
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001369 }
1370 }
Jeff Layton101b92d2012-09-19 06:22:45 -07001371 d_instantiate(dentry, inode);
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001372 return rc;
1373}
1374
1375static int
1376cifs_posix_mkdir(struct inode *inode, struct dentry *dentry, umode_t mode,
1377 const char *full_path, struct cifs_sb_info *cifs_sb,
1378 struct cifs_tcon *tcon, const unsigned int xid)
1379{
1380 int rc = 0;
1381 u32 oplock = 0;
1382 FILE_UNIX_BASIC_INFO *info = NULL;
1383 struct inode *newinode = NULL;
1384 struct cifs_fattr fattr;
1385
1386 info = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1387 if (info == NULL) {
1388 rc = -ENOMEM;
1389 goto posix_mkdir_out;
1390 }
1391
1392 mode &= ~current_umask();
1393 rc = CIFSPOSIXCreate(xid, tcon, SMB_O_DIRECTORY | SMB_O_CREAT, mode,
1394 NULL /* netfid */, info, &oplock, full_path,
1395 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
1396 CIFS_MOUNT_MAP_SPECIAL_CHR);
1397 if (rc == -EOPNOTSUPP)
1398 goto posix_mkdir_out;
1399 else if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001400 cifs_dbg(FYI, "posix mkdir returned 0x%x\n", rc);
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001401 d_drop(dentry);
1402 goto posix_mkdir_out;
1403 }
1404
1405 if (info->Type == cpu_to_le32(-1))
1406 /* no return info, go query for it */
1407 goto posix_mkdir_get_info;
1408 /*
1409 * BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if
1410 * need to set uid/gid.
1411 */
1412
1413 cifs_unix_basic_to_fattr(&fattr, info, cifs_sb);
1414 cifs_fill_uniqueid(inode->i_sb, &fattr);
1415 newinode = cifs_iget(inode->i_sb, &fattr);
1416 if (!newinode)
1417 goto posix_mkdir_get_info;
1418
1419 d_instantiate(dentry, newinode);
1420
1421#ifdef CONFIG_CIFS_DEBUG2
Joe Perchesf96637b2013-05-04 22:12:25 -05001422 cifs_dbg(FYI, "instantiated dentry %p %s to inode %p\n",
1423 dentry, dentry->d_name.name, newinode);
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001424
1425 if (newinode->i_nlink != 2)
Joe Perchesf96637b2013-05-04 22:12:25 -05001426 cifs_dbg(FYI, "unexpected number of links %d\n",
1427 newinode->i_nlink);
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001428#endif
1429
1430posix_mkdir_out:
1431 kfree(info);
1432 return rc;
1433posix_mkdir_get_info:
1434 rc = cifs_mkdir_qinfo(inode, dentry, mode, full_path, cifs_sb, tcon,
1435 xid);
1436 goto posix_mkdir_out;
1437}
1438
Al Viro18bb1db2011-07-26 01:41:39 -04001439int cifs_mkdir(struct inode *inode, struct dentry *direntry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001441 int rc = 0;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001442 unsigned int xid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 struct cifs_sb_info *cifs_sb;
Jeff Layton7ffec372010-09-29 19:51:11 -04001444 struct tcon_link *tlink;
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04001445 struct cifs_tcon *tcon;
Pavel Shilovskyf4367202012-03-17 11:41:12 +03001446 struct TCP_Server_Info *server;
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001447 char *full_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Joe Perchesf96637b2013-05-04 22:12:25 -05001449 cifs_dbg(FYI, "In cifs_mkdir, mode = 0x%hx inode = 0x%p\n",
1450 mode, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 cifs_sb = CIFS_SB(inode->i_sb);
Jeff Layton7ffec372010-09-29 19:51:11 -04001453 tlink = cifs_sb_tlink(cifs_sb);
1454 if (IS_ERR(tlink))
1455 return PTR_ERR(tlink);
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04001456 tcon = tlink_tcon(tlink);
Jeff Layton7ffec372010-09-29 19:51:11 -04001457
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001458 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Steve French7f573562005-08-30 11:32:14 -07001460 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (full_path == NULL) {
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05301462 rc = -ENOMEM;
Jeff Layton7ffec372010-09-29 19:51:11 -04001463 goto mkdir_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
Steve French50c2f752007-07-13 00:33:32 +00001465
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04001466 if (cap_unix(tcon->ses) && (CIFS_UNIX_POSIX_PATH_OPS_CAP &
1467 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001468 rc = cifs_posix_mkdir(inode, direntry, mode, full_path, cifs_sb,
1469 tcon, xid);
1470 if (rc != -EOPNOTSUPP)
Steve French2dd29d32007-04-23 22:07:35 +00001471 goto mkdir_out;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001472 }
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001473
Pavel Shilovskyf4367202012-03-17 11:41:12 +03001474 server = tcon->ses->server;
1475
1476 if (!server->ops->mkdir) {
1477 rc = -ENOSYS;
1478 goto mkdir_out;
1479 }
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 /* BB add setting the equivalent of mode via CreateX w/ACLs */
Pavel Shilovskyf4367202012-03-17 11:41:12 +03001482 rc = server->ops->mkdir(xid, tcon, full_path, cifs_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001484 cifs_dbg(FYI, "cifs_mkdir returned 0x%x\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 d_drop(direntry);
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001486 goto mkdir_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
Pavel Shilovskyff691e92012-07-13 14:04:46 +04001488
1489 rc = cifs_mkdir_qinfo(inode, direntry, mode, full_path, cifs_sb, tcon,
1490 xid);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001491mkdir_out:
Pavel Shilovsky6de2ce42012-02-17 16:13:30 +03001492 /*
1493 * Force revalidate to get parent dir info when needed since cached
1494 * attributes are invalid now.
1495 */
1496 CIFS_I(inode)->time = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 kfree(full_path);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001498 free_xid(xid);
Jeff Layton7ffec372010-09-29 19:51:11 -04001499 cifs_put_tlink(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return rc;
1501}
1502
1503int cifs_rmdir(struct inode *inode, struct dentry *direntry)
1504{
1505 int rc = 0;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001506 unsigned int xid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 struct cifs_sb_info *cifs_sb;
Jeff Layton7ffec372010-09-29 19:51:11 -04001508 struct tcon_link *tlink;
Pavel Shilovskyf958ca52012-07-10 16:14:18 +04001509 struct cifs_tcon *tcon;
1510 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 char *full_path = NULL;
1512 struct cifsInodeInfo *cifsInode;
1513
Joe Perchesf96637b2013-05-04 22:12:25 -05001514 cifs_dbg(FYI, "cifs_rmdir, inode = 0x%p\n", inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001516 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Steve French7f573562005-08-30 11:32:14 -07001518 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (full_path == NULL) {
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05301520 rc = -ENOMEM;
Jeff Layton7ffec372010-09-29 19:51:11 -04001521 goto rmdir_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
1523
Jeff Layton7ffec372010-09-29 19:51:11 -04001524 cifs_sb = CIFS_SB(inode->i_sb);
1525 tlink = cifs_sb_tlink(cifs_sb);
1526 if (IS_ERR(tlink)) {
1527 rc = PTR_ERR(tlink);
1528 goto rmdir_exit;
1529 }
Pavel Shilovskyf958ca52012-07-10 16:14:18 +04001530 tcon = tlink_tcon(tlink);
1531 server = tcon->ses->server;
Jeff Layton7ffec372010-09-29 19:51:11 -04001532
Pavel Shilovskyf958ca52012-07-10 16:14:18 +04001533 if (!server->ops->rmdir) {
1534 rc = -ENOSYS;
1535 cifs_put_tlink(tlink);
1536 goto rmdir_exit;
1537 }
1538
1539 rc = server->ops->rmdir(xid, tcon, full_path, cifs_sb);
Jeff Layton7ffec372010-09-29 19:51:11 -04001540 cifs_put_tlink(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 if (!rc) {
Steve French3677db12007-02-26 16:46:11 +00001543 spin_lock(&direntry->d_inode->i_lock);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001544 i_size_write(direntry->d_inode, 0);
Dave Hansence71ec32006-09-30 23:29:06 -07001545 clear_nlink(direntry->d_inode);
Steve French3677db12007-02-26 16:46:11 +00001546 spin_unlock(&direntry->d_inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
1548
1549 cifsInode = CIFS_I(direntry->d_inode);
Pavel Shilovsky6de2ce42012-02-17 16:13:30 +03001550 /* force revalidate to go get info when needed */
1551 cifsInode->time = 0;
Steve French42c24542009-01-13 22:03:55 +00001552
1553 cifsInode = CIFS_I(inode);
Pavel Shilovsky6de2ce42012-02-17 16:13:30 +03001554 /*
1555 * Force revalidate to get parent dir info when needed since cached
1556 * attributes are invalid now.
1557 */
1558 cifsInode->time = 0;
Steve French42c24542009-01-13 22:03:55 +00001559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
1561 current_fs_time(inode->i_sb);
1562
Jeff Layton7ffec372010-09-29 19:51:11 -04001563rmdir_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 kfree(full_path);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001565 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 return rc;
1567}
1568
Steve Frenchee2fd962008-09-23 18:23:33 +00001569static int
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001570cifs_do_rename(const unsigned int xid, struct dentry *from_dentry,
1571 const char *from_path, struct dentry *to_dentry,
1572 const char *to_path)
Steve Frenchee2fd962008-09-23 18:23:33 +00001573{
1574 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
Jeff Layton7ffec372010-09-29 19:51:11 -04001575 struct tcon_link *tlink;
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001576 struct cifs_tcon *tcon;
1577 struct TCP_Server_Info *server;
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001578 struct cifs_fid fid;
1579 struct cifs_open_parms oparms;
Steve Frenchee2fd962008-09-23 18:23:33 +00001580 int oplock, rc;
1581
Jeff Layton7ffec372010-09-29 19:51:11 -04001582 tlink = cifs_sb_tlink(cifs_sb);
1583 if (IS_ERR(tlink))
1584 return PTR_ERR(tlink);
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001585 tcon = tlink_tcon(tlink);
1586 server = tcon->ses->server;
1587
1588 if (!server->ops->rename)
1589 return -ENOSYS;
Jeff Layton7ffec372010-09-29 19:51:11 -04001590
Steve Frenchee2fd962008-09-23 18:23:33 +00001591 /* try path-based rename first */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001592 rc = server->ops->rename(xid, tcon, from_path, to_path, cifs_sb);
Steve Frenchee2fd962008-09-23 18:23:33 +00001593
1594 /*
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001595 * Don't bother with rename by filehandle unless file is busy and
1596 * source. Note that cross directory moves do not work with
Steve Frenchee2fd962008-09-23 18:23:33 +00001597 * rename by filehandle to various Windows servers.
1598 */
Sachin Prabhu47c78f42013-03-11 13:08:49 +00001599 if (rc == 0 || rc != -EBUSY)
Jeff Layton7ffec372010-09-29 19:51:11 -04001600 goto do_rename_exit;
Steve Frenchee2fd962008-09-23 18:23:33 +00001601
Jeff Laytoned0e3ac2010-06-01 16:21:01 -04001602 /* open-file renames don't work across directories */
1603 if (to_dentry->d_parent != from_dentry->d_parent)
Jeff Layton7ffec372010-09-29 19:51:11 -04001604 goto do_rename_exit;
Jeff Laytoned0e3ac2010-06-01 16:21:01 -04001605
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001606 oparms.tcon = tcon;
1607 oparms.cifs_sb = cifs_sb;
Steve Frenchee2fd962008-09-23 18:23:33 +00001608 /* open the file to be renamed -- we need DELETE perms */
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001609 oparms.desired_access = DELETE;
1610 oparms.create_options = CREATE_NOT_DIR;
1611 oparms.disposition = FILE_OPEN;
1612 oparms.path = from_path;
1613 oparms.fid = &fid;
1614 oparms.reconnect = false;
1615
1616 rc = CIFS_open(xid, &oparms, &oplock, NULL);
Steve Frenchee2fd962008-09-23 18:23:33 +00001617 if (rc == 0) {
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001618 rc = CIFSSMBRenameOpenFile(xid, tcon, fid.netfid,
Steve Frenchee2fd962008-09-23 18:23:33 +00001619 (const char *) to_dentry->d_name.name,
1620 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
1621 CIFS_MOUNT_MAP_SPECIAL_CHR);
Pavel Shilovskyd81b8a42014-01-16 15:53:36 +04001622 CIFSSMBClose(xid, tcon, fid.netfid);
Steve Frenchee2fd962008-09-23 18:23:33 +00001623 }
Jeff Layton7ffec372010-09-29 19:51:11 -04001624do_rename_exit:
1625 cifs_put_tlink(tlink);
Steve Frenchee2fd962008-09-23 18:23:33 +00001626 return rc;
1627}
1628
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001629int
1630cifs_rename(struct inode *source_dir, struct dentry *source_dentry,
1631 struct inode *target_dir, struct dentry *target_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001633 char *from_name = NULL;
1634 char *to_name = NULL;
Jeff Layton639e7a92010-09-03 11:50:09 -04001635 struct cifs_sb_info *cifs_sb;
Jeff Layton7ffec372010-09-29 19:51:11 -04001636 struct tcon_link *tlink;
Steve French96daf2b2011-05-27 04:34:02 +00001637 struct cifs_tcon *tcon;
Steve Frenchee2fd962008-09-23 18:23:33 +00001638 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
1639 FILE_UNIX_BASIC_INFO *info_buf_target;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001640 unsigned int xid;
1641 int rc, tmprc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Jeff Layton639e7a92010-09-03 11:50:09 -04001643 cifs_sb = CIFS_SB(source_dir->i_sb);
Jeff Layton7ffec372010-09-29 19:51:11 -04001644 tlink = cifs_sb_tlink(cifs_sb);
1645 if (IS_ERR(tlink))
1646 return PTR_ERR(tlink);
1647 tcon = tlink_tcon(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001649 xid = get_xid();
Steve Frenchee2fd962008-09-23 18:23:33 +00001650
1651 /*
Steve Frenchee2fd962008-09-23 18:23:33 +00001652 * we already have the rename sem so we do not need to
1653 * grab it again here to protect the path integrity
1654 */
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001655 from_name = build_path_from_dentry(source_dentry);
1656 if (from_name == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 rc = -ENOMEM;
1658 goto cifs_rename_exit;
1659 }
1660
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001661 to_name = build_path_from_dentry(target_dentry);
1662 if (to_name == NULL) {
Steve Frenchee2fd962008-09-23 18:23:33 +00001663 rc = -ENOMEM;
1664 goto cifs_rename_exit;
1665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001667 rc = cifs_do_rename(xid, source_dentry, from_name, target_dentry,
1668 to_name);
Steve Frenchee2fd962008-09-23 18:23:33 +00001669
Jeff Layton14121bd2008-10-20 14:45:22 -04001670 if (rc == -EEXIST && tcon->unix_ext) {
Steve Frenchee2fd962008-09-23 18:23:33 +00001671 /*
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001672 * Are src and dst hardlinks of same inode? We can only tell
1673 * with unix extensions enabled.
Steve Frenchee2fd962008-09-23 18:23:33 +00001674 */
Jeff Layton14121bd2008-10-20 14:45:22 -04001675 info_buf_source =
1676 kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO),
1677 GFP_KERNEL);
1678 if (info_buf_source == NULL) {
1679 rc = -ENOMEM;
1680 goto cifs_rename_exit;
1681 }
1682
1683 info_buf_target = info_buf_source + 1;
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001684 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, from_name,
1685 info_buf_source,
1686 cifs_sb->local_nls,
1687 cifs_sb->mnt_cifs_flags &
1688 CIFS_MOUNT_MAP_SPECIAL_CHR);
Jeff Layton8d281ef2008-10-22 13:57:01 -04001689 if (tmprc != 0)
Jeff Layton14121bd2008-10-20 14:45:22 -04001690 goto unlink_target;
1691
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001692 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, to_name,
1693 info_buf_target,
1694 cifs_sb->local_nls,
1695 cifs_sb->mnt_cifs_flags &
1696 CIFS_MOUNT_MAP_SPECIAL_CHR);
Jeff Layton14121bd2008-10-20 14:45:22 -04001697
Jeff Layton8d281ef2008-10-22 13:57:01 -04001698 if (tmprc == 0 && (info_buf_source->UniqueId ==
Jeff Laytonae6884a2008-11-03 14:05:08 -05001699 info_buf_target->UniqueId)) {
Jeff Layton14121bd2008-10-20 14:45:22 -04001700 /* same file, POSIX says that this is a noop */
Jeff Laytonae6884a2008-11-03 14:05:08 -05001701 rc = 0;
Jeff Layton14121bd2008-10-20 14:45:22 -04001702 goto cifs_rename_exit;
Jeff Laytonae6884a2008-11-03 14:05:08 -05001703 }
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001704 }
1705 /*
1706 * else ... BB we could add the same check for Windows by
1707 * checking the UniqueId via FILE_INTERNAL_INFO
1708 */
Jeff Layton14121bd2008-10-20 14:45:22 -04001709
Jeff Layton14121bd2008-10-20 14:45:22 -04001710unlink_target:
Jeff Laytonfc6f3942009-04-17 11:45:30 -04001711 /* Try unlinking the target dentry if it's not negative */
1712 if (target_dentry->d_inode && (rc == -EACCES || rc == -EEXIST)) {
Jeff Layton8d281ef2008-10-22 13:57:01 -04001713 tmprc = cifs_unlink(target_dir, target_dentry);
Jeff Layton14121bd2008-10-20 14:45:22 -04001714 if (tmprc)
1715 goto cifs_rename_exit;
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001716 rc = cifs_do_rename(xid, source_dentry, from_name,
1717 target_dentry, to_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719
Pavel Shilovskyb46799a2014-08-18 20:49:58 +04001720 /* force revalidate to go get info when needed */
1721 CIFS_I(source_dir)->time = CIFS_I(target_dir)->time = 0;
1722
1723 source_dir->i_ctime = source_dir->i_mtime = target_dir->i_ctime =
1724 target_dir->i_mtime = current_fs_time(source_dir->i_sb);
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726cifs_rename_exit:
Steve Frenchee2fd962008-09-23 18:23:33 +00001727 kfree(info_buf_source);
Pavel Shilovsky8ceb9842012-09-18 16:20:30 -07001728 kfree(from_name);
1729 kfree(to_name);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001730 free_xid(xid);
Jeff Layton7ffec372010-09-29 19:51:11 -04001731 cifs_put_tlink(tlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return rc;
1733}
1734
Jeff Laytondf2cf172010-02-12 07:44:16 -05001735static bool
1736cifs_inode_needs_reval(struct inode *inode)
1737{
1738 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05301739 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Jeff Laytondf2cf172010-02-12 07:44:16 -05001740
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04001741 if (CIFS_CACHE_READ(cifs_i))
Jeff Laytondf2cf172010-02-12 07:44:16 -05001742 return false;
1743
1744 if (!lookupCacheEnabled)
1745 return true;
1746
1747 if (cifs_i->time == 0)
1748 return true;
1749
Jeff Laytona87c9ad2014-03-26 07:24:23 -07001750 if (!cifs_sb->actimeo)
1751 return true;
1752
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05301753 if (!time_in_range(jiffies, cifs_i->time,
1754 cifs_i->time + cifs_sb->actimeo))
Jeff Laytondf2cf172010-02-12 07:44:16 -05001755 return true;
1756
Jeff Laytondb192722010-05-17 14:51:49 -04001757 /* hardlinked files w/ noserverino get "special" treatment */
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05301758 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) &&
Jeff Laytondb192722010-05-17 14:51:49 -04001759 S_ISREG(inode->i_mode) && inode->i_nlink != 1)
1760 return true;
1761
Jeff Laytondf2cf172010-02-12 07:44:16 -05001762 return false;
1763}
1764
Suresh Jayaraman523fb8c2010-11-29 22:39:47 +05301765/*
1766 * Zap the cache. Called when invalid_mapping flag is set.
1767 */
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001768int
Jeff Laytondf2cf172010-02-12 07:44:16 -05001769cifs_invalidate_mapping(struct inode *inode)
1770{
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001771 int rc = 0;
Jeff Laytondf2cf172010-02-12 07:44:16 -05001772
Jeff Laytondf2cf172010-02-12 07:44:16 -05001773 if (inode->i_mapping && inode->i_mapping->nrpages != 0) {
Pavel Shilovsky257fb1f2011-03-16 01:55:32 +03001774 rc = invalidate_inode_pages2(inode->i_mapping);
Jeff Layton4f73c7d2014-04-30 09:31:47 -04001775 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05001776 cifs_dbg(VFS, "%s: could not invalidate inode %p\n",
1777 __func__, inode);
Jeff Laytondf2cf172010-02-12 07:44:16 -05001778 }
Pavel Shilovsky257fb1f2011-03-16 01:55:32 +03001779
Suresh Jayaraman9451a9a2010-07-05 18:12:45 +05301780 cifs_fscache_reset_inode_cookie(inode);
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001781 return rc;
Jeff Laytondf2cf172010-02-12 07:44:16 -05001782}
1783
Jeff Layton4f73c7d2014-04-30 09:31:47 -04001784/**
1785 * cifs_wait_bit_killable - helper for functions that are sleeping on bit locks
1786 * @word: long word containing the bit lock
1787 */
1788static int
NeilBrownc1221322014-07-07 15:16:04 +10001789cifs_wait_bit_killable(struct wait_bit_key *key)
Jeff Layton4f73c7d2014-04-30 09:31:47 -04001790{
1791 if (fatal_signal_pending(current))
1792 return -ERESTARTSYS;
1793 freezable_schedule_unsafe();
1794 return 0;
1795}
1796
Jeff Laytone284e532014-04-30 09:31:46 -04001797int
1798cifs_revalidate_mapping(struct inode *inode)
1799{
Jeff Layton4f73c7d2014-04-30 09:31:47 -04001800 int rc;
1801 unsigned long *flags = &CIFS_I(inode)->flags;
1802
NeilBrown74316202014-07-07 15:16:04 +10001803 rc = wait_on_bit_lock_action(flags, CIFS_INO_LOCK, cifs_wait_bit_killable,
1804 TASK_KILLABLE);
Jeff Layton4f73c7d2014-04-30 09:31:47 -04001805 if (rc)
1806 return rc;
1807
1808 if (test_and_clear_bit(CIFS_INO_INVALID_MAPPING, flags)) {
1809 rc = cifs_invalidate_mapping(inode);
1810 if (rc)
1811 set_bit(CIFS_INO_INVALID_MAPPING, flags);
1812 }
1813
1814 clear_bit_unlock(CIFS_INO_LOCK, flags);
Linus Torvaldsb1cce802014-06-09 19:08:43 -07001815 smp_mb__after_atomic();
Jeff Layton4f73c7d2014-04-30 09:31:47 -04001816 wake_up_bit(flags, CIFS_INO_LOCK);
1817
1818 return rc;
1819}
1820
1821int
1822cifs_zap_mapping(struct inode *inode)
1823{
1824 set_bit(CIFS_INO_INVALID_MAPPING, &CIFS_I(inode)->flags);
1825 return cifs_revalidate_mapping(inode);
Jeff Laytone284e532014-04-30 09:31:46 -04001826}
1827
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001828int cifs_revalidate_file_attr(struct file *filp)
Jeff Laytonabab0952010-02-12 07:44:18 -05001829{
1830 int rc = 0;
Al Viro496ad9a2013-01-23 17:07:38 -05001831 struct inode *inode = file_inode(filp);
Jeff Laytonba00ba62010-09-20 16:01:31 -07001832 struct cifsFileInfo *cfile = (struct cifsFileInfo *) filp->private_data;
Jeff Laytonabab0952010-02-12 07:44:18 -05001833
1834 if (!cifs_inode_needs_reval(inode))
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001835 return rc;
Jeff Laytonabab0952010-02-12 07:44:18 -05001836
Jeff Layton13cfb732010-09-29 19:51:11 -04001837 if (tlink_tcon(cfile->tlink)->unix_ext)
Jeff Laytonabab0952010-02-12 07:44:18 -05001838 rc = cifs_get_file_info_unix(filp);
1839 else
1840 rc = cifs_get_file_info(filp);
1841
Jeff Laytonabab0952010-02-12 07:44:18 -05001842 return rc;
1843}
1844
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001845int cifs_revalidate_dentry_attr(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001847 unsigned int xid;
Jeff Laytondf2cf172010-02-12 07:44:16 -05001848 int rc = 0;
Jeff Laytondf2cf172010-02-12 07:44:16 -05001849 struct inode *inode = dentry->d_inode;
1850 struct super_block *sb = dentry->d_sb;
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001851 char *full_path = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Jeff Laytondf2cf172010-02-12 07:44:16 -05001853 if (inode == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 return -ENOENT;
1855
Jeff Laytondf2cf172010-02-12 07:44:16 -05001856 if (!cifs_inode_needs_reval(inode))
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001857 return rc;
1858
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001859 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 /* can not safely grab the rename sem here if rename calls revalidate
1862 since that would deadlock */
Jeff Laytondf2cf172010-02-12 07:44:16 -05001863 full_path = build_path_from_dentry(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (full_path == NULL) {
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05301865 rc = -ENOMEM;
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001866 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
Jeff Laytondf2cf172010-02-12 07:44:16 -05001868
Joe Perchesf96637b2013-05-04 22:12:25 -05001869 cifs_dbg(FYI, "Update attributes: %s inode 0x%p count %d dentry: 0x%p d_time %ld jiffies %ld\n",
1870 full_path, inode, inode->i_count.counter,
Steve Frenchf19159d2010-04-21 04:12:10 +00001871 dentry, dentry->d_time, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Jeff Layton0d424ad2010-09-20 16:01:35 -07001873 if (cifs_sb_master_tcon(CIFS_SB(sb))->unix_ext)
Jeff Laytondf2cf172010-02-12 07:44:16 -05001874 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
1875 else
1876 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
1877 xid, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001879out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 kfree(full_path);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001881 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return rc;
1883}
1884
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001885int cifs_revalidate_file(struct file *filp)
1886{
1887 int rc;
Al Viro496ad9a2013-01-23 17:07:38 -05001888 struct inode *inode = file_inode(filp);
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001889
1890 rc = cifs_revalidate_file_attr(filp);
1891 if (rc)
1892 return rc;
1893
Jeff Laytone284e532014-04-30 09:31:46 -04001894 return cifs_revalidate_mapping(inode);
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001895}
1896
1897/* revalidate a dentry's inode attributes */
1898int cifs_revalidate_dentry(struct dentry *dentry)
1899{
1900 int rc;
1901 struct inode *inode = dentry->d_inode;
1902
1903 rc = cifs_revalidate_dentry_attr(dentry);
1904 if (rc)
1905 return rc;
1906
Jeff Laytone284e532014-04-30 09:31:46 -04001907 return cifs_revalidate_mapping(inode);
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001908}
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
Jeff Layton1c456012010-10-12 11:32:42 -04001911 struct kstat *stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Jeff Layton3aa1c8c2010-10-07 14:46:28 -04001913 struct cifs_sb_info *cifs_sb = CIFS_SB(dentry->d_sb);
Steve French96daf2b2011-05-27 04:34:02 +00001914 struct cifs_tcon *tcon = cifs_sb_master_tcon(cifs_sb);
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001915 struct inode *inode = dentry->d_inode;
1916 int rc;
Jeff Layton3aa1c8c2010-10-07 14:46:28 -04001917
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001918 /*
1919 * We need to be sure that all dirty pages are written and the server
1920 * has actual ctime, mtime and file length.
1921 */
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04001922 if (!CIFS_CACHE_READ(CIFS_I(inode)) && inode->i_mapping &&
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001923 inode->i_mapping->nrpages != 0) {
1924 rc = filemap_fdatawait(inode->i_mapping);
Steve French156ecb22011-05-20 17:00:01 +00001925 if (rc) {
1926 mapping_set_error(inode->i_mapping, rc);
1927 return rc;
1928 }
Steve French5fe14c82006-11-07 19:26:33 +00001929 }
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001930
1931 rc = cifs_revalidate_dentry_attr(dentry);
1932 if (rc)
1933 return rc;
1934
1935 generic_fillattr(inode, stat);
1936 stat->blksize = CIFS_MAX_MSGSIZE;
1937 stat->ino = CIFS_I(inode)->uniqueid;
1938
1939 /*
Jeff Laytond3d1fce2012-11-25 08:00:40 -05001940 * If on a multiuser mount without unix extensions or cifsacl being
1941 * enabled, and the admin hasn't overridden them, set the ownership
1942 * to the fsuid/fsgid of the current process.
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001943 */
1944 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER) &&
Jeff Laytond3d1fce2012-11-25 08:00:40 -05001945 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
Pavel Shilovsky6feb9892011-04-07 18:18:11 +04001946 !tcon->unix_ext) {
1947 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID))
1948 stat->uid = current_fsuid();
1949 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID))
1950 stat->gid = current_fsgid();
1951 }
1952 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953}
1954
1955static int cifs_truncate_page(struct address_space *mapping, loff_t from)
1956{
1957 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1958 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
1959 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 int rc = 0;
1961
1962 page = grab_cache_page(mapping, index);
1963 if (!page)
1964 return -ENOMEM;
1965
Christoph Lametereebd2aa2008-02-04 22:28:29 -08001966 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 unlock_page(page);
1968 page_cache_release(page);
1969 return rc;
1970}
1971
Christoph Hellwig1b947462010-07-18 17:51:21 -04001972static void cifs_setsize(struct inode *inode, loff_t offset)
Steve French3677db12007-02-26 16:46:11 +00001973{
Steve Frenchba6a46a2007-02-26 20:06:29 +00001974 spin_lock(&inode->i_lock);
Steve French3677db12007-02-26 16:46:11 +00001975 i_size_write(inode, offset);
Steve Frenchba6a46a2007-02-26 20:06:29 +00001976 spin_unlock(&inode->i_lock);
Christoph Hellwig1b947462010-07-18 17:51:21 -04001977
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07001978 truncate_pagecache(inode, offset);
Steve French3677db12007-02-26 16:46:11 +00001979}
1980
Jeff Layton8efdbde2008-07-23 21:28:12 +00001981static int
1982cifs_set_file_size(struct inode *inode, struct iattr *attrs,
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04001983 unsigned int xid, char *full_path)
Jeff Layton8efdbde2008-07-23 21:28:12 +00001984{
1985 int rc;
1986 struct cifsFileInfo *open_file;
1987 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1988 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Jeff Layton7ffec372010-09-29 19:51:11 -04001989 struct tcon_link *tlink = NULL;
Pavel Shilovskyd1433412012-09-18 16:20:31 -07001990 struct cifs_tcon *tcon = NULL;
1991 struct TCP_Server_Info *server;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04001992 struct cifs_io_parms io_parms;
Jeff Layton8efdbde2008-07-23 21:28:12 +00001993
1994 /*
1995 * To avoid spurious oplock breaks from server, in the case of
1996 * inodes that we already have open, avoid doing path based
1997 * setting of file size if we can do it by handle.
1998 * This keeps our caching token (oplock) and avoids timeouts
1999 * when the local oplock break takes longer to flush
2000 * writebehind data than the SMB timeout for the SetPathInfo
2001 * request would allow
2002 */
Jeff Layton6508d902010-09-29 19:51:11 -04002003 open_file = find_writable_file(cifsInode, true);
Jeff Layton8efdbde2008-07-23 21:28:12 +00002004 if (open_file) {
Pavel Shilovskyd1433412012-09-18 16:20:31 -07002005 tcon = tlink_tcon(open_file->tlink);
2006 server = tcon->ses->server;
2007 if (server->ops->set_file_size)
2008 rc = server->ops->set_file_size(xid, tcon, open_file,
2009 attrs->ia_size, false);
2010 else
2011 rc = -ENOSYS;
Dave Kleikamp6ab409b2009-08-31 11:07:12 -04002012 cifsFileInfo_put(open_file);
Joe Perchesf96637b2013-05-04 22:12:25 -05002013 cifs_dbg(FYI, "SetFSize for attrs rc = %d\n", rc);
Jeff Layton8efdbde2008-07-23 21:28:12 +00002014 if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
2015 unsigned int bytes_written;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002016
Pavel Shilovskyd1433412012-09-18 16:20:31 -07002017 io_parms.netfid = open_file->fid.netfid;
2018 io_parms.pid = open_file->pid;
2019 io_parms.tcon = tcon;
Pavel Shilovskyfa2989f2011-05-26 10:01:59 +04002020 io_parms.offset = 0;
2021 io_parms.length = attrs->ia_size;
2022 rc = CIFSSMBWrite(xid, &io_parms, &bytes_written,
2023 NULL, NULL, 1);
Joe Perchesf96637b2013-05-04 22:12:25 -05002024 cifs_dbg(FYI, "Wrt seteof rc %d\n", rc);
Jeff Layton8efdbde2008-07-23 21:28:12 +00002025 }
2026 } else
2027 rc = -EINVAL;
2028
Pavel Shilovskyd1433412012-09-18 16:20:31 -07002029 if (!rc)
2030 goto set_size_out;
Jeff Laytonba00ba62010-09-20 16:01:31 -07002031
Pavel Shilovskyd1433412012-09-18 16:20:31 -07002032 if (tcon == NULL) {
2033 tlink = cifs_sb_tlink(cifs_sb);
2034 if (IS_ERR(tlink))
2035 return PTR_ERR(tlink);
2036 tcon = tlink_tcon(tlink);
2037 server = tcon->ses->server;
Jeff Layton8efdbde2008-07-23 21:28:12 +00002038 }
2039
Pavel Shilovskyd1433412012-09-18 16:20:31 -07002040 /*
2041 * Set file size by pathname rather than by handle either because no
2042 * valid, writeable file handle for it was found or because there was
2043 * an error setting it by handle.
2044 */
2045 if (server->ops->set_path_size)
2046 rc = server->ops->set_path_size(xid, tcon, full_path,
2047 attrs->ia_size, cifs_sb, false);
2048 else
2049 rc = -ENOSYS;
Joe Perchesf96637b2013-05-04 22:12:25 -05002050 cifs_dbg(FYI, "SetEOF by path (setattrs) rc = %d\n", rc);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07002051 if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
2052 __u16 netfid;
2053 int oplock = 0;
2054
2055 rc = SMBLegacyOpen(xid, tcon, full_path, FILE_OPEN,
2056 GENERIC_WRITE, CREATE_NOT_DIR, &netfid,
2057 &oplock, NULL, cifs_sb->local_nls,
2058 cifs_sb->mnt_cifs_flags &
2059 CIFS_MOUNT_MAP_SPECIAL_CHR);
2060 if (rc == 0) {
2061 unsigned int bytes_written;
2062
2063 io_parms.netfid = netfid;
2064 io_parms.pid = current->tgid;
2065 io_parms.tcon = tcon;
2066 io_parms.offset = 0;
2067 io_parms.length = attrs->ia_size;
2068 rc = CIFSSMBWrite(xid, &io_parms, &bytes_written, NULL,
2069 NULL, 1);
Joe Perchesf96637b2013-05-04 22:12:25 -05002070 cifs_dbg(FYI, "wrt seteof rc %d\n", rc);
Pavel Shilovskyd1433412012-09-18 16:20:31 -07002071 CIFSSMBClose(xid, tcon, netfid);
2072 }
2073 }
2074 if (tlink)
2075 cifs_put_tlink(tlink);
2076
2077set_size_out:
Jeff Layton8efdbde2008-07-23 21:28:12 +00002078 if (rc == 0) {
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04002079 cifsInode->server_eof = attrs->ia_size;
Christoph Hellwig1b947462010-07-18 17:51:21 -04002080 cifs_setsize(inode, attrs->ia_size);
Jeff Layton8efdbde2008-07-23 21:28:12 +00002081 cifs_truncate_page(inode->i_mapping, inode->i_size);
2082 }
2083
2084 return rc;
2085}
2086
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002087static int
2088cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
2089{
2090 int rc;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002091 unsigned int xid;
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002092 char *full_path = NULL;
2093 struct inode *inode = direntry->d_inode;
2094 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
2095 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Jeff Layton7ffec372010-09-29 19:51:11 -04002096 struct tcon_link *tlink;
Steve French96daf2b2011-05-27 04:34:02 +00002097 struct cifs_tcon *pTcon;
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002098 struct cifs_unix_set_info_args *args = NULL;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04002099 struct cifsFileInfo *open_file;
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002100
Joe Perchesf96637b2013-05-04 22:12:25 -05002101 cifs_dbg(FYI, "setattr_unix on file %s attrs->ia_valid=0x%x\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00002102 direntry->d_name.name, attrs->ia_valid);
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002103
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002104 xid = get_xid();
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002105
Christoph Hellwigdb78b872010-06-04 11:30:03 +02002106 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2107 attrs->ia_valid |= ATTR_FORCE;
2108
2109 rc = inode_change_ok(inode, attrs);
2110 if (rc < 0)
2111 goto out;
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002112
2113 full_path = build_path_from_dentry(direntry);
2114 if (full_path == NULL) {
2115 rc = -ENOMEM;
2116 goto out;
2117 }
2118
Jeff Layton0f4d6342009-03-26 13:35:37 -04002119 /*
2120 * Attempt to flush data before changing attributes. We need to do
2121 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2122 * ownership or mode then we may also need to do this. Here, we take
2123 * the safe way out and just do the flush on all setattr requests. If
2124 * the flush returns error, store it to report later and continue.
2125 *
2126 * BB: This should be smarter. Why bother flushing pages that
2127 * will be truncated anyway? Also, should we error out here if
2128 * the flush returns error?
2129 */
2130 rc = filemap_write_and_wait(inode->i_mapping);
Jeff Laytoneb4b7562010-10-22 14:52:29 -04002131 mapping_set_error(inode->i_mapping, rc);
2132 rc = 0;
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002133
2134 if (attrs->ia_valid & ATTR_SIZE) {
2135 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2136 if (rc != 0)
2137 goto out;
2138 }
2139
2140 /* skip mode change if it's just for clearing setuid/setgid */
2141 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2142 attrs->ia_valid &= ~ATTR_MODE;
2143
2144 args = kmalloc(sizeof(*args), GFP_KERNEL);
2145 if (args == NULL) {
2146 rc = -ENOMEM;
2147 goto out;
2148 }
2149
2150 /* set up the struct */
2151 if (attrs->ia_valid & ATTR_MODE)
2152 args->mode = attrs->ia_mode;
2153 else
2154 args->mode = NO_CHANGE_64;
2155
2156 if (attrs->ia_valid & ATTR_UID)
2157 args->uid = attrs->ia_uid;
2158 else
Eric W. Biederman49418b22013-02-06 00:57:56 -08002159 args->uid = INVALID_UID; /* no change */
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002160
2161 if (attrs->ia_valid & ATTR_GID)
2162 args->gid = attrs->ia_gid;
2163 else
Eric W. Biederman49418b22013-02-06 00:57:56 -08002164 args->gid = INVALID_GID; /* no change */
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002165
2166 if (attrs->ia_valid & ATTR_ATIME)
2167 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
2168 else
2169 args->atime = NO_CHANGE_64;
2170
2171 if (attrs->ia_valid & ATTR_MTIME)
2172 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
2173 else
2174 args->mtime = NO_CHANGE_64;
2175
2176 if (attrs->ia_valid & ATTR_CTIME)
2177 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
2178 else
2179 args->ctime = NO_CHANGE_64;
2180
2181 args->device = 0;
Jeff Layton6508d902010-09-29 19:51:11 -04002182 open_file = find_writable_file(cifsInode, true);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04002183 if (open_file) {
Pavel Shilovsky4b4de762012-09-18 16:20:26 -07002184 u16 nfid = open_file->fid.netfid;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04002185 u32 npid = open_file->pid;
Jeff Layton13cfb732010-09-29 19:51:11 -04002186 pTcon = tlink_tcon(open_file->tlink);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04002187 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
Dave Kleikamp6ab409b2009-08-31 11:07:12 -04002188 cifsFileInfo_put(open_file);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04002189 } else {
Jeff Layton7ffec372010-09-29 19:51:11 -04002190 tlink = cifs_sb_tlink(cifs_sb);
2191 if (IS_ERR(tlink)) {
2192 rc = PTR_ERR(tlink);
2193 goto out;
2194 }
2195 pTcon = tlink_tcon(tlink);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04002196 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
Jeff Layton01ea95e2009-07-09 20:02:49 -04002197 cifs_sb->local_nls,
2198 cifs_sb->mnt_cifs_flags &
2199 CIFS_MOUNT_MAP_SPECIAL_CHR);
Jeff Layton7ffec372010-09-29 19:51:11 -04002200 cifs_put_tlink(tlink);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04002201 }
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002202
Christoph Hellwig10257742010-06-04 11:30:02 +02002203 if (rc)
2204 goto out;
Steve Frenchccd4bb12010-02-08 17:39:58 +00002205
Christoph Hellwig10257742010-06-04 11:30:02 +02002206 if ((attrs->ia_valid & ATTR_SIZE) &&
Christoph Hellwig1b947462010-07-18 17:51:21 -04002207 attrs->ia_size != i_size_read(inode))
2208 truncate_setsize(inode, attrs->ia_size);
Christoph Hellwig10257742010-06-04 11:30:02 +02002209
2210 setattr_copy(inode, attrs);
2211 mark_inode_dirty(inode);
2212
2213 /* force revalidate when any of these times are set since some
2214 of the fs types (eg ext3, fat) do not have fine enough
2215 time granularity to match protocol, and we do not have a
2216 a way (yet) to query the server fs's time granularity (and
2217 whether it rounds times down).
2218 */
2219 if (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME))
2220 cifsInode->time = 0;
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002221out:
2222 kfree(args);
2223 kfree(full_path);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002224 free_xid(xid);
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002225 return rc;
2226}
2227
Jeff Layton0510eeb2008-08-02 07:26:12 -04002228static int
2229cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002231 unsigned int xid;
Eric W. Biederman8abf2772013-02-06 00:33:17 -08002232 kuid_t uid = INVALID_UID;
2233 kgid_t gid = INVALID_GID;
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002234 struct inode *inode = direntry->d_inode;
2235 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002236 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 char *full_path = NULL;
2238 int rc = -EACCES;
Jeff Laytonfeb3e202008-08-02 07:26:12 -04002239 __u32 dosattr = 0;
Jeff Layton4e1e7fb2008-08-02 07:26:12 -04002240 __u64 mode = NO_CHANGE_64;
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002241
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002242 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Joe Perchesf96637b2013-05-04 22:12:25 -05002244 cifs_dbg(FYI, "setattr on file %s attrs->iavalid 0x%x\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00002245 direntry->d_name.name, attrs->ia_valid);
Steve French6473a552005-11-29 20:20:10 -08002246
Christoph Hellwigdb78b872010-06-04 11:30:03 +02002247 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM)
2248 attrs->ia_valid |= ATTR_FORCE;
2249
2250 rc = inode_change_ok(inode, attrs);
2251 if (rc < 0) {
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002252 free_xid(xid);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02002253 return rc;
Steve French6473a552005-11-29 20:20:10 -08002254 }
Steve French50c2f752007-07-13 00:33:32 +00002255
Steve French7f573562005-08-30 11:32:14 -07002256 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 if (full_path == NULL) {
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05302258 rc = -ENOMEM;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002259 free_xid(xid);
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05302260 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Jeff Layton0f4d6342009-03-26 13:35:37 -04002263 /*
2264 * Attempt to flush data before changing attributes. We need to do
2265 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
2266 * ownership or mode then we may also need to do this. Here, we take
2267 * the safe way out and just do the flush on all setattr requests. If
2268 * the flush returns error, store it to report later and continue.
2269 *
2270 * BB: This should be smarter. Why bother flushing pages that
2271 * will be truncated anyway? Also, should we error out here if
2272 * the flush returns error?
2273 */
2274 rc = filemap_write_and_wait(inode->i_mapping);
Jeff Laytoneb4b7562010-10-22 14:52:29 -04002275 mapping_set_error(inode->i_mapping, rc);
2276 rc = 0;
Jeff Laytoncea21802007-11-20 23:19:03 +00002277
Steve French50531442008-03-14 19:21:31 +00002278 if (attrs->ia_valid & ATTR_SIZE) {
Jeff Layton8efdbde2008-07-23 21:28:12 +00002279 rc = cifs_set_file_size(inode, attrs, xid, full_path);
2280 if (rc != 0)
Steve Frenche30dcf32005-09-20 20:49:16 -07002281 goto cifs_setattr_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 }
Jeff Layton4ca691a2008-05-22 09:33:34 -04002283
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05002284 if (attrs->ia_valid & ATTR_UID)
2285 uid = attrs->ia_uid;
2286
2287 if (attrs->ia_valid & ATTR_GID)
2288 gid = attrs->ia_gid;
2289
2290#ifdef CONFIG_CIFS_ACL
2291 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
Eric W. Biederman8abf2772013-02-06 00:33:17 -08002292 if (uid_valid(uid) || gid_valid(gid)) {
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05002293 rc = id_mode_to_cifs_acl(inode, full_path, NO_CHANGE_64,
2294 uid, gid);
2295 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002296 cifs_dbg(FYI, "%s: Setting id failed with error: %d\n",
2297 __func__, rc);
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05002298 goto cifs_setattr_exit;
2299 }
2300 }
2301 } else
2302#endif /* CONFIG_CIFS_ACL */
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04002303 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
Jeff Layton4ca691a2008-05-22 09:33:34 -04002304 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Jeff Laytond32c4f22007-10-18 03:05:22 -07002306 /* skip mode change if it's just for clearing setuid/setgid */
2307 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
2308 attrs->ia_valid &= ~ATTR_MODE;
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 if (attrs->ia_valid & ATTR_MODE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 mode = attrs->ia_mode;
Steve Frenchcdbce9c2005-11-19 21:04:52 -08002312 rc = 0;
Jeff Layton79df1ba2010-12-06 12:52:08 -05002313#ifdef CONFIG_CIFS_ACL
Shirish Pargaonkar78415d22010-11-27 11:37:26 -06002314 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
Shirish Pargaonkara5ff3762011-10-13 10:26:03 -05002315 rc = id_mode_to_cifs_acl(inode, full_path, mode,
Eric W. Biederman8abf2772013-02-06 00:33:17 -08002316 INVALID_UID, INVALID_GID);
Shirish Pargaonkar78415d22010-11-27 11:37:26 -06002317 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002318 cifs_dbg(FYI, "%s: Setting ACL failed with error: %d\n",
2319 __func__, rc);
Shirish Pargaonkar78415d22010-11-27 11:37:26 -06002320 goto cifs_setattr_exit;
2321 }
2322 } else
Jeff Layton79df1ba2010-12-06 12:52:08 -05002323#endif /* CONFIG_CIFS_ACL */
Jeff Layton51328612008-05-22 09:33:34 -04002324 if (((mode & S_IWUGO) == 0) &&
2325 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
Jeff Laytonfeb3e202008-08-02 07:26:12 -04002326
2327 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
2328
Jeff Layton51328612008-05-22 09:33:34 -04002329 /* fix up mode if we're not using dynperm */
2330 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
2331 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
2332 } else if ((mode & S_IWUGO) &&
2333 (cifsInode->cifsAttrs & ATTR_READONLY)) {
Jeff Laytonfeb3e202008-08-02 07:26:12 -04002334
2335 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
2336 /* Attributes of 0 are ignored */
2337 if (dosattr == 0)
2338 dosattr |= ATTR_NORMAL;
Jeff Layton51328612008-05-22 09:33:34 -04002339
2340 /* reset local inode permissions to normal */
2341 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2342 attrs->ia_mode &= ~(S_IALLUGO);
2343 if (S_ISDIR(inode->i_mode))
2344 attrs->ia_mode |=
2345 cifs_sb->mnt_dir_mode;
2346 else
2347 attrs->ia_mode |=
2348 cifs_sb->mnt_file_mode;
2349 }
2350 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2351 /* ignore mode change - ATTR_READONLY hasn't changed */
2352 attrs->ia_valid &= ~ATTR_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 }
2355
Jeff Laytonfeb3e202008-08-02 07:26:12 -04002356 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
2357 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
2358 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
2359 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Steve Frenche30dcf32005-09-20 20:49:16 -07002361 /* Even if error on time set, no sense failing the call if
2362 the server would set the time to a reasonable value anyway,
2363 and this check ensures that we are not being called from
2364 sys_utimes in which case we ought to fail the call back to
2365 the user when the server rejects the call */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002366 if ((rc) && (attrs->ia_valid &
Jeff Laytonfeb3e202008-08-02 07:26:12 -04002367 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
Steve Frenche30dcf32005-09-20 20:49:16 -07002368 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
2370
2371 /* do not need local check to inode_check_ok since the server does
2372 that */
Christoph Hellwig10257742010-06-04 11:30:02 +02002373 if (rc)
2374 goto cifs_setattr_exit;
2375
2376 if ((attrs->ia_valid & ATTR_SIZE) &&
Christoph Hellwig1b947462010-07-18 17:51:21 -04002377 attrs->ia_size != i_size_read(inode))
2378 truncate_setsize(inode, attrs->ia_size);
Christoph Hellwig10257742010-06-04 11:30:02 +02002379
2380 setattr_copy(inode, attrs);
2381 mark_inode_dirty(inode);
Christoph Hellwig10257742010-06-04 11:30:02 +02002382
Steve Frenche30dcf32005-09-20 20:49:16 -07002383cifs_setattr_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 kfree(full_path);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002385 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 return rc;
2387}
2388
Jeff Layton0510eeb2008-08-02 07:26:12 -04002389int
2390cifs_setattr(struct dentry *direntry, struct iattr *attrs)
2391{
2392 struct inode *inode = direntry->d_inode;
2393 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Steve French96daf2b2011-05-27 04:34:02 +00002394 struct cifs_tcon *pTcon = cifs_sb_master_tcon(cifs_sb);
Jeff Layton0510eeb2008-08-02 07:26:12 -04002395
2396 if (pTcon->unix_ext)
2397 return cifs_setattr_unix(direntry, attrs);
2398
2399 return cifs_setattr_nounix(direntry, attrs);
2400
2401 /* BB: add cifs_setattr_legacy for really old servers */
2402}
2403
Steve French99ee4db2007-02-27 05:35:17 +00002404#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405void cifs_delete_inode(struct inode *inode)
2406{
Joe Perchesf96637b2013-05-04 22:12:25 -05002407 cifs_dbg(FYI, "In cifs_delete_inode, inode = 0x%p\n", inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 /* may have to add back in if and when safe distributed caching of
2409 directories added e.g. via FindNotify */
2410}
Steve French99ee4db2007-02-27 05:35:17 +00002411#endif