blob: e3fda978f4816eca17b9f5e4194fd161a224c5fe [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/inode.c
3 *
Steve French8be0ed42008-12-05 19:14:12 +00004 * Copyright (C) International Business Machines Corp., 2002,2008
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>
23#include <linux/pagemap.h>
24#include <asm/div64.h>
25#include "cifsfs.h"
26#include "cifspdu.h"
27#include "cifsglob.h"
28#include "cifsproto.h"
29#include "cifs_debug.h"
30#include "cifs_fs_sb.h"
31
Christoph Hellwig70eff552008-02-15 20:55:05 +000032
Igor Mammedov79626702008-03-09 03:44:18 +000033static void cifs_set_ops(struct inode *inode, const bool is_dfs_referral)
Christoph Hellwig70eff552008-02-15 20:55:05 +000034{
35 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
36
37 switch (inode->i_mode & S_IFMT) {
38 case S_IFREG:
39 inode->i_op = &cifs_file_inode_ops;
40 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
41 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
42 inode->i_fop = &cifs_file_direct_nobrl_ops;
43 else
44 inode->i_fop = &cifs_file_direct_ops;
45 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
46 inode->i_fop = &cifs_file_nobrl_ops;
47 else { /* not direct, send byte range locks */
48 inode->i_fop = &cifs_file_ops;
49 }
50
51
52 /* check if server can support readpages */
53 if (cifs_sb->tcon->ses->server->maxBuf <
54 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)
55 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
56 else
57 inode->i_data.a_ops = &cifs_addr_ops;
58 break;
59 case S_IFDIR:
Steve Frenchbc5b6e22008-03-11 21:07:48 +000060#ifdef CONFIG_CIFS_DFS_UPCALL
Igor Mammedov79626702008-03-09 03:44:18 +000061 if (is_dfs_referral) {
62 inode->i_op = &cifs_dfs_referral_inode_operations;
63 } else {
Steve Frenchbc5b6e22008-03-11 21:07:48 +000064#else /* NO DFS support, treat as a directory */
65 {
66#endif
Igor Mammedov79626702008-03-09 03:44:18 +000067 inode->i_op = &cifs_dir_inode_ops;
68 inode->i_fop = &cifs_dir_ops;
69 }
Christoph Hellwig70eff552008-02-15 20:55:05 +000070 break;
71 case S_IFLNK:
72 inode->i_op = &cifs_symlink_inode_ops;
73 break;
74 default:
75 init_special_inode(inode, inode->i_mode, inode->i_rdev);
76 break;
77 }
78}
79
Jeff Laytoncc0bad72009-06-25 00:56:52 -040080/* populate an inode with info from a cifs_fattr struct */
81void
82cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
Christoph Hellwig75f12982008-02-25 20:25:21 +000083{
Jeff Laytoncc0bad72009-06-25 00:56:52 -040084 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
Jeff Layton0b8f18e2009-07-09 01:46:37 -040085 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
86 unsigned long oldtime = cifs_i->time;
Christoph Hellwig75f12982008-02-25 20:25:21 +000087
Jeff Laytoncc0bad72009-06-25 00:56:52 -040088 inode->i_atime = fattr->cf_atime;
89 inode->i_mtime = fattr->cf_mtime;
90 inode->i_ctime = fattr->cf_ctime;
Jeff Laytoncc0bad72009-06-25 00:56:52 -040091 inode->i_rdev = fattr->cf_rdev;
92 inode->i_nlink = fattr->cf_nlink;
93 inode->i_uid = fattr->cf_uid;
94 inode->i_gid = fattr->cf_gid;
95
Jeff Layton0b8f18e2009-07-09 01:46:37 -040096 /* if dynperm is set, don't clobber existing mode */
97 if (inode->i_state & I_NEW ||
98 !(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM))
99 inode->i_mode = fattr->cf_mode;
100
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400101 cifs_i->cifsAttrs = fattr->cf_cifsattrs;
102 cifs_i->uniqueid = fattr->cf_uniqueid;
103
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400104 if (fattr->cf_flags & CIFS_FATTR_NEED_REVAL)
105 cifs_i->time = 0;
106 else
107 cifs_i->time = jiffies;
108
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400109 cFYI(1, ("inode 0x%p old_time=%ld new_time=%ld", inode,
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400110 oldtime, cifs_i->time));
111
112 cifs_i->delete_pending = fattr->cf_flags & CIFS_FATTR_DELETE_PENDING;
Christoph Hellwig75f12982008-02-25 20:25:21 +0000113
114 /*
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400115 * Can't safely change the file size here if the client is writing to
116 * it due to potential races.
Christoph Hellwig75f12982008-02-25 20:25:21 +0000117 */
Christoph Hellwig75f12982008-02-25 20:25:21 +0000118 spin_lock(&inode->i_lock);
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400119 if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
120 i_size_write(inode, fattr->cf_eof);
Christoph Hellwig75f12982008-02-25 20:25:21 +0000121
122 /*
123 * i_blocks is not related to (i_size / i_blksize),
124 * but instead 512 byte (2**9) size is required for
125 * calculating num blocks.
126 */
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400127 inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
Christoph Hellwig75f12982008-02-25 20:25:21 +0000128 }
129 spin_unlock(&inode->i_lock);
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400130
131 cifs_set_ops(inode, fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL);
Christoph Hellwig75f12982008-02-25 20:25:21 +0000132}
133
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400134/* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
135void
136cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
137 struct cifs_sb_info *cifs_sb)
138{
139 memset(fattr, 0, sizeof(*fattr));
140 fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
141 fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
142 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
143
144 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
145 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
146 fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
147 fattr->cf_mode = le64_to_cpu(info->Permissions);
148
149 /*
150 * Since we set the inode type below we need to mask off
151 * to avoid strange results if bits set above.
152 */
153 fattr->cf_mode &= ~S_IFMT;
154 switch (le32_to_cpu(info->Type)) {
155 case UNIX_FILE:
156 fattr->cf_mode |= S_IFREG;
157 fattr->cf_dtype = DT_REG;
158 break;
159 case UNIX_SYMLINK:
160 fattr->cf_mode |= S_IFLNK;
161 fattr->cf_dtype = DT_LNK;
162 break;
163 case UNIX_DIR:
164 fattr->cf_mode |= S_IFDIR;
165 fattr->cf_dtype = DT_DIR;
166 break;
167 case UNIX_CHARDEV:
168 fattr->cf_mode |= S_IFCHR;
169 fattr->cf_dtype = DT_CHR;
170 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
171 le64_to_cpu(info->DevMinor) & MINORMASK);
172 break;
173 case UNIX_BLOCKDEV:
174 fattr->cf_mode |= S_IFBLK;
175 fattr->cf_dtype = DT_BLK;
176 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
177 le64_to_cpu(info->DevMinor) & MINORMASK);
178 break;
179 case UNIX_FIFO:
180 fattr->cf_mode |= S_IFIFO;
181 fattr->cf_dtype = DT_FIFO;
182 break;
183 case UNIX_SOCKET:
184 fattr->cf_mode |= S_IFSOCK;
185 fattr->cf_dtype = DT_SOCK;
186 break;
187 default:
188 /* safest to call it a file if we do not know */
189 fattr->cf_mode |= S_IFREG;
190 fattr->cf_dtype = DT_REG;
191 cFYI(1, ("unknown type %d", le32_to_cpu(info->Type)));
192 break;
193 }
194
195 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
196 fattr->cf_uid = cifs_sb->mnt_uid;
197 else
198 fattr->cf_uid = le64_to_cpu(info->Uid);
199
200 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
201 fattr->cf_gid = cifs_sb->mnt_gid;
202 else
203 fattr->cf_gid = le64_to_cpu(info->Gid);
204
205 fattr->cf_nlink = le64_to_cpu(info->Nlinks);
206}
Steve Frenchb9a32602008-05-20 21:52:32 +0000207
208/*
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400209 * Fill a cifs_fattr struct with fake inode info.
210 *
211 * Needed to setup cifs_fattr data for the directory which is the
212 * junction to the new submount (ie to setup the fake directory
213 * which represents a DFS referral).
Steve Frenchb9a32602008-05-20 21:52:32 +0000214 */
Steve Frenchf1230c92009-07-22 23:13:01 +0000215static void
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400216cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb)
Steve French0e4bbde2008-05-20 19:50:46 +0000217{
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400218 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
Steve French0e4bbde2008-05-20 19:50:46 +0000219
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400220 cFYI(1, ("creating fake fattr for DFS referral"));
Steve French0e4bbde2008-05-20 19:50:46 +0000221
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400222 memset(fattr, 0, sizeof(*fattr));
223 fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
224 fattr->cf_uid = cifs_sb->mnt_uid;
225 fattr->cf_gid = cifs_sb->mnt_gid;
226 fattr->cf_atime = CURRENT_TIME;
227 fattr->cf_ctime = CURRENT_TIME;
228 fattr->cf_mtime = CURRENT_TIME;
229 fattr->cf_nlink = 2;
230 fattr->cf_flags |= CIFS_FATTR_DFS_REFERRAL;
Steve French0e4bbde2008-05-20 19:50:46 +0000231}
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233int cifs_get_inode_info_unix(struct inode **pinode,
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400234 const unsigned char *full_path,
235 struct super_block *sb, int xid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400237 int rc;
Steve French0e4bbde2008-05-20 19:50:46 +0000238 FILE_UNIX_BASIC_INFO find_data;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400239 struct cifs_fattr fattr;
240 struct cifsTconInfo *tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400243 tcon = cifs_sb->tcon;
Steve French646dd532008-05-15 01:50:56 +0000244 cFYI(1, ("Getting info on %s", full_path));
Igor Mammedov79626702008-03-09 03:44:18 +0000245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 /* could have done a find first instead but this returns more info */
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400247 rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
Steve French737b7582005-04-28 22:41:06 -0700248 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
249 CIFS_MOUNT_MAP_SPECIAL_CHR);
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400250
251 if (!rc) {
252 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
253 } else if (rc == -EREMOTE) {
254 cifs_create_dfs_fattr(&fattr, sb);
Jeff Laytone911d0c2008-07-12 13:47:59 -0700255 rc = 0;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400256 } else {
257 return rc;
Steve French0e4bbde2008-05-20 19:50:46 +0000258 }
259
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400260 if (*pinode == NULL) {
261 /* get new inode */
262 *pinode = cifs_iget(sb, &fattr);
263 if (!*pinode)
264 rc = -ENOMEM;
265 } else {
266 /* we already have inode, update it */
267 cifs_fattr_to_inode(*pinode, &fattr);
268 }
Steve French0e4bbde2008-05-20 19:50:46 +0000269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return rc;
271}
272
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400273static int
274cifs_sfu_type(struct cifs_fattr *fattr, const unsigned char *path,
275 struct cifs_sb_info *cifs_sb, int xid)
Steve Frenchd6e2f2a2005-11-15 16:43:39 -0800276{
277 int rc;
Steve French4b18f2a2008-04-29 00:06:05 +0000278 int oplock = 0;
Steve Frenchd6e2f2a2005-11-15 16:43:39 -0800279 __u16 netfid;
280 struct cifsTconInfo *pTcon = cifs_sb->tcon;
Steve French86c96b42005-11-18 20:25:31 -0800281 char buf[24];
Steve Frenchd6e2f2a2005-11-15 16:43:39 -0800282 unsigned int bytes_read;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000283 char *pbuf;
Steve Frenchd6e2f2a2005-11-15 16:43:39 -0800284
285 pbuf = buf;
286
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400287 fattr->cf_mode &= ~S_IFMT;
288
289 if (fattr->cf_eof == 0) {
290 fattr->cf_mode |= S_IFIFO;
291 fattr->cf_dtype = DT_FIFO;
Steve Frenchd6e2f2a2005-11-15 16:43:39 -0800292 return 0;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400293 } else if (fattr->cf_eof < 8) {
294 fattr->cf_mode |= S_IFREG;
295 fattr->cf_dtype = DT_REG;
Steve Frenchd6e2f2a2005-11-15 16:43:39 -0800296 return -EINVAL; /* EOPNOTSUPP? */
297 }
Steve French50c2f752007-07-13 00:33:32 +0000298
Steve Frenchd6e2f2a2005-11-15 16:43:39 -0800299 rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ,
300 CREATE_NOT_DIR, &netfid, &oplock, NULL,
301 cifs_sb->local_nls,
302 cifs_sb->mnt_cifs_flags &
303 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000304 if (rc == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -0800305 int buf_type = CIFS_NO_BUFFER;
Steve Frenchd6e2f2a2005-11-15 16:43:39 -0800306 /* Read header */
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400307 rc = CIFSSMBRead(xid, pTcon, netfid,
Steve French86c96b42005-11-18 20:25:31 -0800308 24 /* length */, 0 /* offset */,
Steve Frenchec637e32005-12-12 20:53:18 -0800309 &bytes_read, &pbuf, &buf_type);
Steve French4523cc32007-04-30 20:13:06 +0000310 if ((rc == 0) && (bytes_read >= 8)) {
311 if (memcmp("IntxBLK", pbuf, 8) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000312 cFYI(1, ("Block device"));
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400313 fattr->cf_mode |= S_IFBLK;
314 fattr->cf_dtype = DT_BLK;
Steve French4523cc32007-04-30 20:13:06 +0000315 if (bytes_read == 24) {
Steve French86c96b42005-11-18 20:25:31 -0800316 /* we have enough to decode dev num */
317 __u64 mjr; /* major */
318 __u64 mnr; /* minor */
319 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
320 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400321 fattr->cf_rdev = MKDEV(mjr, mnr);
Steve French86c96b42005-11-18 20:25:31 -0800322 }
Steve French4523cc32007-04-30 20:13:06 +0000323 } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000324 cFYI(1, ("Char device"));
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400325 fattr->cf_mode |= S_IFCHR;
326 fattr->cf_dtype = DT_CHR;
Steve French4523cc32007-04-30 20:13:06 +0000327 if (bytes_read == 24) {
Steve French86c96b42005-11-18 20:25:31 -0800328 /* we have enough to decode dev num */
329 __u64 mjr; /* major */
330 __u64 mnr; /* minor */
331 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
332 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400333 fattr->cf_rdev = MKDEV(mjr, mnr);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000334 }
Steve French4523cc32007-04-30 20:13:06 +0000335 } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000336 cFYI(1, ("Symlink"));
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400337 fattr->cf_mode |= S_IFLNK;
338 fattr->cf_dtype = DT_LNK;
Steve French86c96b42005-11-18 20:25:31 -0800339 } else {
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400340 fattr->cf_mode |= S_IFREG; /* file? */
341 fattr->cf_dtype = DT_REG;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000342 rc = -EOPNOTSUPP;
Steve French86c96b42005-11-18 20:25:31 -0800343 }
Steve French3020a1f2005-11-18 11:31:10 -0800344 } else {
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400345 fattr->cf_mode |= S_IFREG; /* then it is a file */
346 fattr->cf_dtype = DT_REG;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000347 rc = -EOPNOTSUPP; /* or some unknown SFU type */
348 }
Steve Frenchd6e2f2a2005-11-15 16:43:39 -0800349 CIFSSMBClose(xid, pTcon, netfid);
Steve Frenchd6e2f2a2005-11-15 16:43:39 -0800350 }
351 return rc;
Steve Frenchd6e2f2a2005-11-15 16:43:39 -0800352}
353
Steve French9e294f12005-11-17 16:59:21 -0800354#define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
355
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400356/*
357 * Fetch mode bits as provided by SFU.
358 *
359 * FIXME: Doesn't this clobber the type bit we got from cifs_sfu_type ?
360 */
361static int cifs_sfu_mode(struct cifs_fattr *fattr, const unsigned char *path,
362 struct cifs_sb_info *cifs_sb, int xid)
Steve French9e294f12005-11-17 16:59:21 -0800363{
Steve French3020a1f2005-11-18 11:31:10 -0800364#ifdef CONFIG_CIFS_XATTR
Steve French9e294f12005-11-17 16:59:21 -0800365 ssize_t rc;
366 char ea_value[4];
367 __u32 mode;
368
369 rc = CIFSSMBQueryEA(xid, cifs_sb->tcon, path, "SETFILEBITS",
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400370 ea_value, 4 /* size of buf */, cifs_sb->local_nls,
371 cifs_sb->mnt_cifs_flags &
372 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve French4523cc32007-04-30 20:13:06 +0000373 if (rc < 0)
Steve French9e294f12005-11-17 16:59:21 -0800374 return (int)rc;
375 else if (rc > 3) {
376 mode = le32_to_cpu(*((__le32 *)ea_value));
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400377 fattr->cf_mode &= ~SFBITS_MASK;
378 cFYI(1, ("special bits 0%o org mode 0%o", mode,
379 fattr->cf_mode));
380 fattr->cf_mode = (mode & SFBITS_MASK) | fattr->cf_mode;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000381 cFYI(1, ("special mode bits 0%o", mode));
Steve French9e294f12005-11-17 16:59:21 -0800382 }
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400383
384 return 0;
Steve French3020a1f2005-11-18 11:31:10 -0800385#else
386 return -EOPNOTSUPP;
387#endif
Steve French9e294f12005-11-17 16:59:21 -0800388}
389
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400390/* Fill a cifs_fattr struct with info from FILE_ALL_INFO */
Steve Frenchf1230c92009-07-22 23:13:01 +0000391static void
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400392cifs_all_info_to_fattr(struct cifs_fattr *fattr, FILE_ALL_INFO *info,
393 struct cifs_sb_info *cifs_sb, bool adjust_tz)
Steve Frenchb9a32602008-05-20 21:52:32 +0000394{
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400395 memset(fattr, 0, sizeof(*fattr));
396 fattr->cf_cifsattrs = le32_to_cpu(info->Attributes);
397 if (info->DeletePending)
398 fattr->cf_flags |= CIFS_FATTR_DELETE_PENDING;
Steve Frenchb9a32602008-05-20 21:52:32 +0000399
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400400 if (info->LastAccessTime)
401 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
402 else
403 fattr->cf_atime = CURRENT_TIME;
404
405 fattr->cf_ctime = cifs_NTtimeToUnix(info->ChangeTime);
406 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastWriteTime);
407
408 if (adjust_tz) {
409 fattr->cf_ctime.tv_sec += cifs_sb->tcon->ses->server->timeAdj;
410 fattr->cf_mtime.tv_sec += cifs_sb->tcon->ses->server->timeAdj;
411 }
412
413 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
414 fattr->cf_bytes = le64_to_cpu(info->AllocationSize);
415
416 if (fattr->cf_cifsattrs & ATTR_DIRECTORY) {
417 fattr->cf_mode = S_IFDIR | cifs_sb->mnt_dir_mode;
418 fattr->cf_dtype = DT_DIR;
419 } else {
420 fattr->cf_mode = S_IFREG | cifs_sb->mnt_file_mode;
421 fattr->cf_dtype = DT_REG;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400422
Jeff Laytond0c280d2009-07-09 01:46:44 -0400423 /* clear write bits if ATTR_READONLY is set */
424 if (fattr->cf_cifsattrs & ATTR_READONLY)
425 fattr->cf_mode &= ~(S_IWUGO);
426 }
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400427
428 fattr->cf_nlink = le32_to_cpu(info->NumberOfLinks);
429
430 fattr->cf_uid = cifs_sb->mnt_uid;
431 fattr->cf_gid = cifs_sb->mnt_gid;
Steve Frenchb9a32602008-05-20 21:52:32 +0000432}
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434int cifs_get_inode_info(struct inode **pinode,
Steve French646dd532008-05-15 01:50:56 +0000435 const unsigned char *full_path, FILE_ALL_INFO *pfindData,
Steve French8b1327f2008-03-14 22:37:16 +0000436 struct super_block *sb, int xid, const __u16 *pfid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400438 int rc = 0, tmprc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 struct cifsTconInfo *pTcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 char *buf = NULL;
Steve French5ade9de2008-05-02 20:56:23 +0000442 bool adjustTZ = false;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400443 struct cifs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 pTcon = cifs_sb->tcon;
Steve French646dd532008-05-15 01:50:56 +0000446 cFYI(1, ("Getting info on %s", full_path));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700448 if ((pfindData == NULL) && (*pinode != NULL)) {
449 if (CIFS_I(*pinode)->clientCanCacheRead) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000450 cFYI(1, ("No need to revalidate cached inode sizes"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return rc;
452 }
453 }
454
455 /* if file info not passed in then get it from server */
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700456 if (pfindData == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700458 if (buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return -ENOMEM;
460 pfindData = (FILE_ALL_INFO *)buf;
Igor Mammedov79626702008-03-09 03:44:18 +0000461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 /* could do find first instead but this returns more info */
Igor Mammedov79626702008-03-09 03:44:18 +0000463 rc = CIFSSMBQPathInfo(xid, pTcon, full_path, pfindData,
Steve Frenchacf1a1b2006-10-12 03:28:28 +0000464 0 /* not legacy */,
Steve French6b8edfe2005-08-23 20:26:03 -0700465 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
Steve French737b7582005-04-28 22:41:06 -0700466 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve French6b8edfe2005-08-23 20:26:03 -0700467 /* BB optimize code so we do not make the above call
468 when server claims no NT SMB support and the above call
469 failed at least once - set flag in tcon or mount */
Steve French4523cc32007-04-30 20:13:06 +0000470 if ((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
Igor Mammedov79626702008-03-09 03:44:18 +0000471 rc = SMBQueryInformation(xid, pTcon, full_path,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000472 pfindData, cifs_sb->local_nls,
Steve French6b8edfe2005-08-23 20:26:03 -0700473 cifs_sb->mnt_cifs_flags &
474 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve French4b18f2a2008-04-29 00:06:05 +0000475 adjustTZ = true;
Steve French6b8edfe2005-08-23 20:26:03 -0700476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400478
479 if (!rc) {
480 cifs_all_info_to_fattr(&fattr, (FILE_ALL_INFO *) pfindData,
481 cifs_sb, adjustTZ);
482 } else if (rc == -EREMOTE) {
483 cifs_create_dfs_fattr(&fattr, sb);
Steve Frenchb9a32602008-05-20 21:52:32 +0000484 rc = 0;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400485 } else {
Igor Mammedov79626702008-03-09 03:44:18 +0000486 goto cgii_exit;
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400489 /*
490 * If an inode wasn't passed in, then get the inode number
491 *
492 * Is an i_ino of zero legal? Can we use that to check if the server
493 * supports returning inode numbers? Are there other sanity checks we
494 * can use to ensure that the server is really filling in that field?
495 *
496 * We can not use the IndexNumber field by default from Windows or
497 * Samba (in ALL_INFO buf) but we can request it explicitly. The SNIA
498 * CIFS spec claims that this value is unique within the scope of a
499 * share, and the windows docs hint that it's actually unique
500 * per-machine.
501 *
502 * There may be higher info levels that work but are there Windows
503 * server or network appliances for which IndexNumber field is not
504 * guaranteed unique?
505 */
Steve Frenchb9a32602008-05-20 21:52:32 +0000506 if (*pinode == NULL) {
Steve Frenchb9a32602008-05-20 21:52:32 +0000507 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
508 int rc1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Steve Frenchb9a32602008-05-20 21:52:32 +0000510 rc1 = CIFSGetSrvInodeNumber(xid, pTcon,
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400511 full_path, &fattr.cf_uniqueid,
Steve French737b7582005-04-28 22:41:06 -0700512 cifs_sb->local_nls,
513 cifs_sb->mnt_cifs_flags &
514 CIFS_MOUNT_MAP_SPECIAL_CHR);
Jeff Laytonec06aed2009-11-06 14:18:29 -0500515 if (rc1 || !fattr.cf_uniqueid) {
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400516 cFYI(1, ("GetSrvInodeNum rc %d", rc1));
517 fattr.cf_uniqueid = iunique(sb, ROOT_I);
Jeff Laytonec06aed2009-11-06 14:18:29 -0500518 cifs_autodisable_serverino(cifs_sb);
Jeff Layton132ac7b2009-02-10 07:33:57 -0500519 }
Jeff Layton132ac7b2009-02-10 07:33:57 -0500520 } else {
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400521 fattr.cf_uniqueid = iunique(sb, ROOT_I);
Jeff Layton132ac7b2009-02-10 07:33:57 -0500522 }
Steve Frenchb9a32602008-05-20 21:52:32 +0000523 } else {
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400524 fattr.cf_uniqueid = CIFS_I(*pinode)->uniqueid;
Steve Frenchb9a32602008-05-20 21:52:32 +0000525 }
526
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400527 /* query for SFU type info if supported and needed */
528 if (fattr.cf_cifsattrs & ATTR_SYSTEM &&
529 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
530 tmprc = cifs_sfu_type(&fattr, full_path, cifs_sb, xid);
531 if (tmprc)
532 cFYI(1, ("cifs_sfu_type failed: %d", tmprc));
Steve Frenchb9a32602008-05-20 21:52:32 +0000533 }
Steve Frenchb9a32602008-05-20 21:52:32 +0000534
Steve Frenchb9a32602008-05-20 21:52:32 +0000535#ifdef CONFIG_CIFS_EXPERIMENTAL
536 /* fill in 0777 bits from ACL */
537 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
538 cFYI(1, ("Getting mode bits from ACL"));
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400539 cifs_acl_to_fattr(cifs_sb, &fattr, *pinode, full_path, pfid);
Steve Frenchb9a32602008-05-20 21:52:32 +0000540 }
541#endif
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400542
543 /* fill in remaining high mode bits e.g. SUID, VTX */
544 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)
545 cifs_sfu_mode(&fattr, full_path, cifs_sb, xid);
546
547 if (!*pinode) {
548 *pinode = cifs_iget(sb, &fattr);
549 if (!*pinode)
550 rc = -ENOMEM;
551 } else {
552 cifs_fattr_to_inode(*pinode, &fattr);
Steve Frenchb9a32602008-05-20 21:52:32 +0000553 }
554
Igor Mammedov79626702008-03-09 03:44:18 +0000555cgii_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 kfree(buf);
557 return rc;
558}
559
Steve French7f8ed422007-09-28 22:28:55 +0000560static const struct inode_operations cifs_ipc_inode_ops = {
561 .lookup = cifs_lookup,
562};
563
Igor Mammedove4cce942009-02-10 14:10:26 +0300564char *cifs_build_path_to_root(struct cifs_sb_info *cifs_sb)
Steve French8be0ed42008-12-05 19:14:12 +0000565{
566 int pplen = cifs_sb->prepathlen;
567 int dfsplen;
568 char *full_path = NULL;
569
570 /* if no prefix path, simply set path to the root of share to "" */
571 if (pplen == 0) {
572 full_path = kmalloc(1, GFP_KERNEL);
573 if (full_path)
574 full_path[0] = 0;
575 return full_path;
576 }
577
578 if (cifs_sb->tcon && (cifs_sb->tcon->Flags & SMB_SHARE_IS_IN_DFS))
579 dfsplen = strnlen(cifs_sb->tcon->treeName, MAX_TREE_SIZE + 1);
580 else
581 dfsplen = 0;
582
583 full_path = kmalloc(dfsplen + pplen + 1, GFP_KERNEL);
584 if (full_path == NULL)
585 return full_path;
586
587 if (dfsplen) {
588 strncpy(full_path, cifs_sb->tcon->treeName, dfsplen);
589 /* switch slash direction in prepath depending on whether
590 * windows or posix style path names
591 */
592 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) {
593 int i;
594 for (i = 0; i < dfsplen; i++) {
595 if (full_path[i] == '\\')
596 full_path[i] = '/';
597 }
598 }
599 }
600 strncpy(full_path + dfsplen, cifs_sb->prepath, pplen);
601 full_path[dfsplen + pplen] = 0; /* add trailing null */
602 return full_path;
603}
604
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400605static int
606cifs_find_inode(struct inode *inode, void *opaque)
607{
608 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
609
610 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
611 return 0;
612
613 return 1;
614}
615
616static int
617cifs_init_inode(struct inode *inode, void *opaque)
618{
619 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
620
621 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
622 return 0;
623}
624
625/* Given fattrs, get a corresponding inode */
626struct inode *
627cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
628{
629 unsigned long hash;
630 struct inode *inode;
631
632 cFYI(1, ("looking for uniqueid=%llu", fattr->cf_uniqueid));
633
634 /* hash down to 32-bits on 32-bit arch */
635 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
636
637 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
638
639 /* we have fattrs in hand, update the inode */
640 if (inode) {
641 cifs_fattr_to_inode(inode, fattr);
642 if (sb->s_flags & MS_NOATIME)
643 inode->i_flags |= S_NOATIME | S_NOCMTIME;
644 if (inode->i_state & I_NEW) {
645 inode->i_ino = hash;
646 unlock_new_inode(inode);
647 }
648 }
649
650 return inode;
651}
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653/* gets root inode */
Jeff Laytonbd433d42009-05-27 09:37:34 -0400654struct inode *cifs_root_iget(struct super_block *sb, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
David Howellsce634ab2008-02-07 00:15:33 -0800656 int xid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 struct cifs_sb_info *cifs_sb;
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400658 struct inode *inode = NULL;
David Howellsce634ab2008-02-07 00:15:33 -0800659 long rc;
Steve French8be0ed42008-12-05 19:14:12 +0000660 char *full_path;
David Howellsce634ab2008-02-07 00:15:33 -0800661
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400662 cifs_sb = CIFS_SB(sb);
Igor Mammedove4cce942009-02-10 14:10:26 +0300663 full_path = cifs_build_path_to_root(cifs_sb);
Steve French8be0ed42008-12-05 19:14:12 +0000664 if (full_path == NULL)
665 return ERR_PTR(-ENOMEM);
Steve Frenchc18c8422007-07-18 23:21:09 +0000666
Steve French8be0ed42008-12-05 19:14:12 +0000667 xid = GetXid();
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400668 if (cifs_sb->tcon->unix_ext)
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400669 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400670 else
671 rc = cifs_get_inode_info(&inode, full_path, NULL, sb,
Steve French8be0ed42008-12-05 19:14:12 +0000672 xid, NULL);
Jeff Layton0b8f18e2009-07-09 01:46:37 -0400673
674 if (!inode)
675 return ERR_PTR(-ENOMEM);
Jeff Laytoncc0bad72009-06-25 00:56:52 -0400676
Steve French7f8ed422007-09-28 22:28:55 +0000677 if (rc && cifs_sb->tcon->ipc) {
678 cFYI(1, ("ipc connection - fake read inode"));
679 inode->i_mode |= S_IFDIR;
680 inode->i_nlink = 2;
681 inode->i_op = &cifs_ipc_inode_ops;
682 inode->i_fop = &simple_dir_operations;
683 inode->i_uid = cifs_sb->mnt_uid;
684 inode->i_gid = cifs_sb->mnt_gid;
Steve Frenchad661332008-08-12 14:14:40 +0000685 } else if (rc) {
Steve French8be0ed42008-12-05 19:14:12 +0000686 kfree(full_path);
David Howellsce634ab2008-02-07 00:15:33 -0800687 _FreeXid(xid);
688 iget_failed(inode);
689 return ERR_PTR(rc);
Steve French7f8ed422007-09-28 22:28:55 +0000690 }
691
David Howellsce634ab2008-02-07 00:15:33 -0800692
Steve French8be0ed42008-12-05 19:14:12 +0000693 kfree(full_path);
David Howellsce634ab2008-02-07 00:15:33 -0800694 /* can not call macro FreeXid here since in a void func
695 * TODO: This is no longer true
696 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 _FreeXid(xid);
David Howellsce634ab2008-02-07 00:15:33 -0800698 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Steve French388e57b2008-09-16 23:50:58 +0000701static int
702cifs_set_file_info(struct inode *inode, struct iattr *attrs, int xid,
703 char *full_path, __u32 dosattr)
704{
705 int rc;
706 int oplock = 0;
707 __u16 netfid;
708 __u32 netpid;
709 bool set_time = false;
710 struct cifsFileInfo *open_file;
711 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
712 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
713 struct cifsTconInfo *pTcon = cifs_sb->tcon;
714 FILE_BASIC_INFO info_buf;
715
Steve French1adcb712009-02-25 14:19:56 +0000716 if (attrs == NULL)
717 return -EINVAL;
718
Steve French388e57b2008-09-16 23:50:58 +0000719 if (attrs->ia_valid & ATTR_ATIME) {
720 set_time = true;
721 info_buf.LastAccessTime =
722 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
723 } else
724 info_buf.LastAccessTime = 0;
725
726 if (attrs->ia_valid & ATTR_MTIME) {
727 set_time = true;
728 info_buf.LastWriteTime =
729 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
730 } else
731 info_buf.LastWriteTime = 0;
732
733 /*
734 * Samba throws this field away, but windows may actually use it.
735 * Do not set ctime unless other time stamps are changed explicitly
736 * (i.e. by utimes()) since we would then have a mix of client and
737 * server times.
738 */
739 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
740 cFYI(1, ("CIFS - CTIME changed"));
741 info_buf.ChangeTime =
742 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
743 } else
744 info_buf.ChangeTime = 0;
745
746 info_buf.CreationTime = 0; /* don't change */
747 info_buf.Attributes = cpu_to_le32(dosattr);
748
749 /*
750 * If the file is already open for write, just use that fileid
751 */
752 open_file = find_writable_file(cifsInode);
753 if (open_file) {
754 netfid = open_file->netfid;
755 netpid = open_file->pid;
756 goto set_via_filehandle;
757 }
758
759 /*
760 * NT4 apparently returns success on this call, but it doesn't
761 * really work.
762 */
763 if (!(pTcon->ses->flags & CIFS_SES_NT4)) {
764 rc = CIFSSMBSetPathInfo(xid, pTcon, full_path,
765 &info_buf, cifs_sb->local_nls,
766 cifs_sb->mnt_cifs_flags &
767 CIFS_MOUNT_MAP_SPECIAL_CHR);
Jeff Layton6b37faa2008-10-06 21:54:41 +0000768 if (rc == 0) {
769 cifsInode->cifsAttrs = dosattr;
770 goto out;
771 } else if (rc != -EOPNOTSUPP && rc != -EINVAL)
Steve French388e57b2008-09-16 23:50:58 +0000772 goto out;
773 }
774
775 cFYI(1, ("calling SetFileInfo since SetPathInfo for "
776 "times not supported by this server"));
777 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN,
778 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
779 CREATE_NOT_DIR, &netfid, &oplock,
780 NULL, cifs_sb->local_nls,
781 cifs_sb->mnt_cifs_flags &
782 CIFS_MOUNT_MAP_SPECIAL_CHR);
783
784 if (rc != 0) {
785 if (rc == -EIO)
786 rc = -EINVAL;
787 goto out;
788 }
789
790 netpid = current->tgid;
791
792set_via_filehandle:
793 rc = CIFSSMBSetFileInfo(xid, pTcon, &info_buf, netfid, netpid);
Steve Frenchd3889082008-09-24 19:22:52 +0000794 if (!rc)
795 cifsInode->cifsAttrs = dosattr;
796
Steve French388e57b2008-09-16 23:50:58 +0000797 if (open_file == NULL)
798 CIFSSMBClose(xid, pTcon, netfid);
799 else
Dave Kleikamp6ab409b2009-08-31 11:07:12 -0400800 cifsFileInfo_put(open_file);
Steve French388e57b2008-09-16 23:50:58 +0000801out:
802 return rc;
803}
804
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400805/*
806 * open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
807 * and rename it to a random name that hopefully won't conflict with
808 * anything else.
809 */
810static int
Steve French32709582008-10-20 00:44:19 +0000811cifs_rename_pending_delete(char *full_path, struct dentry *dentry, int xid)
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400812{
813 int oplock = 0;
814 int rc;
815 __u16 netfid;
Steve French32709582008-10-20 00:44:19 +0000816 struct inode *inode = dentry->d_inode;
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400817 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
818 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
819 struct cifsTconInfo *tcon = cifs_sb->tcon;
Steve French32709582008-10-20 00:44:19 +0000820 __u32 dosattr, origattr;
821 FILE_BASIC_INFO *info_buf = NULL;
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400822
823 rc = CIFSSMBOpen(xid, tcon, full_path, FILE_OPEN,
Jeff Laytondd1db2d2008-10-16 19:27:12 -0400824 DELETE|FILE_WRITE_ATTRIBUTES, CREATE_NOT_DIR,
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400825 &netfid, &oplock, NULL, cifs_sb->local_nls,
826 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
827 if (rc != 0)
828 goto out;
829
Steve French32709582008-10-20 00:44:19 +0000830 origattr = cifsInode->cifsAttrs;
831 if (origattr == 0)
832 origattr |= ATTR_NORMAL;
833
834 dosattr = origattr & ~ATTR_READONLY;
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400835 if (dosattr == 0)
836 dosattr |= ATTR_NORMAL;
837 dosattr |= ATTR_HIDDEN;
838
Steve French32709582008-10-20 00:44:19 +0000839 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
840 if (dosattr != origattr) {
841 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
842 if (info_buf == NULL) {
843 rc = -ENOMEM;
844 goto out_close;
845 }
846 info_buf->Attributes = cpu_to_le32(dosattr);
847 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, netfid,
848 current->tgid);
849 /* although we would like to mark the file hidden
850 if that fails we will still try to rename it */
Steve French41346092008-10-20 18:24:42 +0000851 if (rc != 0)
Steve French32709582008-10-20 00:44:19 +0000852 cifsInode->cifsAttrs = dosattr;
853 else
854 dosattr = origattr; /* since not able to change them */
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400855 }
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400856
Jeff Laytondd1db2d2008-10-16 19:27:12 -0400857 /* rename the file */
858 rc = CIFSSMBRenameOpenFile(xid, tcon, netfid, NULL, cifs_sb->local_nls,
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400859 cifs_sb->mnt_cifs_flags &
860 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve French32709582008-10-20 00:44:19 +0000861 if (rc != 0) {
862 rc = -ETXTBSY;
863 goto undo_setattr;
864 }
Jeff Layton6d22f092008-09-23 11:48:35 -0400865
Steve French32709582008-10-20 00:44:19 +0000866 /* try to set DELETE_ON_CLOSE */
867 if (!cifsInode->delete_pending) {
868 rc = CIFSSMBSetFileDisposition(xid, tcon, true, netfid,
869 current->tgid);
870 /*
871 * some samba versions return -ENOENT when we try to set the
872 * file disposition here. Likely a samba bug, but work around
873 * it for now. This means that some cifsXXX files may hang
874 * around after they shouldn't.
875 *
876 * BB: remove this hack after more servers have the fix
877 */
878 if (rc == -ENOENT)
879 rc = 0;
880 else if (rc != 0) {
881 rc = -ETXTBSY;
882 goto undo_rename;
883 }
884 cifsInode->delete_pending = true;
885 }
Jeff Layton7ce86d52008-09-24 11:32:59 -0400886
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400887out_close:
888 CIFSSMBClose(xid, tcon, netfid);
889out:
Steve French32709582008-10-20 00:44:19 +0000890 kfree(info_buf);
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400891 return rc;
Steve French32709582008-10-20 00:44:19 +0000892
893 /*
894 * reset everything back to the original state. Don't bother
895 * dealing with errors here since we can't do anything about
896 * them anyway.
897 */
898undo_rename:
899 CIFSSMBRenameOpenFile(xid, tcon, netfid, dentry->d_name.name,
900 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
901 CIFS_MOUNT_MAP_SPECIAL_CHR);
902undo_setattr:
903 if (dosattr != origattr) {
904 info_buf->Attributes = cpu_to_le32(origattr);
905 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, netfid,
906 current->tgid))
907 cifsInode->cifsAttrs = origattr;
908 }
909
910 goto out_close;
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400911}
912
Steve Frenchff694522009-04-20 19:45:13 +0000913
914/*
915 * If dentry->d_inode is null (usually meaning the cached dentry
916 * is a negative dentry) then we would attempt a standard SMB delete, but
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200917 * if that fails we can not attempt the fall back mechanisms on EACCESS
918 * but will return the EACCESS to the caller. Note that the VFS does not call
Steve Frenchff694522009-04-20 19:45:13 +0000919 * unlink on negative dentries currently.
920 */
Jeff Layton5f0319a2008-09-16 14:05:16 -0400921int cifs_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
923 int rc = 0;
924 int xid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 char *full_path = NULL;
Jeff Layton5f0319a2008-09-16 14:05:16 -0400926 struct inode *inode = dentry->d_inode;
Steve Frenchff694522009-04-20 19:45:13 +0000927 struct cifsInodeInfo *cifs_inode;
Jeff Layton5f0319a2008-09-16 14:05:16 -0400928 struct super_block *sb = dir->i_sb;
929 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
930 struct cifsTconInfo *tcon = cifs_sb->tcon;
Steve French60502472008-10-07 18:42:52 +0000931 struct iattr *attrs = NULL;
932 __u32 dosattr = 0, origattr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Jeff Layton5f0319a2008-09-16 14:05:16 -0400934 cFYI(1, ("cifs_unlink, dir=0x%p, dentry=0x%p", dir, dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 xid = GetXid();
937
Jeff Layton5f0319a2008-09-16 14:05:16 -0400938 /* Unlink can be called from rename so we can not take the
939 * sb->s_vfs_rename_mutex here */
940 full_path = build_path_from_dentry(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (full_path == NULL) {
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +0530942 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 FreeXid(xid);
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +0530944 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
Steve French2d785a52007-07-15 01:48:57 +0000946
Jeff Layton5f0319a2008-09-16 14:05:16 -0400947 if ((tcon->ses->capabilities & CAP_UNIX) &&
Steve French2d785a52007-07-15 01:48:57 +0000948 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
Jeff Layton5f0319a2008-09-16 14:05:16 -0400949 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
950 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
Steve French2d785a52007-07-15 01:48:57 +0000951 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
952 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
953 cFYI(1, ("posix del rc %d", rc));
954 if ((rc == 0) || (rc == -ENOENT))
955 goto psx_del_no_retry;
956 }
957
Steve French60502472008-10-07 18:42:52 +0000958retry_std_delete:
Jeff Layton5f0319a2008-09-16 14:05:16 -0400959 rc = CIFSSMBDelFile(xid, tcon, full_path, cifs_sb->local_nls,
Steve French737b7582005-04-28 22:41:06 -0700960 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve French60502472008-10-07 18:42:52 +0000961
Steve French2d785a52007-07-15 01:48:57 +0000962psx_del_no_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (!rc) {
Jeff Layton5f0319a2008-09-16 14:05:16 -0400964 if (inode)
965 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 } else if (rc == -ENOENT) {
Jeff Layton5f0319a2008-09-16 14:05:16 -0400967 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 } else if (rc == -ETXTBSY) {
Steve French32709582008-10-20 00:44:19 +0000969 rc = cifs_rename_pending_delete(full_path, dentry, xid);
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400970 if (rc == 0)
971 drop_nlink(inode);
Steve Frenchff694522009-04-20 19:45:13 +0000972 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
Steve French388e57b2008-09-16 23:50:58 +0000973 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
974 if (attrs == NULL) {
975 rc = -ENOMEM;
976 goto out_reval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
Steve French388e57b2008-09-16 23:50:58 +0000978
979 /* try to reset dos attributes */
Steve Frenchff694522009-04-20 19:45:13 +0000980 cifs_inode = CIFS_I(inode);
981 origattr = cifs_inode->cifsAttrs;
Steve French60502472008-10-07 18:42:52 +0000982 if (origattr == 0)
983 origattr |= ATTR_NORMAL;
984 dosattr = origattr & ~ATTR_READONLY;
Steve French388e57b2008-09-16 23:50:58 +0000985 if (dosattr == 0)
986 dosattr |= ATTR_NORMAL;
987 dosattr |= ATTR_HIDDEN;
988
989 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
Steve French388e57b2008-09-16 23:50:58 +0000990 if (rc != 0)
991 goto out_reval;
Steve French60502472008-10-07 18:42:52 +0000992
993 goto retry_std_delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 }
Steve French60502472008-10-07 18:42:52 +0000995
996 /* undo the setattr if we errored out and it's needed */
997 if (rc != 0 && dosattr != 0)
998 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
999
Steve French388e57b2008-09-16 23:50:58 +00001000out_reval:
Jeff Layton5f0319a2008-09-16 14:05:16 -04001001 if (inode) {
Steve Frenchff694522009-04-20 19:45:13 +00001002 cifs_inode = CIFS_I(inode);
1003 cifs_inode->time = 0; /* will force revalidate to get info
Steve Frenchb2aeb9d2005-05-17 13:16:18 -05001004 when needed */
Jeff Layton5f0319a2008-09-16 14:05:16 -04001005 inode->i_ctime = current_fs_time(sb);
Steve Frenchb2aeb9d2005-05-17 13:16:18 -05001006 }
Jeff Layton5f0319a2008-09-16 14:05:16 -04001007 dir->i_ctime = dir->i_mtime = current_fs_time(sb);
Steve Frenchff694522009-04-20 19:45:13 +00001008 cifs_inode = CIFS_I(dir);
Steve French60502472008-10-07 18:42:52 +00001009 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 kfree(full_path);
Steve French60502472008-10-07 18:42:52 +00001012 kfree(attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 FreeXid(xid);
1014 return rc;
1015}
1016
1017int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode)
1018{
Jeff Layton6b37faa2008-10-06 21:54:41 +00001019 int rc = 0, tmprc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 int xid;
1021 struct cifs_sb_info *cifs_sb;
1022 struct cifsTconInfo *pTcon;
1023 char *full_path = NULL;
1024 struct inode *newinode = NULL;
Jeff Laytoncc0bad72009-06-25 00:56:52 -04001025 struct cifs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Steve French6473a552005-11-29 20:20:10 -08001027 cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p", mode, inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 xid = GetXid();
1030
1031 cifs_sb = CIFS_SB(inode->i_sb);
1032 pTcon = cifs_sb->tcon;
1033
Steve French7f573562005-08-30 11:32:14 -07001034 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (full_path == NULL) {
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05301036 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 FreeXid(xid);
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05301038 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 }
Steve French50c2f752007-07-13 00:33:32 +00001040
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001041 if ((pTcon->ses->capabilities & CAP_UNIX) &&
1042 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
Steve French2dd29d32007-04-23 22:07:35 +00001043 le64_to_cpu(pTcon->fsUnixInfo.Capability))) {
1044 u32 oplock = 0;
Steve Frenchf6d09982008-01-08 23:18:22 +00001045 FILE_UNIX_BASIC_INFO *pInfo =
Steve French2dd29d32007-04-23 22:07:35 +00001046 kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001047 if (pInfo == NULL) {
Steve French2dd29d32007-04-23 22:07:35 +00001048 rc = -ENOMEM;
1049 goto mkdir_out;
1050 }
Steve French50c2f752007-07-13 00:33:32 +00001051
Al Viroce3b0f82009-03-29 19:08:22 -04001052 mode &= ~current_umask();
Steve French2dd29d32007-04-23 22:07:35 +00001053 rc = CIFSPOSIXCreate(xid, pTcon, SMB_O_DIRECTORY | SMB_O_CREAT,
1054 mode, NULL /* netfid */, pInfo, &oplock,
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001055 full_path, cifs_sb->local_nls,
1056 cifs_sb->mnt_cifs_flags &
Steve French2dd29d32007-04-23 22:07:35 +00001057 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve Frenchc45d7072007-09-17 02:04:21 +00001058 if (rc == -EOPNOTSUPP) {
1059 kfree(pInfo);
1060 goto mkdir_retry_old;
1061 } else if (rc) {
Steve French2dd29d32007-04-23 22:07:35 +00001062 cFYI(1, ("posix mkdir returned 0x%x", rc));
1063 d_drop(direntry);
1064 } else {
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001065 if (pInfo->Type == cpu_to_le32(-1)) {
1066 /* no return info, go query for it */
Steve French5a07cdf2007-09-16 23:12:47 +00001067 kfree(pInfo);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001068 goto mkdir_get_info;
Steve French5a07cdf2007-09-16 23:12:47 +00001069 }
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001070/*BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if need
1071 to set uid/gid */
Steve French2dd29d32007-04-23 22:07:35 +00001072 inc_nlink(inode);
1073 if (pTcon->nocase)
1074 direntry->d_op = &cifs_ci_dentry_ops;
1075 else
1076 direntry->d_op = &cifs_dentry_ops;
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001077
Jeff Laytoncc0bad72009-06-25 00:56:52 -04001078 cifs_unix_basic_to_fattr(&fattr, pInfo, cifs_sb);
1079 newinode = cifs_iget(inode->i_sb, &fattr);
1080 if (!newinode) {
Steve French5a07cdf2007-09-16 23:12:47 +00001081 kfree(pInfo);
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001082 goto mkdir_get_info;
Steve French5a07cdf2007-09-16 23:12:47 +00001083 }
Jeff Layton6b37faa2008-10-06 21:54:41 +00001084
Steve French2dd29d32007-04-23 22:07:35 +00001085 d_instantiate(direntry, newinode);
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001086
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001087#ifdef CONFIG_CIFS_DEBUG2
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001088 cFYI(1, ("instantiated dentry %p %s to inode %p",
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001089 direntry, direntry->d_name.name, newinode));
1090
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001091 if (newinode->i_nlink != 2)
1092 cFYI(1, ("unexpected number of links %d",
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001093 newinode->i_nlink));
1094#endif
Steve French2dd29d32007-04-23 22:07:35 +00001095 }
1096 kfree(pInfo);
1097 goto mkdir_out;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001098 }
Steve Frenchc45d7072007-09-17 02:04:21 +00001099mkdir_retry_old:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 /* BB add setting the equivalent of mode via CreateX w/ACLs */
Steve French737b7582005-04-28 22:41:06 -07001101 rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls,
1102 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (rc) {
Steve French26a21b92006-05-31 18:05:34 +00001104 cFYI(1, ("cifs_mkdir returned 0x%x", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 d_drop(direntry);
1106 } else {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001107mkdir_get_info:
Dave Hansend8c76e62006-09-30 23:29:04 -07001108 inc_nlink(inode);
Steve Frenchc18c8422007-07-18 23:21:09 +00001109 if (pTcon->unix_ext)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 rc = cifs_get_inode_info_unix(&newinode, full_path,
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001111 inode->i_sb, xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 else
1113 rc = cifs_get_inode_info(&newinode, full_path, NULL,
Steve French8b1327f2008-03-14 22:37:16 +00001114 inode->i_sb, xid, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Steve Frenchb92327f2005-08-22 20:09:43 -07001116 if (pTcon->nocase)
1117 direntry->d_op = &cifs_ci_dentry_ops;
1118 else
1119 direntry->d_op = &cifs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 d_instantiate(direntry, newinode);
Steve French2dd29d32007-04-23 22:07:35 +00001121 /* setting nlink not necessary except in cases where we
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001122 * failed to get it from the server or was set bogus */
Steve French2dd29d32007-04-23 22:07:35 +00001123 if ((direntry->d_inode) && (direntry->d_inode->i_nlink < 2))
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001124 direntry->d_inode->i_nlink = 2;
Jeff Layton95089912008-08-06 04:39:02 +00001125
Al Viroce3b0f82009-03-29 19:08:22 -04001126 mode &= ~current_umask();
Jeff Layton95089912008-08-06 04:39:02 +00001127 /* must turn on setgid bit if parent dir has it */
1128 if (inode->i_mode & S_ISGID)
1129 mode |= S_ISGID;
1130
Steve Frenchc18c8422007-07-18 23:21:09 +00001131 if (pTcon->unix_ext) {
Jeff Layton4e1e7fb2008-08-02 07:26:12 -04001132 struct cifs_unix_set_info_args args = {
1133 .mode = mode,
1134 .ctime = NO_CHANGE_64,
1135 .atime = NO_CHANGE_64,
1136 .mtime = NO_CHANGE_64,
1137 .device = 0,
1138 };
Steve Frenchd0d2f2d2005-06-02 15:12:36 -07001139 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
David Howellsa001e5b2008-11-14 10:38:47 +11001140 args.uid = (__u64)current_fsuid();
Jeff Layton95089912008-08-06 04:39:02 +00001141 if (inode->i_mode & S_ISGID)
1142 args.gid = (__u64)inode->i_gid;
1143 else
David Howellsa001e5b2008-11-14 10:38:47 +11001144 args.gid = (__u64)current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 } else {
Jeff Layton4e1e7fb2008-08-02 07:26:12 -04001146 args.uid = NO_CHANGE_64;
1147 args.gid = NO_CHANGE_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
Jeff Layton01ea95e2009-07-09 20:02:49 -04001149 CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, &args,
1150 cifs_sb->local_nls,
1151 cifs_sb->mnt_cifs_flags &
1152 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve French3ce53fc2007-06-08 14:55:14 +00001153 } else {
Jeff Layton67750fb2008-05-09 22:28:02 +00001154 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1155 (mode & S_IWUGO) == 0) {
1156 FILE_BASIC_INFO pInfo;
Jeff Layton6b37faa2008-10-06 21:54:41 +00001157 struct cifsInodeInfo *cifsInode;
1158 u32 dosattrs;
1159
Jeff Layton67750fb2008-05-09 22:28:02 +00001160 memset(&pInfo, 0, sizeof(pInfo));
Jeff Layton6b37faa2008-10-06 21:54:41 +00001161 cifsInode = CIFS_I(newinode);
1162 dosattrs = cifsInode->cifsAttrs|ATTR_READONLY;
1163 pInfo.Attributes = cpu_to_le32(dosattrs);
1164 tmprc = CIFSSMBSetPathInfo(xid, pTcon,
1165 full_path, &pInfo,
1166 cifs_sb->local_nls,
Jeff Layton67750fb2008-05-09 22:28:02 +00001167 cifs_sb->mnt_cifs_flags &
1168 CIFS_MOUNT_MAP_SPECIAL_CHR);
Jeff Layton6b37faa2008-10-06 21:54:41 +00001169 if (tmprc == 0)
1170 cifsInode->cifsAttrs = dosattrs;
Jeff Layton67750fb2008-05-09 22:28:02 +00001171 }
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001172 if (direntry->d_inode) {
Jeff Laytonb0fd30d2008-05-22 09:33:34 -04001173 if (cifs_sb->mnt_cifs_flags &
1174 CIFS_MOUNT_DYNPERM)
1175 direntry->d_inode->i_mode =
1176 (mode | S_IFDIR);
Steve French4e94a102008-05-23 18:22:46 +00001177
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001178 if (cifs_sb->mnt_cifs_flags &
Steve French6473a552005-11-29 20:20:10 -08001179 CIFS_MOUNT_SET_UID) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001180 direntry->d_inode->i_uid =
David Howellsa001e5b2008-11-14 10:38:47 +11001181 current_fsuid();
Jeff Layton95089912008-08-06 04:39:02 +00001182 if (inode->i_mode & S_ISGID)
1183 direntry->d_inode->i_gid =
1184 inode->i_gid;
1185 else
1186 direntry->d_inode->i_gid =
David Howellsa001e5b2008-11-14 10:38:47 +11001187 current_fsgid();
Steve French6473a552005-11-29 20:20:10 -08001188 }
1189 }
Steve French2a138ebb2005-11-29 21:22:19 -08001190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001192mkdir_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 kfree(full_path);
1194 FreeXid(xid);
1195 return rc;
1196}
1197
1198int cifs_rmdir(struct inode *inode, struct dentry *direntry)
1199{
1200 int rc = 0;
1201 int xid;
1202 struct cifs_sb_info *cifs_sb;
1203 struct cifsTconInfo *pTcon;
1204 char *full_path = NULL;
1205 struct cifsInodeInfo *cifsInode;
1206
Steve French26a21b92006-05-31 18:05:34 +00001207 cFYI(1, ("cifs_rmdir, inode = 0x%p", inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 xid = GetXid();
1210
1211 cifs_sb = CIFS_SB(inode->i_sb);
1212 pTcon = cifs_sb->tcon;
1213
Steve French7f573562005-08-30 11:32:14 -07001214 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (full_path == NULL) {
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05301216 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 FreeXid(xid);
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05301218 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
1220
Steve French737b7582005-04-28 22:41:06 -07001221 rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls,
1222 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 if (!rc) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001225 drop_nlink(inode);
Steve French3677db12007-02-26 16:46:11 +00001226 spin_lock(&direntry->d_inode->i_lock);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001227 i_size_write(direntry->d_inode, 0);
Dave Hansence71ec32006-09-30 23:29:06 -07001228 clear_nlink(direntry->d_inode);
Steve French3677db12007-02-26 16:46:11 +00001229 spin_unlock(&direntry->d_inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231
1232 cifsInode = CIFS_I(direntry->d_inode);
1233 cifsInode->time = 0; /* force revalidate to go get info when
1234 needed */
Steve French42c24542009-01-13 22:03:55 +00001235
1236 cifsInode = CIFS_I(inode);
1237 cifsInode->time = 0; /* force revalidate to get parent dir info
1238 since cached search results now invalid */
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
1241 current_fs_time(inode->i_sb);
1242
1243 kfree(full_path);
1244 FreeXid(xid);
1245 return rc;
1246}
1247
Steve Frenchee2fd962008-09-23 18:23:33 +00001248static int
1249cifs_do_rename(int xid, struct dentry *from_dentry, const char *fromPath,
1250 struct dentry *to_dentry, const char *toPath)
1251{
1252 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
1253 struct cifsTconInfo *pTcon = cifs_sb->tcon;
1254 __u16 srcfid;
1255 int oplock, rc;
1256
1257 /* try path-based rename first */
1258 rc = CIFSSMBRename(xid, pTcon, fromPath, toPath, cifs_sb->local_nls,
1259 cifs_sb->mnt_cifs_flags &
1260 CIFS_MOUNT_MAP_SPECIAL_CHR);
1261
1262 /*
1263 * don't bother with rename by filehandle unless file is busy and
1264 * source Note that cross directory moves do not work with
1265 * rename by filehandle to various Windows servers.
1266 */
1267 if (rc == 0 || rc != -ETXTBSY)
1268 return rc;
1269
1270 /* open the file to be renamed -- we need DELETE perms */
1271 rc = CIFSSMBOpen(xid, pTcon, fromPath, FILE_OPEN, DELETE,
1272 CREATE_NOT_DIR, &srcfid, &oplock, NULL,
1273 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
1274 CIFS_MOUNT_MAP_SPECIAL_CHR);
1275
1276 if (rc == 0) {
1277 rc = CIFSSMBRenameOpenFile(xid, pTcon, srcfid,
1278 (const char *) to_dentry->d_name.name,
1279 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
1280 CIFS_MOUNT_MAP_SPECIAL_CHR);
1281
1282 CIFSSMBClose(xid, pTcon, srcfid);
1283 }
1284
1285 return rc;
1286}
1287
Jeff Layton14121bd2008-10-20 14:45:22 -04001288int cifs_rename(struct inode *source_dir, struct dentry *source_dentry,
1289 struct inode *target_dir, struct dentry *target_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
Steve Frenchee2fd962008-09-23 18:23:33 +00001291 char *fromName = NULL;
1292 char *toName = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 struct cifs_sb_info *cifs_sb_source;
1294 struct cifs_sb_info *cifs_sb_target;
Jeff Layton14121bd2008-10-20 14:45:22 -04001295 struct cifsTconInfo *tcon;
Steve Frenchee2fd962008-09-23 18:23:33 +00001296 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
1297 FILE_UNIX_BASIC_INFO *info_buf_target;
Jeff Layton8d281ef2008-10-22 13:57:01 -04001298 int xid, rc, tmprc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Jeff Layton14121bd2008-10-20 14:45:22 -04001300 cifs_sb_target = CIFS_SB(target_dir->i_sb);
1301 cifs_sb_source = CIFS_SB(source_dir->i_sb);
1302 tcon = cifs_sb_source->tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Steve Frenchee2fd962008-09-23 18:23:33 +00001304 xid = GetXid();
1305
1306 /*
1307 * BB: this might be allowed if same server, but different share.
1308 * Consider adding support for this
1309 */
Jeff Layton14121bd2008-10-20 14:45:22 -04001310 if (tcon != cifs_sb_target->tcon) {
Steve Frenchee2fd962008-09-23 18:23:33 +00001311 rc = -EXDEV;
1312 goto cifs_rename_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
1314
Steve Frenchee2fd962008-09-23 18:23:33 +00001315 /*
1316 * we already have the rename sem so we do not need to
1317 * grab it again here to protect the path integrity
1318 */
Jeff Layton14121bd2008-10-20 14:45:22 -04001319 fromName = build_path_from_dentry(source_dentry);
Steve Frenchee2fd962008-09-23 18:23:33 +00001320 if (fromName == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 rc = -ENOMEM;
1322 goto cifs_rename_exit;
1323 }
1324
Jeff Layton14121bd2008-10-20 14:45:22 -04001325 toName = build_path_from_dentry(target_dentry);
Steve Frenchee2fd962008-09-23 18:23:33 +00001326 if (toName == NULL) {
1327 rc = -ENOMEM;
1328 goto cifs_rename_exit;
1329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Jeff Layton14121bd2008-10-20 14:45:22 -04001331 rc = cifs_do_rename(xid, source_dentry, fromName,
1332 target_dentry, toName);
Steve Frenchee2fd962008-09-23 18:23:33 +00001333
Jeff Layton14121bd2008-10-20 14:45:22 -04001334 if (rc == -EEXIST && tcon->unix_ext) {
Steve Frenchee2fd962008-09-23 18:23:33 +00001335 /*
Jeff Layton14121bd2008-10-20 14:45:22 -04001336 * Are src and dst hardlinks of same inode? We can
1337 * only tell with unix extensions enabled
Steve Frenchee2fd962008-09-23 18:23:33 +00001338 */
Jeff Layton14121bd2008-10-20 14:45:22 -04001339 info_buf_source =
1340 kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO),
1341 GFP_KERNEL);
1342 if (info_buf_source == NULL) {
1343 rc = -ENOMEM;
1344 goto cifs_rename_exit;
1345 }
1346
1347 info_buf_target = info_buf_source + 1;
Jeff Layton8d281ef2008-10-22 13:57:01 -04001348 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, fromName,
Jeff Layton14121bd2008-10-20 14:45:22 -04001349 info_buf_source,
1350 cifs_sb_source->local_nls,
1351 cifs_sb_source->mnt_cifs_flags &
1352 CIFS_MOUNT_MAP_SPECIAL_CHR);
Jeff Layton8d281ef2008-10-22 13:57:01 -04001353 if (tmprc != 0)
Jeff Layton14121bd2008-10-20 14:45:22 -04001354 goto unlink_target;
1355
Jeff Layton8d281ef2008-10-22 13:57:01 -04001356 tmprc = CIFSSMBUnixQPathInfo(xid, tcon,
Jeff Layton14121bd2008-10-20 14:45:22 -04001357 toName, info_buf_target,
1358 cifs_sb_target->local_nls,
1359 /* remap based on source sb */
1360 cifs_sb_source->mnt_cifs_flags &
1361 CIFS_MOUNT_MAP_SPECIAL_CHR);
1362
Jeff Layton8d281ef2008-10-22 13:57:01 -04001363 if (tmprc == 0 && (info_buf_source->UniqueId ==
Jeff Laytonae6884a2008-11-03 14:05:08 -05001364 info_buf_target->UniqueId)) {
Jeff Layton14121bd2008-10-20 14:45:22 -04001365 /* same file, POSIX says that this is a noop */
Jeff Laytonae6884a2008-11-03 14:05:08 -05001366 rc = 0;
Jeff Layton14121bd2008-10-20 14:45:22 -04001367 goto cifs_rename_exit;
Jeff Laytonae6884a2008-11-03 14:05:08 -05001368 }
Jeff Layton14121bd2008-10-20 14:45:22 -04001369 } /* else ... BB we could add the same check for Windows by
1370 checking the UniqueId via FILE_INTERNAL_INFO */
1371
Jeff Layton14121bd2008-10-20 14:45:22 -04001372unlink_target:
Jeff Laytonfc6f3942009-04-17 11:45:30 -04001373 /* Try unlinking the target dentry if it's not negative */
1374 if (target_dentry->d_inode && (rc == -EACCES || rc == -EEXIST)) {
Jeff Layton8d281ef2008-10-22 13:57:01 -04001375 tmprc = cifs_unlink(target_dir, target_dentry);
Jeff Layton14121bd2008-10-20 14:45:22 -04001376 if (tmprc)
1377 goto cifs_rename_exit;
1378
Jeff Layton14121bd2008-10-20 14:45:22 -04001379 rc = cifs_do_rename(xid, source_dentry, fromName,
1380 target_dentry, toName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382
1383cifs_rename_exit:
Steve Frenchee2fd962008-09-23 18:23:33 +00001384 kfree(info_buf_source);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 kfree(fromName);
1386 kfree(toName);
1387 FreeXid(xid);
1388 return rc;
1389}
1390
1391int cifs_revalidate(struct dentry *direntry)
1392{
1393 int xid;
Jeff Laytoncea21802007-11-20 23:19:03 +00001394 int rc = 0, wbrc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 char *full_path;
1396 struct cifs_sb_info *cifs_sb;
1397 struct cifsInodeInfo *cifsInode;
1398 loff_t local_size;
1399 struct timespec local_mtime;
Steve French4b18f2a2008-04-29 00:06:05 +00001400 bool invalidate_inode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 if (direntry->d_inode == NULL)
1403 return -ENOENT;
1404
1405 cifsInode = CIFS_I(direntry->d_inode);
1406
1407 if (cifsInode == NULL)
1408 return -ENOENT;
1409
1410 /* no sense revalidating inode info on file that no one can write */
1411 if (CIFS_I(direntry->d_inode)->clientCanCacheRead)
1412 return rc;
1413
1414 xid = GetXid();
1415
1416 cifs_sb = CIFS_SB(direntry->d_sb);
1417
1418 /* can not safely grab the rename sem here if rename calls revalidate
1419 since that would deadlock */
Steve French7f573562005-08-30 11:32:14 -07001420 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 if (full_path == NULL) {
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05301422 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 FreeXid(xid);
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05301424 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
1426 cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld "
1427 "jiffies %ld", full_path, direntry->d_inode,
1428 direntry->d_inode->i_count.counter, direntry,
1429 direntry->d_time, jiffies));
1430
1431 if (cifsInode->time == 0) {
1432 /* was set to zero previously to force revalidate */
1433 } else if (time_before(jiffies, cifsInode->time + HZ) &&
1434 lookupCacheEnabled) {
1435 if ((S_ISREG(direntry->d_inode->i_mode) == 0) ||
1436 (direntry->d_inode->i_nlink == 1)) {
1437 kfree(full_path);
1438 FreeXid(xid);
1439 return rc;
1440 } else {
1441 cFYI(1, ("Have to revalidate file due to hardlinks"));
1442 }
1443 }
1444
1445 /* save mtime and size */
1446 local_mtime = direntry->d_inode->i_mtime;
1447 local_size = direntry->d_inode->i_size;
1448
Steve Frenchc18c8422007-07-18 23:21:09 +00001449 if (cifs_sb->tcon->unix_ext) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path,
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001451 direntry->d_sb, xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (rc) {
1453 cFYI(1, ("error on getting revalidate info %d", rc));
1454/* if (rc != -ENOENT)
1455 rc = 0; */ /* BB should we cache info on
1456 certain errors? */
1457 }
1458 } else {
1459 rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL,
Steve French8b1327f2008-03-14 22:37:16 +00001460 direntry->d_sb, xid, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (rc) {
1462 cFYI(1, ("error on getting revalidate info %d", rc));
1463/* if (rc != -ENOENT)
1464 rc = 0; */ /* BB should we cache info on
1465 certain errors? */
1466 }
1467 }
1468 /* should we remap certain errors, access denied?, to zero */
1469
1470 /* if not oplocked, we invalidate inode pages if mtime or file size
1471 had changed on server */
1472
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001473 if (timespec_equal(&local_mtime, &direntry->d_inode->i_mtime) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 (local_size == direntry->d_inode->i_size)) {
1475 cFYI(1, ("cifs_revalidate - inode unchanged"));
1476 } else {
1477 /* file may have changed on server */
1478 if (cifsInode->clientCanCacheRead) {
1479 /* no need to invalidate inode pages since we were the
1480 only ones who could have modified the file and the
1481 server copy is staler than ours */
1482 } else {
Steve French4b18f2a2008-04-29 00:06:05 +00001483 invalidate_inode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 }
1485 }
1486
1487 /* can not grab this sem since kernel filesys locking documentation
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001488 indicates i_mutex may be taken by the kernel on lookup and rename
1489 which could deadlock if we grab the i_mutex here as well */
1490/* mutex_lock(&direntry->d_inode->i_mutex);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 /* need to write out dirty pages here */
1492 if (direntry->d_inode->i_mapping) {
1493 /* do we need to lock inode until after invalidate completes
1494 below? */
Jeff Laytoncea21802007-11-20 23:19:03 +00001495 wbrc = filemap_fdatawrite(direntry->d_inode->i_mapping);
1496 if (wbrc)
1497 CIFS_I(direntry->d_inode)->write_behind_rc = wbrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 }
1499 if (invalidate_inode) {
Steve French3abb9272005-11-28 08:16:13 -08001500 /* shrink_dcache not necessary now that cifs dentry ops
1501 are exported for negative dentries */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001502/* if (S_ISDIR(direntry->d_inode->i_mode))
Steve French3abb9272005-11-28 08:16:13 -08001503 shrink_dcache_parent(direntry); */
1504 if (S_ISREG(direntry->d_inode->i_mode)) {
Suresh Jayaraman9e96af82008-08-05 14:38:40 +05301505 if (direntry->d_inode->i_mapping) {
Jeff Laytoncea21802007-11-20 23:19:03 +00001506 wbrc = filemap_fdatawait(direntry->d_inode->i_mapping);
1507 if (wbrc)
1508 CIFS_I(direntry->d_inode)->write_behind_rc = wbrc;
Suresh Jayaraman9e96af82008-08-05 14:38:40 +05301509 }
Steve French3abb9272005-11-28 08:16:13 -08001510 /* may eventually have to do this for open files too */
1511 if (list_empty(&(cifsInode->openFileList))) {
1512 /* changed on server - flush read ahead pages */
1513 cFYI(1, ("Invalidating read ahead data on "
1514 "closed file"));
1515 invalidate_remote_inode(direntry->d_inode);
1516 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
1518 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001519/* mutex_unlock(&direntry->d_inode->i_mutex); */
Steve French50c2f752007-07-13 00:33:32 +00001520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 kfree(full_path);
1522 FreeXid(xid);
1523 return rc;
1524}
1525
1526int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1527 struct kstat *stat)
1528{
1529 int err = cifs_revalidate(dentry);
Steve French5fe14c82006-11-07 19:26:33 +00001530 if (!err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 generic_fillattr(dentry->d_inode, stat);
Steve French5fe14c82006-11-07 19:26:33 +00001532 stat->blksize = CIFS_MAX_MSGSIZE;
Jeff Laytoncc0bad72009-06-25 00:56:52 -04001533 stat->ino = CIFS_I(dentry->d_inode)->uniqueid;
Steve French5fe14c82006-11-07 19:26:33 +00001534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return err;
1536}
1537
1538static int cifs_truncate_page(struct address_space *mapping, loff_t from)
1539{
1540 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1541 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
1542 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 int rc = 0;
1544
1545 page = grab_cache_page(mapping, index);
1546 if (!page)
1547 return -ENOMEM;
1548
Christoph Lametereebd2aa2008-02-04 22:28:29 -08001549 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 unlock_page(page);
1551 page_cache_release(page);
1552 return rc;
1553}
1554
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001555static int cifs_vmtruncate(struct inode *inode, loff_t offset)
Steve French3677db12007-02-26 16:46:11 +00001556{
npiggin@suse.dec08d3b02009-08-21 02:35:06 +10001557 loff_t oldsize;
1558 int err;
Steve French3677db12007-02-26 16:46:11 +00001559
Steve Frenchba6a46a2007-02-26 20:06:29 +00001560 spin_lock(&inode->i_lock);
npiggin@suse.dec08d3b02009-08-21 02:35:06 +10001561 err = inode_newsize_ok(inode, offset);
1562 if (err) {
Steve Frenchba6a46a2007-02-26 20:06:29 +00001563 spin_unlock(&inode->i_lock);
npiggin@suse.dec08d3b02009-08-21 02:35:06 +10001564 goto out;
Steve Frenchba6a46a2007-02-26 20:06:29 +00001565 }
Steve French3677db12007-02-26 16:46:11 +00001566
npiggin@suse.dec08d3b02009-08-21 02:35:06 +10001567 oldsize = inode->i_size;
Steve French3677db12007-02-26 16:46:11 +00001568 i_size_write(inode, offset);
Steve Frenchba6a46a2007-02-26 20:06:29 +00001569 spin_unlock(&inode->i_lock);
npiggin@suse.dec08d3b02009-08-21 02:35:06 +10001570 truncate_pagecache(inode, oldsize, offset);
Al Viroacfa4382008-12-04 10:06:33 -05001571 if (inode->i_op->truncate)
Steve French3677db12007-02-26 16:46:11 +00001572 inode->i_op->truncate(inode);
npiggin@suse.dec08d3b02009-08-21 02:35:06 +10001573out:
1574 return err;
Steve French3677db12007-02-26 16:46:11 +00001575}
1576
Jeff Layton8efdbde2008-07-23 21:28:12 +00001577static int
1578cifs_set_file_size(struct inode *inode, struct iattr *attrs,
1579 int xid, char *full_path)
1580{
1581 int rc;
1582 struct cifsFileInfo *open_file;
1583 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1584 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1585 struct cifsTconInfo *pTcon = cifs_sb->tcon;
1586
1587 /*
1588 * To avoid spurious oplock breaks from server, in the case of
1589 * inodes that we already have open, avoid doing path based
1590 * setting of file size if we can do it by handle.
1591 * This keeps our caching token (oplock) and avoids timeouts
1592 * when the local oplock break takes longer to flush
1593 * writebehind data than the SMB timeout for the SetPathInfo
1594 * request would allow
1595 */
1596 open_file = find_writable_file(cifsInode);
1597 if (open_file) {
1598 __u16 nfid = open_file->netfid;
1599 __u32 npid = open_file->pid;
1600 rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size, nfid,
1601 npid, false);
Dave Kleikamp6ab409b2009-08-31 11:07:12 -04001602 cifsFileInfo_put(open_file);
Jeff Layton8efdbde2008-07-23 21:28:12 +00001603 cFYI(1, ("SetFSize for attrs rc = %d", rc));
1604 if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
1605 unsigned int bytes_written;
1606 rc = CIFSSMBWrite(xid, pTcon, nfid, 0, attrs->ia_size,
1607 &bytes_written, NULL, NULL, 1);
1608 cFYI(1, ("Wrt seteof rc %d", rc));
1609 }
1610 } else
1611 rc = -EINVAL;
1612
1613 if (rc != 0) {
1614 /* Set file size by pathname rather than by handle
1615 either because no valid, writeable file handle for
1616 it was found or because there was an error setting
1617 it by handle */
1618 rc = CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size,
1619 false, cifs_sb->local_nls,
1620 cifs_sb->mnt_cifs_flags &
1621 CIFS_MOUNT_MAP_SPECIAL_CHR);
1622 cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc));
1623 if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
1624 __u16 netfid;
1625 int oplock = 0;
1626
1627 rc = SMBLegacyOpen(xid, pTcon, full_path,
1628 FILE_OPEN, GENERIC_WRITE,
1629 CREATE_NOT_DIR, &netfid, &oplock, NULL,
1630 cifs_sb->local_nls,
1631 cifs_sb->mnt_cifs_flags &
1632 CIFS_MOUNT_MAP_SPECIAL_CHR);
1633 if (rc == 0) {
1634 unsigned int bytes_written;
1635 rc = CIFSSMBWrite(xid, pTcon, netfid, 0,
1636 attrs->ia_size,
1637 &bytes_written, NULL,
1638 NULL, 1);
1639 cFYI(1, ("wrt seteof rc %d", rc));
1640 CIFSSMBClose(xid, pTcon, netfid);
1641 }
1642 }
1643 }
1644
1645 if (rc == 0) {
Jeff Laytonfbec9ab2009-04-03 13:44:00 -04001646 cifsInode->server_eof = attrs->ia_size;
Jeff Layton8efdbde2008-07-23 21:28:12 +00001647 rc = cifs_vmtruncate(inode, attrs->ia_size);
1648 cifs_truncate_page(inode->i_mapping, inode->i_size);
1649 }
1650
1651 return rc;
1652}
1653
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04001654static int
1655cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
1656{
1657 int rc;
1658 int xid;
1659 char *full_path = NULL;
1660 struct inode *inode = direntry->d_inode;
1661 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1662 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1663 struct cifsTconInfo *pTcon = cifs_sb->tcon;
1664 struct cifs_unix_set_info_args *args = NULL;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04001665 struct cifsFileInfo *open_file;
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04001666
1667 cFYI(1, ("setattr_unix on file %s attrs->ia_valid=0x%x",
1668 direntry->d_name.name, attrs->ia_valid));
1669
1670 xid = GetXid();
1671
1672 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) {
1673 /* check if we have permission to change attrs */
1674 rc = inode_change_ok(inode, attrs);
1675 if (rc < 0)
1676 goto out;
1677 else
1678 rc = 0;
1679 }
1680
1681 full_path = build_path_from_dentry(direntry);
1682 if (full_path == NULL) {
1683 rc = -ENOMEM;
1684 goto out;
1685 }
1686
Jeff Layton0f4d6342009-03-26 13:35:37 -04001687 /*
1688 * Attempt to flush data before changing attributes. We need to do
1689 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
1690 * ownership or mode then we may also need to do this. Here, we take
1691 * the safe way out and just do the flush on all setattr requests. If
1692 * the flush returns error, store it to report later and continue.
1693 *
1694 * BB: This should be smarter. Why bother flushing pages that
1695 * will be truncated anyway? Also, should we error out here if
1696 * the flush returns error?
1697 */
1698 rc = filemap_write_and_wait(inode->i_mapping);
1699 if (rc != 0) {
1700 cifsInode->write_behind_rc = rc;
1701 rc = 0;
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04001702 }
1703
1704 if (attrs->ia_valid & ATTR_SIZE) {
1705 rc = cifs_set_file_size(inode, attrs, xid, full_path);
1706 if (rc != 0)
1707 goto out;
1708 }
1709
1710 /* skip mode change if it's just for clearing setuid/setgid */
1711 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
1712 attrs->ia_valid &= ~ATTR_MODE;
1713
1714 args = kmalloc(sizeof(*args), GFP_KERNEL);
1715 if (args == NULL) {
1716 rc = -ENOMEM;
1717 goto out;
1718 }
1719
1720 /* set up the struct */
1721 if (attrs->ia_valid & ATTR_MODE)
1722 args->mode = attrs->ia_mode;
1723 else
1724 args->mode = NO_CHANGE_64;
1725
1726 if (attrs->ia_valid & ATTR_UID)
1727 args->uid = attrs->ia_uid;
1728 else
1729 args->uid = NO_CHANGE_64;
1730
1731 if (attrs->ia_valid & ATTR_GID)
1732 args->gid = attrs->ia_gid;
1733 else
1734 args->gid = NO_CHANGE_64;
1735
1736 if (attrs->ia_valid & ATTR_ATIME)
1737 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
1738 else
1739 args->atime = NO_CHANGE_64;
1740
1741 if (attrs->ia_valid & ATTR_MTIME)
1742 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
1743 else
1744 args->mtime = NO_CHANGE_64;
1745
1746 if (attrs->ia_valid & ATTR_CTIME)
1747 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
1748 else
1749 args->ctime = NO_CHANGE_64;
1750
1751 args->device = 0;
Jeff Layton3bbeeb32009-07-09 20:02:50 -04001752 open_file = find_writable_file(cifsInode);
1753 if (open_file) {
1754 u16 nfid = open_file->netfid;
1755 u32 npid = open_file->pid;
1756 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
Dave Kleikamp6ab409b2009-08-31 11:07:12 -04001757 cifsFileInfo_put(open_file);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04001758 } else {
1759 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
Jeff Layton01ea95e2009-07-09 20:02:49 -04001760 cifs_sb->local_nls,
1761 cifs_sb->mnt_cifs_flags &
1762 CIFS_MOUNT_MAP_SPECIAL_CHR);
Jeff Layton3bbeeb32009-07-09 20:02:50 -04001763 }
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04001764
Steve Frenchccd4bb12010-02-08 17:39:58 +00001765 if (!rc) {
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04001766 rc = inode_setattr(inode, attrs);
Steve Frenchccd4bb12010-02-08 17:39:58 +00001767
1768 /* force revalidate when any of these times are set since some
1769 of the fs types (eg ext3, fat) do not have fine enough
1770 time granularity to match protocol, and we do not have a
1771 a way (yet) to query the server fs's time granularity (and
1772 whether it rounds times down).
1773 */
1774 if (!rc && (attrs->ia_valid & (ATTR_MTIME | ATTR_CTIME)))
1775 cifsInode->time = 0;
1776 }
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04001777out:
1778 kfree(args);
1779 kfree(full_path);
1780 FreeXid(xid);
1781 return rc;
1782}
1783
Jeff Layton0510eeb2008-08-02 07:26:12 -04001784static int
1785cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
1787 int xid;
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04001788 struct inode *inode = direntry->d_inode;
1789 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04001790 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 char *full_path = NULL;
1792 int rc = -EACCES;
Jeff Laytonfeb3e202008-08-02 07:26:12 -04001793 __u32 dosattr = 0;
Jeff Layton4e1e7fb2008-08-02 07:26:12 -04001794 __u64 mode = NO_CHANGE_64;
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04001795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 xid = GetXid();
1797
Steve French39798772006-05-31 22:40:51 +00001798 cFYI(1, ("setattr on file %s attrs->iavalid 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 direntry->d_name.name, attrs->ia_valid));
Steve French6473a552005-11-29 20:20:10 -08001800
Steve French2a138ebb2005-11-29 21:22:19 -08001801 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) {
Steve French6473a552005-11-29 20:20:10 -08001802 /* check if we have permission to change attrs */
Jeff Layton02eadef2008-05-09 21:26:11 +00001803 rc = inode_change_ok(inode, attrs);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001804 if (rc < 0) {
Steve French6473a552005-11-29 20:20:10 -08001805 FreeXid(xid);
1806 return rc;
1807 } else
1808 rc = 0;
1809 }
Steve French50c2f752007-07-13 00:33:32 +00001810
Steve French7f573562005-08-30 11:32:14 -07001811 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 if (full_path == NULL) {
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05301813 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 FreeXid(xid);
Suresh Jayaraman0f3bc092009-06-25 18:12:34 +05301815 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Jeff Layton0f4d6342009-03-26 13:35:37 -04001818 /*
1819 * Attempt to flush data before changing attributes. We need to do
1820 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
1821 * ownership or mode then we may also need to do this. Here, we take
1822 * the safe way out and just do the flush on all setattr requests. If
1823 * the flush returns error, store it to report later and continue.
1824 *
1825 * BB: This should be smarter. Why bother flushing pages that
1826 * will be truncated anyway? Also, should we error out here if
1827 * the flush returns error?
1828 */
1829 rc = filemap_write_and_wait(inode->i_mapping);
1830 if (rc != 0) {
1831 cifsInode->write_behind_rc = rc;
1832 rc = 0;
Steve French50531442008-03-14 19:21:31 +00001833 }
Jeff Laytoncea21802007-11-20 23:19:03 +00001834
Steve French50531442008-03-14 19:21:31 +00001835 if (attrs->ia_valid & ATTR_SIZE) {
Jeff Layton8efdbde2008-07-23 21:28:12 +00001836 rc = cifs_set_file_size(inode, attrs, xid, full_path);
1837 if (rc != 0)
Steve Frenche30dcf32005-09-20 20:49:16 -07001838 goto cifs_setattr_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 }
Jeff Layton4ca691a2008-05-22 09:33:34 -04001840
1841 /*
1842 * Without unix extensions we can't send ownership changes to the
1843 * server, so silently ignore them. This is consistent with how
1844 * local DOS/Windows filesystems behave (VFAT, NTFS, etc). With
1845 * CIFSACL support + proper Windows to Unix idmapping, we may be
1846 * able to support this in the future.
1847 */
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04001848 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
Jeff Layton4ca691a2008-05-22 09:33:34 -04001849 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Jeff Laytond32c4f22007-10-18 03:05:22 -07001851 /* skip mode change if it's just for clearing setuid/setgid */
1852 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
1853 attrs->ia_valid &= ~ATTR_MODE;
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (attrs->ia_valid & ATTR_MODE) {
Jeff Layton51328612008-05-22 09:33:34 -04001856 cFYI(1, ("Mode changed to 0%o", attrs->ia_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 mode = attrs->ia_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
1859
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04001860 if (attrs->ia_valid & ATTR_MODE) {
Steve Frenchcdbce9c2005-11-19 21:04:52 -08001861 rc = 0;
Steve French97837582007-12-31 07:47:21 +00001862#ifdef CONFIG_CIFS_EXPERIMENTAL
1863 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
Jeff Layton02eadef2008-05-09 21:26:11 +00001864 rc = mode_to_acl(inode, full_path, mode);
Jeff Layton51328612008-05-22 09:33:34 -04001865 else
Steve French97837582007-12-31 07:47:21 +00001866#endif
Jeff Layton51328612008-05-22 09:33:34 -04001867 if (((mode & S_IWUGO) == 0) &&
1868 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
Jeff Laytonfeb3e202008-08-02 07:26:12 -04001869
1870 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
1871
Jeff Layton51328612008-05-22 09:33:34 -04001872 /* fix up mode if we're not using dynperm */
1873 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
1874 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
1875 } else if ((mode & S_IWUGO) &&
1876 (cifsInode->cifsAttrs & ATTR_READONLY)) {
Jeff Laytonfeb3e202008-08-02 07:26:12 -04001877
1878 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
1879 /* Attributes of 0 are ignored */
1880 if (dosattr == 0)
1881 dosattr |= ATTR_NORMAL;
Jeff Layton51328612008-05-22 09:33:34 -04001882
1883 /* reset local inode permissions to normal */
1884 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
1885 attrs->ia_mode &= ~(S_IALLUGO);
1886 if (S_ISDIR(inode->i_mode))
1887 attrs->ia_mode |=
1888 cifs_sb->mnt_dir_mode;
1889 else
1890 attrs->ia_mode |=
1891 cifs_sb->mnt_file_mode;
1892 }
1893 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
1894 /* ignore mode change - ATTR_READONLY hasn't changed */
1895 attrs->ia_valid &= ~ATTR_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 }
1898
Jeff Laytonfeb3e202008-08-02 07:26:12 -04001899 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
1900 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
1901 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
1902 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Steve Frenche30dcf32005-09-20 20:49:16 -07001904 /* Even if error on time set, no sense failing the call if
1905 the server would set the time to a reasonable value anyway,
1906 and this check ensures that we are not being called from
1907 sys_utimes in which case we ought to fail the call back to
1908 the user when the server rejects the call */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001909 if ((rc) && (attrs->ia_valid &
Jeff Laytonfeb3e202008-08-02 07:26:12 -04001910 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
Steve Frenche30dcf32005-09-20 20:49:16 -07001911 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
1913
1914 /* do not need local check to inode_check_ok since the server does
1915 that */
1916 if (!rc)
Jeff Layton02eadef2008-05-09 21:26:11 +00001917 rc = inode_setattr(inode, attrs);
Steve Frenche30dcf32005-09-20 20:49:16 -07001918cifs_setattr_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 kfree(full_path);
1920 FreeXid(xid);
1921 return rc;
1922}
1923
Jeff Layton0510eeb2008-08-02 07:26:12 -04001924int
1925cifs_setattr(struct dentry *direntry, struct iattr *attrs)
1926{
1927 struct inode *inode = direntry->d_inode;
1928 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1929 struct cifsTconInfo *pTcon = cifs_sb->tcon;
1930
1931 if (pTcon->unix_ext)
1932 return cifs_setattr_unix(direntry, attrs);
1933
1934 return cifs_setattr_nounix(direntry, attrs);
1935
1936 /* BB: add cifs_setattr_legacy for really old servers */
1937}
1938
Steve French99ee4db2007-02-27 05:35:17 +00001939#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940void cifs_delete_inode(struct inode *inode)
1941{
Steve French26a21b92006-05-31 18:05:34 +00001942 cFYI(1, ("In cifs_delete_inode, inode = 0x%p", inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 /* may have to add back in if and when safe distributed caching of
1944 directories added e.g. via FindNotify */
1945}
Steve French99ee4db2007-02-27 05:35:17 +00001946#endif