blob: fceebee39f2722d81f5b6c28567a3c8283af71fd [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
Christoph Hellwig75f12982008-02-25 20:25:21 +000080static void cifs_unix_info_to_inode(struct inode *inode,
81 FILE_UNIX_BASIC_INFO *info, int force_uid_gid)
82{
83 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
84 struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
85 __u64 num_of_bytes = le64_to_cpu(info->NumOfBytes);
86 __u64 end_of_file = le64_to_cpu(info->EndOfFile);
87
88 inode->i_atime = cifs_NTtimeToUnix(le64_to_cpu(info->LastAccessTime));
89 inode->i_mtime =
90 cifs_NTtimeToUnix(le64_to_cpu(info->LastModificationTime));
91 inode->i_ctime = cifs_NTtimeToUnix(le64_to_cpu(info->LastStatusChange));
92 inode->i_mode = le64_to_cpu(info->Permissions);
93
94 /*
95 * Since we set the inode type below we need to mask off
96 * to avoid strange results if bits set above.
97 */
98 inode->i_mode &= ~S_IFMT;
99 switch (le32_to_cpu(info->Type)) {
100 case UNIX_FILE:
101 inode->i_mode |= S_IFREG;
102 break;
103 case UNIX_SYMLINK:
104 inode->i_mode |= S_IFLNK;
105 break;
106 case UNIX_DIR:
107 inode->i_mode |= S_IFDIR;
108 break;
109 case UNIX_CHARDEV:
110 inode->i_mode |= S_IFCHR;
111 inode->i_rdev = MKDEV(le64_to_cpu(info->DevMajor),
112 le64_to_cpu(info->DevMinor) & MINORMASK);
113 break;
114 case UNIX_BLOCKDEV:
115 inode->i_mode |= S_IFBLK;
116 inode->i_rdev = MKDEV(le64_to_cpu(info->DevMajor),
117 le64_to_cpu(info->DevMinor) & MINORMASK);
118 break;
119 case UNIX_FIFO:
120 inode->i_mode |= S_IFIFO;
121 break;
122 case UNIX_SOCKET:
123 inode->i_mode |= S_IFSOCK;
124 break;
125 default:
126 /* safest to call it a file if we do not know */
127 inode->i_mode |= S_IFREG;
128 cFYI(1, ("unknown type %d", le32_to_cpu(info->Type)));
129 break;
130 }
131
132 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID) &&
133 !force_uid_gid)
134 inode->i_uid = cifs_sb->mnt_uid;
135 else
136 inode->i_uid = le64_to_cpu(info->Uid);
137
138 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID) &&
139 !force_uid_gid)
140 inode->i_gid = cifs_sb->mnt_gid;
141 else
142 inode->i_gid = le64_to_cpu(info->Gid);
143
144 inode->i_nlink = le64_to_cpu(info->Nlinks);
145
146 spin_lock(&inode->i_lock);
147 if (is_size_safe_to_change(cifsInfo, end_of_file)) {
148 /*
149 * We can not safely change the file size here if the client
150 * is writing to it due to potential races.
151 */
152 i_size_write(inode, end_of_file);
153
154 /*
155 * i_blocks is not related to (i_size / i_blksize),
156 * but instead 512 byte (2**9) size is required for
157 * calculating num blocks.
158 */
159 inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
160 }
161 spin_unlock(&inode->i_lock);
162}
163
Steve Frenchb9a32602008-05-20 21:52:32 +0000164
165/*
166 * Needed to setup inode data for the directory which is the
167 * junction to the new submount (ie to setup the fake directory
168 * which represents a DFS referral)
169 */
Steve French0e4bbde2008-05-20 19:50:46 +0000170static void fill_fake_finddataunix(FILE_UNIX_BASIC_INFO *pfnd_dat,
171 struct super_block *sb)
172{
173 struct inode *pinode = NULL;
174
Dave Jones0a891ad2008-05-22 14:20:21 +0000175 memset(pfnd_dat, 0, sizeof(FILE_UNIX_BASIC_INFO));
Steve French0e4bbde2008-05-20 19:50:46 +0000176
177/* __le64 pfnd_dat->EndOfFile = cpu_to_le64(0);
178 __le64 pfnd_dat->NumOfBytes = cpu_to_le64(0);
179 __u64 UniqueId = 0; */
180 pfnd_dat->LastStatusChange =
181 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
182 pfnd_dat->LastAccessTime =
183 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
184 pfnd_dat->LastModificationTime =
185 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
186 pfnd_dat->Type = cpu_to_le32(UNIX_DIR);
187 pfnd_dat->Permissions = cpu_to_le64(S_IXUGO | S_IRWXU);
188 pfnd_dat->Nlinks = cpu_to_le64(2);
189 if (sb->s_root)
190 pinode = sb->s_root->d_inode;
191 if (pinode == NULL)
192 return;
193
194 /* fill in default values for the remaining based on root
195 inode since we can not query the server for this inode info */
196 pfnd_dat->DevMajor = cpu_to_le64(MAJOR(pinode->i_rdev));
197 pfnd_dat->DevMinor = cpu_to_le64(MINOR(pinode->i_rdev));
198 pfnd_dat->Uid = cpu_to_le64(pinode->i_uid);
199 pfnd_dat->Gid = cpu_to_le64(pinode->i_gid);
200}
201
Jeff Layton132ac7b2009-02-10 07:33:57 -0500202/**
203 * cifs_new inode - create new inode, initialize, and hash it
204 * @sb - pointer to superblock
205 * @inum - if valid pointer and serverino is enabled, replace i_ino with val
206 *
207 * Create a new inode, initialize it for CIFS and hash it. Returns the new
208 * inode or NULL if one couldn't be allocated.
209 *
210 * If the share isn't mounted with "serverino" or inum is a NULL pointer then
211 * we'll just use the inode number assigned by new_inode(). Note that this can
212 * mean i_ino collisions since the i_ino assigned by new_inode is not
213 * guaranteed to be unique.
214 */
215struct inode *
Jeff Layton950ec522009-02-11 08:08:26 -0500216cifs_new_inode(struct super_block *sb, __u64 *inum)
Jeff Layton132ac7b2009-02-10 07:33:57 -0500217{
218 struct inode *inode;
219
220 inode = new_inode(sb);
221 if (inode == NULL)
222 return NULL;
223
224 /*
225 * BB: Is i_ino == 0 legal? Here, we assume that it is. If it isn't we
226 * stop passing inum as ptr. Are there sanity checks we can use to
227 * ensure that the server is really filling in that field? Also,
228 * if serverino is disabled, perhaps we should be using iunique()?
229 */
230 if (inum && (CIFS_SB(sb)->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM))
Jeff Layton950ec522009-02-11 08:08:26 -0500231 inode->i_ino = (unsigned long) *inum;
Jeff Layton132ac7b2009-02-10 07:33:57 -0500232
233 /*
234 * must set this here instead of cifs_alloc_inode since VFS will
235 * clobber i_flags
236 */
237 if (sb->s_flags & MS_NOATIME)
238 inode->i_flags |= S_NOATIME | S_NOCMTIME;
239
240 insert_inode_hash(inode);
241
242 return inode;
243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245int cifs_get_inode_info_unix(struct inode **pinode,
Steve French646dd532008-05-15 01:50:56 +0000246 const unsigned char *full_path, struct super_block *sb, int xid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 int rc = 0;
Steve French0e4bbde2008-05-20 19:50:46 +0000249 FILE_UNIX_BASIC_INFO find_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 struct cifsTconInfo *pTcon;
251 struct inode *inode;
252 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
Igor Mammedov79626702008-03-09 03:44:18 +0000253 bool is_dfs_referral = false;
Steve French0e4bbde2008-05-20 19:50:46 +0000254 struct cifsInodeInfo *cifsInfo;
255 __u64 num_of_bytes;
256 __u64 end_of_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 pTcon = cifs_sb->tcon;
Steve French646dd532008-05-15 01:50:56 +0000259 cFYI(1, ("Getting info on %s", full_path));
Igor Mammedov79626702008-03-09 03:44:18 +0000260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 /* could have done a find first instead but this returns more info */
Steve French0e4bbde2008-05-20 19:50:46 +0000262 rc = CIFSSMBUnixQPathInfo(xid, pTcon, full_path, &find_data,
Steve French737b7582005-04-28 22:41:06 -0700263 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
264 CIFS_MOUNT_MAP_SPECIAL_CHR);
Jeff Laytone911d0c2008-07-12 13:47:59 -0700265 if (rc == -EREMOTE && !is_dfs_referral) {
266 is_dfs_referral = true;
267 cFYI(DBG2, ("DFS ref"));
268 /* for DFS, server does not give us real inode data */
269 fill_fake_finddataunix(&find_data, sb);
270 rc = 0;
271 } else if (rc)
272 goto cgiiu_exit;
273
Steve French0e4bbde2008-05-20 19:50:46 +0000274 num_of_bytes = le64_to_cpu(find_data.NumOfBytes);
275 end_of_file = le64_to_cpu(find_data.EndOfFile);
276
277 /* get new inode */
278 if (*pinode == NULL) {
Steve French85a6dac2009-04-01 05:22:00 +0000279 __u64 unique_id = le64_to_cpu(find_data.UniqueId);
280 *pinode = cifs_new_inode(sb, &unique_id);
Steve French0e4bbde2008-05-20 19:50:46 +0000281 if (*pinode == NULL) {
282 rc = -ENOMEM;
Jeff Laytone911d0c2008-07-12 13:47:59 -0700283 goto cgiiu_exit;
Steve French0e4bbde2008-05-20 19:50:46 +0000284 }
Steve French0e4bbde2008-05-20 19:50:46 +0000285 }
286
287 inode = *pinode;
288 cifsInfo = CIFS_I(inode);
289
290 cFYI(1, ("Old time %ld", cifsInfo->time));
291 cifsInfo->time = jiffies;
292 cFYI(1, ("New time %ld", cifsInfo->time));
293 /* this is ok to set on every inode revalidate */
294 atomic_set(&cifsInfo->inUse, 1);
295
296 cifs_unix_info_to_inode(inode, &find_data, 0);
297
298 if (num_of_bytes < end_of_file)
299 cFYI(1, ("allocation size less than end of file"));
300 cFYI(1, ("Size %ld and blocks %llu",
301 (unsigned long) inode->i_size,
302 (unsigned long long)inode->i_blocks));
303
304 cifs_set_ops(inode, is_dfs_referral);
Igor Mammedov79626702008-03-09 03:44:18 +0000305cgiiu_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return rc;
307}
308
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000309static int decode_sfu_inode(struct inode *inode, __u64 size,
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800310 const unsigned char *path,
311 struct cifs_sb_info *cifs_sb, int xid)
312{
313 int rc;
Steve French4b18f2a2008-04-29 00:06:05 +0000314 int oplock = 0;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800315 __u16 netfid;
316 struct cifsTconInfo *pTcon = cifs_sb->tcon;
Steve French86c96b42005-11-18 20:25:31 -0800317 char buf[24];
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800318 unsigned int bytes_read;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000319 char *pbuf;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800320
321 pbuf = buf;
322
Steve French4523cc32007-04-30 20:13:06 +0000323 if (size == 0) {
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800324 inode->i_mode |= S_IFIFO;
325 return 0;
326 } else if (size < 8) {
327 return -EINVAL; /* EOPNOTSUPP? */
328 }
Steve French50c2f752007-07-13 00:33:32 +0000329
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800330 rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ,
331 CREATE_NOT_DIR, &netfid, &oplock, NULL,
332 cifs_sb->local_nls,
333 cifs_sb->mnt_cifs_flags &
334 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000335 if (rc == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -0800336 int buf_type = CIFS_NO_BUFFER;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800337 /* Read header */
338 rc = CIFSSMBRead(xid, pTcon,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000339 netfid,
Steve French86c96b42005-11-18 20:25:31 -0800340 24 /* length */, 0 /* offset */,
Steve Frenchec637e32005-12-12 20:53:18 -0800341 &bytes_read, &pbuf, &buf_type);
Steve French4523cc32007-04-30 20:13:06 +0000342 if ((rc == 0) && (bytes_read >= 8)) {
343 if (memcmp("IntxBLK", pbuf, 8) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000344 cFYI(1, ("Block device"));
Steve French3020a1f2005-11-18 11:31:10 -0800345 inode->i_mode |= S_IFBLK;
Steve French4523cc32007-04-30 20:13:06 +0000346 if (bytes_read == 24) {
Steve French86c96b42005-11-18 20:25:31 -0800347 /* we have enough to decode dev num */
348 __u64 mjr; /* major */
349 __u64 mnr; /* minor */
350 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
351 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
352 inode->i_rdev = MKDEV(mjr, mnr);
353 }
Steve French4523cc32007-04-30 20:13:06 +0000354 } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000355 cFYI(1, ("Char device"));
Steve French3020a1f2005-11-18 11:31:10 -0800356 inode->i_mode |= S_IFCHR;
Steve French4523cc32007-04-30 20:13:06 +0000357 if (bytes_read == 24) {
Steve French86c96b42005-11-18 20:25:31 -0800358 /* we have enough to decode dev num */
359 __u64 mjr; /* major */
360 __u64 mnr; /* minor */
361 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
362 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
363 inode->i_rdev = MKDEV(mjr, mnr);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000364 }
Steve French4523cc32007-04-30 20:13:06 +0000365 } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000366 cFYI(1, ("Symlink"));
Steve French3020a1f2005-11-18 11:31:10 -0800367 inode->i_mode |= S_IFLNK;
Steve French86c96b42005-11-18 20:25:31 -0800368 } else {
369 inode->i_mode |= S_IFREG; /* file? */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000370 rc = -EOPNOTSUPP;
Steve French86c96b42005-11-18 20:25:31 -0800371 }
Steve French3020a1f2005-11-18 11:31:10 -0800372 } else {
373 inode->i_mode |= S_IFREG; /* then it is a file */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000374 rc = -EOPNOTSUPP; /* or some unknown SFU type */
375 }
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800376 CIFSSMBClose(xid, pTcon, netfid);
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800377 }
378 return rc;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800379}
380
Steve French9e294f12005-11-17 16:59:21 -0800381#define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
382
Steve French953f8682007-10-31 04:54:42 +0000383static int get_sfu_mode(struct inode *inode,
Steve French9e294f12005-11-17 16:59:21 -0800384 const unsigned char *path,
385 struct cifs_sb_info *cifs_sb, int xid)
386{
Steve French3020a1f2005-11-18 11:31:10 -0800387#ifdef CONFIG_CIFS_XATTR
Steve French9e294f12005-11-17 16:59:21 -0800388 ssize_t rc;
389 char ea_value[4];
390 __u32 mode;
391
392 rc = CIFSSMBQueryEA(xid, cifs_sb->tcon, path, "SETFILEBITS",
393 ea_value, 4 /* size of buf */, cifs_sb->local_nls,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000394 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve French4523cc32007-04-30 20:13:06 +0000395 if (rc < 0)
Steve French9e294f12005-11-17 16:59:21 -0800396 return (int)rc;
397 else if (rc > 3) {
398 mode = le32_to_cpu(*((__le32 *)ea_value));
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000399 inode->i_mode &= ~SFBITS_MASK;
400 cFYI(1, ("special bits 0%o org mode 0%o", mode, inode->i_mode));
Steve French9e294f12005-11-17 16:59:21 -0800401 inode->i_mode = (mode & SFBITS_MASK) | inode->i_mode;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000402 cFYI(1, ("special mode bits 0%o", mode));
Steve French9e294f12005-11-17 16:59:21 -0800403 return 0;
404 } else {
405 return 0;
406 }
Steve French3020a1f2005-11-18 11:31:10 -0800407#else
408 return -EOPNOTSUPP;
409#endif
Steve French9e294f12005-11-17 16:59:21 -0800410}
411
Steve Frenchb9a32602008-05-20 21:52:32 +0000412/*
413 * Needed to setup inode data for the directory which is the
414 * junction to the new submount (ie to setup the fake directory
415 * which represents a DFS referral)
416 */
417static void fill_fake_finddata(FILE_ALL_INFO *pfnd_dat,
418 struct super_block *sb)
419{
Dave Jones0a891ad2008-05-22 14:20:21 +0000420 memset(pfnd_dat, 0, sizeof(FILE_ALL_INFO));
Steve Frenchb9a32602008-05-20 21:52:32 +0000421
422/* __le64 pfnd_dat->AllocationSize = cpu_to_le64(0);
423 __le64 pfnd_dat->EndOfFile = cpu_to_le64(0);
424 __u8 pfnd_dat->DeletePending = 0;
425 __u8 pfnd_data->Directory = 0;
426 __le32 pfnd_dat->EASize = 0;
427 __u64 pfnd_dat->IndexNumber = 0;
428 __u64 pfnd_dat->IndexNumber1 = 0; */
429 pfnd_dat->CreationTime =
430 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
431 pfnd_dat->LastAccessTime =
432 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
433 pfnd_dat->LastWriteTime =
434 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
435 pfnd_dat->ChangeTime =
436 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
437 pfnd_dat->Attributes = cpu_to_le32(ATTR_DIRECTORY);
438 pfnd_dat->NumberOfLinks = cpu_to_le32(2);
439}
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441int cifs_get_inode_info(struct inode **pinode,
Steve French646dd532008-05-15 01:50:56 +0000442 const unsigned char *full_path, FILE_ALL_INFO *pfindData,
Steve French8b1327f2008-03-14 22:37:16 +0000443 struct super_block *sb, int xid, const __u16 *pfid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
445 int rc = 0;
Steve Frenchb9a32602008-05-20 21:52:32 +0000446 __u32 attr;
447 struct cifsInodeInfo *cifsInfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 struct cifsTconInfo *pTcon;
449 struct inode *inode;
450 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 char *buf = NULL;
Steve French5ade9de2008-05-02 20:56:23 +0000452 bool adjustTZ = false;
Igor Mammedov79626702008-03-09 03:44:18 +0000453 bool is_dfs_referral = false;
Jeff Layton4468eb32008-05-22 09:31:40 -0400454 umode_t default_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 pTcon = cifs_sb->tcon;
Steve French646dd532008-05-15 01:50:56 +0000457 cFYI(1, ("Getting info on %s", full_path));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700459 if ((pfindData == NULL) && (*pinode != NULL)) {
460 if (CIFS_I(*pinode)->clientCanCacheRead) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000461 cFYI(1, ("No need to revalidate cached inode sizes"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return rc;
463 }
464 }
465
466 /* if file info not passed in then get it from server */
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700467 if (pfindData == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700469 if (buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return -ENOMEM;
471 pfindData = (FILE_ALL_INFO *)buf;
Igor Mammedov79626702008-03-09 03:44:18 +0000472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 /* could do find first instead but this returns more info */
Igor Mammedov79626702008-03-09 03:44:18 +0000474 rc = CIFSSMBQPathInfo(xid, pTcon, full_path, pfindData,
Steve Frenchacf1a1b2006-10-12 03:28:28 +0000475 0 /* not legacy */,
Steve French6b8edfe2005-08-23 20:26:03 -0700476 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
Steve French737b7582005-04-28 22:41:06 -0700477 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve French6b8edfe2005-08-23 20:26:03 -0700478 /* BB optimize code so we do not make the above call
479 when server claims no NT SMB support and the above call
480 failed at least once - set flag in tcon or mount */
Steve French4523cc32007-04-30 20:13:06 +0000481 if ((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
Igor Mammedov79626702008-03-09 03:44:18 +0000482 rc = SMBQueryInformation(xid, pTcon, full_path,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000483 pfindData, cifs_sb->local_nls,
Steve French6b8edfe2005-08-23 20:26:03 -0700484 cifs_sb->mnt_cifs_flags &
485 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve French4b18f2a2008-04-29 00:06:05 +0000486 adjustTZ = true;
Steve French6b8edfe2005-08-23 20:26:03 -0700487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489 /* dump_mem("\nQPathInfo return data",&findData, sizeof(findData)); */
Steve Frenchb9a32602008-05-20 21:52:32 +0000490 if (rc == -EREMOTE) {
491 is_dfs_referral = true;
492 fill_fake_finddata(pfindData, sb);
493 rc = 0;
494 } else if (rc)
Igor Mammedov79626702008-03-09 03:44:18 +0000495 goto cgii_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Steve Frenchb9a32602008-05-20 21:52:32 +0000497 attr = le32_to_cpu(pfindData->Attributes);
498
499 /* get new inode */
500 if (*pinode == NULL) {
Jeff Layton132ac7b2009-02-10 07:33:57 -0500501 __u64 inode_num;
Jeff Layton950ec522009-02-11 08:08:26 -0500502 __u64 *pinum = &inode_num;
Jeff Layton132ac7b2009-02-10 07:33:57 -0500503
Steve Frenchb9a32602008-05-20 21:52:32 +0000504 /* Is an i_ino of zero legal? Can we use that to check
505 if the server supports returning inode numbers? Are
506 there other sanity checks we can use to ensure that
507 the server is really filling in that field? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Steve Frenchb9a32602008-05-20 21:52:32 +0000509 /* We can not use the IndexNumber field by default from
510 Windows or Samba (in ALL_INFO buf) but we can request
511 it explicitly. It may not be unique presumably if
512 the server has multiple devices mounted under one share */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Steve Frenchb9a32602008-05-20 21:52:32 +0000514 /* There may be higher info levels that work but are
515 there Windows server or network appliances for which
516 IndexNumber field is not guaranteed unique? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Steve Frenchb9a32602008-05-20 21:52:32 +0000518 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
519 int rc1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Steve Frenchb9a32602008-05-20 21:52:32 +0000521 rc1 = CIFSGetSrvInodeNumber(xid, pTcon,
Jeff Layton950ec522009-02-11 08:08:26 -0500522 full_path, pinum,
Steve French737b7582005-04-28 22:41:06 -0700523 cifs_sb->local_nls,
524 cifs_sb->mnt_cifs_flags &
525 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve Frenchb9a32602008-05-20 21:52:32 +0000526 if (rc1) {
527 cFYI(1, ("GetSrvInodeNum rc %d", rc1));
Jeff Layton950ec522009-02-11 08:08:26 -0500528 pinum = NULL;
Steve Frenchb9a32602008-05-20 21:52:32 +0000529 /* BB EOPNOSUPP disable SERVER_INUM? */
Jeff Layton132ac7b2009-02-10 07:33:57 -0500530 }
Jeff Layton132ac7b2009-02-10 07:33:57 -0500531 } else {
Jeff Layton950ec522009-02-11 08:08:26 -0500532 pinum = NULL;
Jeff Layton132ac7b2009-02-10 07:33:57 -0500533 }
534
Jeff Layton950ec522009-02-11 08:08:26 -0500535 *pinode = cifs_new_inode(sb, pinum);
Jeff Layton132ac7b2009-02-10 07:33:57 -0500536 if (*pinode == NULL) {
537 rc = -ENOMEM;
538 goto cgii_exit;
539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
Steve Frenchb9a32602008-05-20 21:52:32 +0000541 inode = *pinode;
542 cifsInfo = CIFS_I(inode);
543 cifsInfo->cifsAttrs = attr;
Jeff Layton9a8165f2008-10-17 21:03:20 -0400544 cifsInfo->delete_pending = pfindData->DeletePending ? true : false;
Steve Frenchb9a32602008-05-20 21:52:32 +0000545 cFYI(1, ("Old time %ld", cifsInfo->time));
546 cifsInfo->time = jiffies;
547 cFYI(1, ("New time %ld", cifsInfo->time));
548
549 /* blksize needs to be multiple of two. So safer to default to
550 blksize and blkbits set in superblock so 2**blkbits and blksize
551 will match rather than setting to:
552 (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
553
554 /* Linux can not store file creation time so ignore it */
555 if (pfindData->LastAccessTime)
556 inode->i_atime = cifs_NTtimeToUnix
557 (le64_to_cpu(pfindData->LastAccessTime));
558 else /* do not need to use current_fs_time - time not stored */
559 inode->i_atime = CURRENT_TIME;
560 inode->i_mtime =
561 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
562 inode->i_ctime =
563 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
564 cFYI(DBG2, ("Attributes came in as 0x%x", attr));
565 if (adjustTZ && (pTcon->ses) && (pTcon->ses->server)) {
566 inode->i_ctime.tv_sec += pTcon->ses->server->timeAdj;
567 inode->i_mtime.tv_sec += pTcon->ses->server->timeAdj;
568 }
569
Jeff Layton4468eb32008-05-22 09:31:40 -0400570 /* get default inode mode */
571 if (attr & ATTR_DIRECTORY)
572 default_mode = cifs_sb->mnt_dir_mode;
573 else
574 default_mode = cifs_sb->mnt_file_mode;
Steve Frenchb9a32602008-05-20 21:52:32 +0000575
Jeff Layton4468eb32008-05-22 09:31:40 -0400576 /* set permission bits */
577 if (atomic_read(&cifsInfo->inUse) == 0 ||
578 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
579 inode->i_mode = default_mode;
580 else {
581 /* just reenable write bits if !ATTR_READONLY */
582 if ((inode->i_mode & S_IWUGO) == 0 &&
583 (attr & ATTR_READONLY) == 0)
584 inode->i_mode |= (S_IWUGO & default_mode);
Ilpo Järvinenaab3a8c2008-08-19 14:23:37 +0000585
586 inode->i_mode &= ~S_IFMT;
Jeff Layton4468eb32008-05-22 09:31:40 -0400587 }
588 /* clear write bits if ATTR_READONLY is set */
589 if (attr & ATTR_READONLY)
590 inode->i_mode &= ~S_IWUGO;
591
592 /* set inode type */
593 if ((attr & ATTR_SYSTEM) &&
594 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) {
595 /* no need to fix endianness on 0 */
596 if (pfindData->EndOfFile == 0)
597 inode->i_mode |= S_IFIFO;
598 else if (decode_sfu_inode(inode,
599 le64_to_cpu(pfindData->EndOfFile),
600 full_path, cifs_sb, xid))
601 cFYI(1, ("unknown SFU file type\n"));
Steve Frenchb9a32602008-05-20 21:52:32 +0000602 } else {
Jeff Layton4468eb32008-05-22 09:31:40 -0400603 if (attr & ATTR_DIRECTORY)
604 inode->i_mode |= S_IFDIR;
605 else
606 inode->i_mode |= S_IFREG;
Steve Frenchb9a32602008-05-20 21:52:32 +0000607 }
608
609 spin_lock(&inode->i_lock);
610 if (is_size_safe_to_change(cifsInfo,
611 le64_to_cpu(pfindData->EndOfFile))) {
612 /* can not safely shrink the file size here if the
613 client is writing to it due to potential races */
614 i_size_write(inode, le64_to_cpu(pfindData->EndOfFile));
615
616 /* 512 bytes (2**9) is the fake blocksize that must be
617 used for this calculation */
618 inode->i_blocks = (512 - 1 + le64_to_cpu(
619 pfindData->AllocationSize)) >> 9;
620 }
621 spin_unlock(&inode->i_lock);
622
623 inode->i_nlink = le32_to_cpu(pfindData->NumberOfLinks);
624
625 /* BB fill in uid and gid here? with help from winbind?
626 or retrieve from NTFS stream extended attribute */
627#ifdef CONFIG_CIFS_EXPERIMENTAL
628 /* fill in 0777 bits from ACL */
629 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
630 cFYI(1, ("Getting mode bits from ACL"));
631 acl_to_uid_mode(inode, full_path, pfid);
632 }
633#endif
634 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
635 /* fill in remaining high mode bits e.g. SUID, VTX */
636 get_sfu_mode(inode, full_path, cifs_sb, xid);
637 } else if (atomic_read(&cifsInfo->inUse) == 0) {
638 inode->i_uid = cifs_sb->mnt_uid;
639 inode->i_gid = cifs_sb->mnt_gid;
640 /* set so we do not keep refreshing these fields with
641 bad data after user has changed them in memory */
642 atomic_set(&cifsInfo->inUse, 1);
643 }
644
645 cifs_set_ops(inode, is_dfs_referral);
646
647
648
649
Igor Mammedov79626702008-03-09 03:44:18 +0000650cgii_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 kfree(buf);
652 return rc;
653}
654
Steve French7f8ed422007-09-28 22:28:55 +0000655static const struct inode_operations cifs_ipc_inode_ops = {
656 .lookup = cifs_lookup,
657};
658
Igor Mammedove4cce942009-02-10 14:10:26 +0300659char *cifs_build_path_to_root(struct cifs_sb_info *cifs_sb)
Steve French8be0ed42008-12-05 19:14:12 +0000660{
661 int pplen = cifs_sb->prepathlen;
662 int dfsplen;
663 char *full_path = NULL;
664
665 /* if no prefix path, simply set path to the root of share to "" */
666 if (pplen == 0) {
667 full_path = kmalloc(1, GFP_KERNEL);
668 if (full_path)
669 full_path[0] = 0;
670 return full_path;
671 }
672
673 if (cifs_sb->tcon && (cifs_sb->tcon->Flags & SMB_SHARE_IS_IN_DFS))
674 dfsplen = strnlen(cifs_sb->tcon->treeName, MAX_TREE_SIZE + 1);
675 else
676 dfsplen = 0;
677
678 full_path = kmalloc(dfsplen + pplen + 1, GFP_KERNEL);
679 if (full_path == NULL)
680 return full_path;
681
682 if (dfsplen) {
683 strncpy(full_path, cifs_sb->tcon->treeName, dfsplen);
684 /* switch slash direction in prepath depending on whether
685 * windows or posix style path names
686 */
687 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) {
688 int i;
689 for (i = 0; i < dfsplen; i++) {
690 if (full_path[i] == '\\')
691 full_path[i] = '/';
692 }
693 }
694 }
695 strncpy(full_path + dfsplen, cifs_sb->prepath, pplen);
696 full_path[dfsplen + pplen] = 0; /* add trailing null */
697 return full_path;
698}
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700/* gets root inode */
David Howellsce634ab2008-02-07 00:15:33 -0800701struct inode *cifs_iget(struct super_block *sb, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
David Howellsce634ab2008-02-07 00:15:33 -0800703 int xid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 struct cifs_sb_info *cifs_sb;
David Howellsce634ab2008-02-07 00:15:33 -0800705 struct inode *inode;
706 long rc;
Steve French8be0ed42008-12-05 19:14:12 +0000707 char *full_path;
David Howellsce634ab2008-02-07 00:15:33 -0800708
709 inode = iget_locked(sb, ino);
710 if (!inode)
711 return ERR_PTR(-ENOMEM);
712 if (!(inode->i_state & I_NEW))
713 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 cifs_sb = CIFS_SB(inode->i_sb);
Igor Mammedove4cce942009-02-10 14:10:26 +0300716 full_path = cifs_build_path_to_root(cifs_sb);
Steve French8be0ed42008-12-05 19:14:12 +0000717 if (full_path == NULL)
718 return ERR_PTR(-ENOMEM);
Steve Frenchc18c8422007-07-18 23:21:09 +0000719
Steve French8be0ed42008-12-05 19:14:12 +0000720 xid = GetXid();
Steve Frenchc18c8422007-07-18 23:21:09 +0000721 if (cifs_sb->tcon->unix_ext)
Steve French8be0ed42008-12-05 19:14:12 +0000722 rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb,
723 xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 else
Steve French8be0ed42008-12-05 19:14:12 +0000725 rc = cifs_get_inode_info(&inode, full_path, NULL, inode->i_sb,
726 xid, NULL);
Steve French7f8ed422007-09-28 22:28:55 +0000727 if (rc && cifs_sb->tcon->ipc) {
728 cFYI(1, ("ipc connection - fake read inode"));
729 inode->i_mode |= S_IFDIR;
730 inode->i_nlink = 2;
731 inode->i_op = &cifs_ipc_inode_ops;
732 inode->i_fop = &simple_dir_operations;
733 inode->i_uid = cifs_sb->mnt_uid;
734 inode->i_gid = cifs_sb->mnt_gid;
Steve Frenchad661332008-08-12 14:14:40 +0000735 } else if (rc) {
Steve French8be0ed42008-12-05 19:14:12 +0000736 kfree(full_path);
David Howellsce634ab2008-02-07 00:15:33 -0800737 _FreeXid(xid);
738 iget_failed(inode);
739 return ERR_PTR(rc);
Steve French7f8ed422007-09-28 22:28:55 +0000740 }
741
David Howellsce634ab2008-02-07 00:15:33 -0800742 unlock_new_inode(inode);
743
Steve French8be0ed42008-12-05 19:14:12 +0000744 kfree(full_path);
David Howellsce634ab2008-02-07 00:15:33 -0800745 /* can not call macro FreeXid here since in a void func
746 * TODO: This is no longer true
747 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 _FreeXid(xid);
David Howellsce634ab2008-02-07 00:15:33 -0800749 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
Steve French388e57b2008-09-16 23:50:58 +0000752static int
753cifs_set_file_info(struct inode *inode, struct iattr *attrs, int xid,
754 char *full_path, __u32 dosattr)
755{
756 int rc;
757 int oplock = 0;
758 __u16 netfid;
759 __u32 netpid;
760 bool set_time = false;
761 struct cifsFileInfo *open_file;
762 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
763 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
764 struct cifsTconInfo *pTcon = cifs_sb->tcon;
765 FILE_BASIC_INFO info_buf;
766
Steve French1adcb712009-02-25 14:19:56 +0000767 if (attrs == NULL)
768 return -EINVAL;
769
Steve French388e57b2008-09-16 23:50:58 +0000770 if (attrs->ia_valid & ATTR_ATIME) {
771 set_time = true;
772 info_buf.LastAccessTime =
773 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
774 } else
775 info_buf.LastAccessTime = 0;
776
777 if (attrs->ia_valid & ATTR_MTIME) {
778 set_time = true;
779 info_buf.LastWriteTime =
780 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
781 } else
782 info_buf.LastWriteTime = 0;
783
784 /*
785 * Samba throws this field away, but windows may actually use it.
786 * Do not set ctime unless other time stamps are changed explicitly
787 * (i.e. by utimes()) since we would then have a mix of client and
788 * server times.
789 */
790 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
791 cFYI(1, ("CIFS - CTIME changed"));
792 info_buf.ChangeTime =
793 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
794 } else
795 info_buf.ChangeTime = 0;
796
797 info_buf.CreationTime = 0; /* don't change */
798 info_buf.Attributes = cpu_to_le32(dosattr);
799
800 /*
801 * If the file is already open for write, just use that fileid
802 */
803 open_file = find_writable_file(cifsInode);
804 if (open_file) {
805 netfid = open_file->netfid;
806 netpid = open_file->pid;
807 goto set_via_filehandle;
808 }
809
810 /*
811 * NT4 apparently returns success on this call, but it doesn't
812 * really work.
813 */
814 if (!(pTcon->ses->flags & CIFS_SES_NT4)) {
815 rc = CIFSSMBSetPathInfo(xid, pTcon, full_path,
816 &info_buf, cifs_sb->local_nls,
817 cifs_sb->mnt_cifs_flags &
818 CIFS_MOUNT_MAP_SPECIAL_CHR);
Jeff Layton6b37faa2008-10-06 21:54:41 +0000819 if (rc == 0) {
820 cifsInode->cifsAttrs = dosattr;
821 goto out;
822 } else if (rc != -EOPNOTSUPP && rc != -EINVAL)
Steve French388e57b2008-09-16 23:50:58 +0000823 goto out;
824 }
825
826 cFYI(1, ("calling SetFileInfo since SetPathInfo for "
827 "times not supported by this server"));
828 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN,
829 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
830 CREATE_NOT_DIR, &netfid, &oplock,
831 NULL, cifs_sb->local_nls,
832 cifs_sb->mnt_cifs_flags &
833 CIFS_MOUNT_MAP_SPECIAL_CHR);
834
835 if (rc != 0) {
836 if (rc == -EIO)
837 rc = -EINVAL;
838 goto out;
839 }
840
841 netpid = current->tgid;
842
843set_via_filehandle:
844 rc = CIFSSMBSetFileInfo(xid, pTcon, &info_buf, netfid, netpid);
Steve Frenchd3889082008-09-24 19:22:52 +0000845 if (!rc)
846 cifsInode->cifsAttrs = dosattr;
847
Steve French388e57b2008-09-16 23:50:58 +0000848 if (open_file == NULL)
849 CIFSSMBClose(xid, pTcon, netfid);
850 else
851 atomic_dec(&open_file->wrtPending);
852out:
853 return rc;
854}
855
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400856/*
857 * open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
858 * and rename it to a random name that hopefully won't conflict with
859 * anything else.
860 */
861static int
Steve French32709582008-10-20 00:44:19 +0000862cifs_rename_pending_delete(char *full_path, struct dentry *dentry, int xid)
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400863{
864 int oplock = 0;
865 int rc;
866 __u16 netfid;
Steve French32709582008-10-20 00:44:19 +0000867 struct inode *inode = dentry->d_inode;
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400868 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
869 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
870 struct cifsTconInfo *tcon = cifs_sb->tcon;
Steve French32709582008-10-20 00:44:19 +0000871 __u32 dosattr, origattr;
872 FILE_BASIC_INFO *info_buf = NULL;
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400873
874 rc = CIFSSMBOpen(xid, tcon, full_path, FILE_OPEN,
Jeff Laytondd1db2d2008-10-16 19:27:12 -0400875 DELETE|FILE_WRITE_ATTRIBUTES, CREATE_NOT_DIR,
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400876 &netfid, &oplock, NULL, cifs_sb->local_nls,
877 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
878 if (rc != 0)
879 goto out;
880
Steve French32709582008-10-20 00:44:19 +0000881 origattr = cifsInode->cifsAttrs;
882 if (origattr == 0)
883 origattr |= ATTR_NORMAL;
884
885 dosattr = origattr & ~ATTR_READONLY;
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400886 if (dosattr == 0)
887 dosattr |= ATTR_NORMAL;
888 dosattr |= ATTR_HIDDEN;
889
Steve French32709582008-10-20 00:44:19 +0000890 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
891 if (dosattr != origattr) {
892 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
893 if (info_buf == NULL) {
894 rc = -ENOMEM;
895 goto out_close;
896 }
897 info_buf->Attributes = cpu_to_le32(dosattr);
898 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, netfid,
899 current->tgid);
900 /* although we would like to mark the file hidden
901 if that fails we will still try to rename it */
Steve French41346092008-10-20 18:24:42 +0000902 if (rc != 0)
Steve French32709582008-10-20 00:44:19 +0000903 cifsInode->cifsAttrs = dosattr;
904 else
905 dosattr = origattr; /* since not able to change them */
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400906 }
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400907
Jeff Laytondd1db2d2008-10-16 19:27:12 -0400908 /* rename the file */
909 rc = CIFSSMBRenameOpenFile(xid, tcon, netfid, NULL, cifs_sb->local_nls,
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400910 cifs_sb->mnt_cifs_flags &
911 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve French32709582008-10-20 00:44:19 +0000912 if (rc != 0) {
913 rc = -ETXTBSY;
914 goto undo_setattr;
915 }
Jeff Layton6d22f092008-09-23 11:48:35 -0400916
Steve French32709582008-10-20 00:44:19 +0000917 /* try to set DELETE_ON_CLOSE */
918 if (!cifsInode->delete_pending) {
919 rc = CIFSSMBSetFileDisposition(xid, tcon, true, netfid,
920 current->tgid);
921 /*
922 * some samba versions return -ENOENT when we try to set the
923 * file disposition here. Likely a samba bug, but work around
924 * it for now. This means that some cifsXXX files may hang
925 * around after they shouldn't.
926 *
927 * BB: remove this hack after more servers have the fix
928 */
929 if (rc == -ENOENT)
930 rc = 0;
931 else if (rc != 0) {
932 rc = -ETXTBSY;
933 goto undo_rename;
934 }
935 cifsInode->delete_pending = true;
936 }
Jeff Layton7ce86d52008-09-24 11:32:59 -0400937
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400938out_close:
939 CIFSSMBClose(xid, tcon, netfid);
940out:
Steve French32709582008-10-20 00:44:19 +0000941 kfree(info_buf);
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400942 return rc;
Steve French32709582008-10-20 00:44:19 +0000943
944 /*
945 * reset everything back to the original state. Don't bother
946 * dealing with errors here since we can't do anything about
947 * them anyway.
948 */
949undo_rename:
950 CIFSSMBRenameOpenFile(xid, tcon, netfid, dentry->d_name.name,
951 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
952 CIFS_MOUNT_MAP_SPECIAL_CHR);
953undo_setattr:
954 if (dosattr != origattr) {
955 info_buf->Attributes = cpu_to_le32(origattr);
956 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, netfid,
957 current->tgid))
958 cifsInode->cifsAttrs = origattr;
959 }
960
961 goto out_close;
Jeff Laytona12a1ac2008-09-23 11:48:35 -0400962}
963
Jeff Layton5f0319a2008-09-16 14:05:16 -0400964int cifs_unlink(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 int rc = 0;
967 int xid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 char *full_path = NULL;
Jeff Layton5f0319a2008-09-16 14:05:16 -0400969 struct inode *inode = dentry->d_inode;
Steve French60502472008-10-07 18:42:52 +0000970 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
Jeff Layton5f0319a2008-09-16 14:05:16 -0400971 struct super_block *sb = dir->i_sb;
972 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
973 struct cifsTconInfo *tcon = cifs_sb->tcon;
Steve French60502472008-10-07 18:42:52 +0000974 struct iattr *attrs = NULL;
975 __u32 dosattr = 0, origattr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Jeff Layton5f0319a2008-09-16 14:05:16 -0400977 cFYI(1, ("cifs_unlink, dir=0x%p, dentry=0x%p", dir, dentry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 xid = GetXid();
980
Jeff Layton5f0319a2008-09-16 14:05:16 -0400981 /* Unlink can be called from rename so we can not take the
982 * sb->s_vfs_rename_mutex here */
983 full_path = build_path_from_dentry(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (full_path == NULL) {
985 FreeXid(xid);
986 return -ENOMEM;
987 }
Steve French2d785a52007-07-15 01:48:57 +0000988
Jeff Layton5f0319a2008-09-16 14:05:16 -0400989 if ((tcon->ses->capabilities & CAP_UNIX) &&
Steve French2d785a52007-07-15 01:48:57 +0000990 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
Jeff Layton5f0319a2008-09-16 14:05:16 -0400991 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
992 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
Steve French2d785a52007-07-15 01:48:57 +0000993 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
994 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
995 cFYI(1, ("posix del rc %d", rc));
996 if ((rc == 0) || (rc == -ENOENT))
997 goto psx_del_no_retry;
998 }
999
Steve French60502472008-10-07 18:42:52 +00001000retry_std_delete:
Jeff Layton5f0319a2008-09-16 14:05:16 -04001001 rc = CIFSSMBDelFile(xid, tcon, full_path, cifs_sb->local_nls,
Steve French737b7582005-04-28 22:41:06 -07001002 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve French60502472008-10-07 18:42:52 +00001003
Steve French2d785a52007-07-15 01:48:57 +00001004psx_del_no_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (!rc) {
Jeff Layton5f0319a2008-09-16 14:05:16 -04001006 if (inode)
1007 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 } else if (rc == -ENOENT) {
Jeff Layton5f0319a2008-09-16 14:05:16 -04001009 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 } else if (rc == -ETXTBSY) {
Steve French32709582008-10-20 00:44:19 +00001011 rc = cifs_rename_pending_delete(full_path, dentry, xid);
Jeff Laytona12a1ac2008-09-23 11:48:35 -04001012 if (rc == 0)
1013 drop_nlink(inode);
Steve French60502472008-10-07 18:42:52 +00001014 } else if (rc == -EACCES && dosattr == 0) {
Steve French388e57b2008-09-16 23:50:58 +00001015 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1016 if (attrs == NULL) {
1017 rc = -ENOMEM;
1018 goto out_reval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
Steve French388e57b2008-09-16 23:50:58 +00001020
1021 /* try to reset dos attributes */
Steve French60502472008-10-07 18:42:52 +00001022 origattr = cifsInode->cifsAttrs;
1023 if (origattr == 0)
1024 origattr |= ATTR_NORMAL;
1025 dosattr = origattr & ~ATTR_READONLY;
Steve French388e57b2008-09-16 23:50:58 +00001026 if (dosattr == 0)
1027 dosattr |= ATTR_NORMAL;
1028 dosattr |= ATTR_HIDDEN;
1029
1030 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
Steve French388e57b2008-09-16 23:50:58 +00001031 if (rc != 0)
1032 goto out_reval;
Steve French60502472008-10-07 18:42:52 +00001033
1034 goto retry_std_delete;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
Steve French60502472008-10-07 18:42:52 +00001036
1037 /* undo the setattr if we errored out and it's needed */
1038 if (rc != 0 && dosattr != 0)
1039 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1040
Steve French388e57b2008-09-16 23:50:58 +00001041out_reval:
Jeff Layton5f0319a2008-09-16 14:05:16 -04001042 if (inode) {
1043 cifsInode = CIFS_I(inode);
Steve Frenchb2aeb9d2005-05-17 13:16:18 -05001044 cifsInode->time = 0; /* will force revalidate to get info
1045 when needed */
Jeff Layton5f0319a2008-09-16 14:05:16 -04001046 inode->i_ctime = current_fs_time(sb);
Steve Frenchb2aeb9d2005-05-17 13:16:18 -05001047 }
Jeff Layton5f0319a2008-09-16 14:05:16 -04001048 dir->i_ctime = dir->i_mtime = current_fs_time(sb);
1049 cifsInode = CIFS_I(dir);
Steve French60502472008-10-07 18:42:52 +00001050 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 kfree(full_path);
Steve French60502472008-10-07 18:42:52 +00001053 kfree(attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 FreeXid(xid);
1055 return rc;
1056}
1057
Jeff Layton44f68fa2009-02-11 08:08:28 -05001058void posix_fill_in_inode(struct inode *tmp_inode,
Steve French0b442d22008-02-26 03:44:02 +00001059 FILE_UNIX_BASIC_INFO *pData, int isNewInode)
Steve French2dd29d32007-04-23 22:07:35 +00001060{
Christoph Hellwig75f12982008-02-25 20:25:21 +00001061 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
Steve French2dd29d32007-04-23 22:07:35 +00001062 loff_t local_size;
1063 struct timespec local_mtime;
1064
Steve French2dd29d32007-04-23 22:07:35 +00001065 cifsInfo->time = jiffies;
1066 atomic_inc(&cifsInfo->inUse);
1067
1068 /* save mtime and size */
1069 local_mtime = tmp_inode->i_mtime;
1070 local_size = tmp_inode->i_size;
1071
Christoph Hellwig75f12982008-02-25 20:25:21 +00001072 cifs_unix_info_to_inode(tmp_inode, pData, 1);
Igor Mammedov79626702008-03-09 03:44:18 +00001073 cifs_set_ops(tmp_inode, false);
Steve French2dd29d32007-04-23 22:07:35 +00001074
Christoph Hellwig75f12982008-02-25 20:25:21 +00001075 if (!S_ISREG(tmp_inode->i_mode))
1076 return;
1077
1078 /*
1079 * No sense invalidating pages for new inode
1080 * since we we have not started caching
1081 * readahead file data yet.
1082 */
1083 if (isNewInode)
1084 return;
1085
1086 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
1087 (local_size == tmp_inode->i_size)) {
1088 cFYI(1, ("inode exists but unchanged"));
Steve French2dd29d32007-04-23 22:07:35 +00001089 } else {
Christoph Hellwig75f12982008-02-25 20:25:21 +00001090 /* file may have changed on server */
1091 cFYI(1, ("invalidate inode, readdir detected change"));
1092 invalidate_remote_inode(tmp_inode);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001093 }
Steve French2dd29d32007-04-23 22:07:35 +00001094}
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode)
1097{
Jeff Layton6b37faa2008-10-06 21:54:41 +00001098 int rc = 0, tmprc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 int xid;
1100 struct cifs_sb_info *cifs_sb;
1101 struct cifsTconInfo *pTcon;
1102 char *full_path = NULL;
1103 struct inode *newinode = NULL;
1104
Steve French6473a552005-11-29 20:20:10 -08001105 cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p", mode, inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 xid = GetXid();
1108
1109 cifs_sb = CIFS_SB(inode->i_sb);
1110 pTcon = cifs_sb->tcon;
1111
Steve French7f573562005-08-30 11:32:14 -07001112 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (full_path == NULL) {
1114 FreeXid(xid);
1115 return -ENOMEM;
1116 }
Steve French50c2f752007-07-13 00:33:32 +00001117
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001118 if ((pTcon->ses->capabilities & CAP_UNIX) &&
1119 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
Steve French2dd29d32007-04-23 22:07:35 +00001120 le64_to_cpu(pTcon->fsUnixInfo.Capability))) {
1121 u32 oplock = 0;
Steve Frenchf6d09982008-01-08 23:18:22 +00001122 FILE_UNIX_BASIC_INFO *pInfo =
Steve French2dd29d32007-04-23 22:07:35 +00001123 kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001124 if (pInfo == NULL) {
Steve French2dd29d32007-04-23 22:07:35 +00001125 rc = -ENOMEM;
1126 goto mkdir_out;
1127 }
Steve French50c2f752007-07-13 00:33:32 +00001128
Al Viroce3b0f82009-03-29 19:08:22 -04001129 mode &= ~current_umask();
Steve French2dd29d32007-04-23 22:07:35 +00001130 rc = CIFSPOSIXCreate(xid, pTcon, SMB_O_DIRECTORY | SMB_O_CREAT,
1131 mode, NULL /* netfid */, pInfo, &oplock,
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001132 full_path, cifs_sb->local_nls,
1133 cifs_sb->mnt_cifs_flags &
Steve French2dd29d32007-04-23 22:07:35 +00001134 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve Frenchc45d7072007-09-17 02:04:21 +00001135 if (rc == -EOPNOTSUPP) {
1136 kfree(pInfo);
1137 goto mkdir_retry_old;
1138 } else if (rc) {
Steve French2dd29d32007-04-23 22:07:35 +00001139 cFYI(1, ("posix mkdir returned 0x%x", rc));
1140 d_drop(direntry);
1141 } else {
Steve French85a6dac2009-04-01 05:22:00 +00001142 __u64 unique_id;
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +00001143 if (pInfo->Type == cpu_to_le32(-1)) {
1144 /* no return info, go query for it */
Steve French5a07cdf2007-09-16 23:12:47 +00001145 kfree(pInfo);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001146 goto mkdir_get_info;
Steve French5a07cdf2007-09-16 23:12:47 +00001147 }
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001148/*BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if need
1149 to set uid/gid */
Steve French2dd29d32007-04-23 22:07:35 +00001150 inc_nlink(inode);
1151 if (pTcon->nocase)
1152 direntry->d_op = &cifs_ci_dentry_ops;
1153 else
1154 direntry->d_op = &cifs_dentry_ops;
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001155
Steve French85a6dac2009-04-01 05:22:00 +00001156 unique_id = le64_to_cpu(pInfo->UniqueId);
1157 newinode = cifs_new_inode(inode->i_sb, &unique_id);
Steve French5a07cdf2007-09-16 23:12:47 +00001158 if (newinode == NULL) {
1159 kfree(pInfo);
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001160 goto mkdir_get_info;
Steve French5a07cdf2007-09-16 23:12:47 +00001161 }
Jeff Layton6b37faa2008-10-06 21:54:41 +00001162
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001163 newinode->i_nlink = 2;
Steve French2dd29d32007-04-23 22:07:35 +00001164 d_instantiate(direntry, newinode);
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001165
1166 /* we already checked in POSIXCreate whether
1167 frame was long enough */
1168 posix_fill_in_inode(direntry->d_inode,
Steve French0b442d22008-02-26 03:44:02 +00001169 pInfo, 1 /* NewInode */);
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001170#ifdef CONFIG_CIFS_DEBUG2
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001171 cFYI(1, ("instantiated dentry %p %s to inode %p",
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001172 direntry, direntry->d_name.name, newinode));
1173
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001174 if (newinode->i_nlink != 2)
1175 cFYI(1, ("unexpected number of links %d",
Steve Frenchcbac3cb2007-04-25 11:46:06 +00001176 newinode->i_nlink));
1177#endif
Steve French2dd29d32007-04-23 22:07:35 +00001178 }
1179 kfree(pInfo);
1180 goto mkdir_out;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001181 }
Steve Frenchc45d7072007-09-17 02:04:21 +00001182mkdir_retry_old:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 /* BB add setting the equivalent of mode via CreateX w/ACLs */
Steve French737b7582005-04-28 22:41:06 -07001184 rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls,
1185 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (rc) {
Steve French26a21b92006-05-31 18:05:34 +00001187 cFYI(1, ("cifs_mkdir returned 0x%x", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 d_drop(direntry);
1189 } else {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001190mkdir_get_info:
Dave Hansend8c76e62006-09-30 23:29:04 -07001191 inc_nlink(inode);
Steve Frenchc18c8422007-07-18 23:21:09 +00001192 if (pTcon->unix_ext)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 rc = cifs_get_inode_info_unix(&newinode, full_path,
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001194 inode->i_sb, xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 else
1196 rc = cifs_get_inode_info(&newinode, full_path, NULL,
Steve French8b1327f2008-03-14 22:37:16 +00001197 inode->i_sb, xid, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Steve Frenchb92327f2005-08-22 20:09:43 -07001199 if (pTcon->nocase)
1200 direntry->d_op = &cifs_ci_dentry_ops;
1201 else
1202 direntry->d_op = &cifs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 d_instantiate(direntry, newinode);
Steve French2dd29d32007-04-23 22:07:35 +00001204 /* setting nlink not necessary except in cases where we
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001205 * failed to get it from the server or was set bogus */
Steve French2dd29d32007-04-23 22:07:35 +00001206 if ((direntry->d_inode) && (direntry->d_inode->i_nlink < 2))
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001207 direntry->d_inode->i_nlink = 2;
Jeff Layton95089912008-08-06 04:39:02 +00001208
Al Viroce3b0f82009-03-29 19:08:22 -04001209 mode &= ~current_umask();
Jeff Layton95089912008-08-06 04:39:02 +00001210 /* must turn on setgid bit if parent dir has it */
1211 if (inode->i_mode & S_ISGID)
1212 mode |= S_ISGID;
1213
Steve Frenchc18c8422007-07-18 23:21:09 +00001214 if (pTcon->unix_ext) {
Jeff Layton4e1e7fb2008-08-02 07:26:12 -04001215 struct cifs_unix_set_info_args args = {
1216 .mode = mode,
1217 .ctime = NO_CHANGE_64,
1218 .atime = NO_CHANGE_64,
1219 .mtime = NO_CHANGE_64,
1220 .device = 0,
1221 };
Steve Frenchd0d2f2d2005-06-02 15:12:36 -07001222 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
David Howellsa001e5b2008-11-14 10:38:47 +11001223 args.uid = (__u64)current_fsuid();
Jeff Layton95089912008-08-06 04:39:02 +00001224 if (inode->i_mode & S_ISGID)
1225 args.gid = (__u64)inode->i_gid;
1226 else
David Howellsa001e5b2008-11-14 10:38:47 +11001227 args.gid = (__u64)current_fsgid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 } else {
Jeff Layton4e1e7fb2008-08-02 07:26:12 -04001229 args.uid = NO_CHANGE_64;
1230 args.gid = NO_CHANGE_64;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
Jeff Layton4e1e7fb2008-08-02 07:26:12 -04001232 CIFSSMBUnixSetInfo(xid, pTcon, full_path, &args,
1233 cifs_sb->local_nls,
1234 cifs_sb->mnt_cifs_flags &
1235 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve French3ce53fc2007-06-08 14:55:14 +00001236 } else {
Jeff Layton67750fb2008-05-09 22:28:02 +00001237 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1238 (mode & S_IWUGO) == 0) {
1239 FILE_BASIC_INFO pInfo;
Jeff Layton6b37faa2008-10-06 21:54:41 +00001240 struct cifsInodeInfo *cifsInode;
1241 u32 dosattrs;
1242
Jeff Layton67750fb2008-05-09 22:28:02 +00001243 memset(&pInfo, 0, sizeof(pInfo));
Jeff Layton6b37faa2008-10-06 21:54:41 +00001244 cifsInode = CIFS_I(newinode);
1245 dosattrs = cifsInode->cifsAttrs|ATTR_READONLY;
1246 pInfo.Attributes = cpu_to_le32(dosattrs);
1247 tmprc = CIFSSMBSetPathInfo(xid, pTcon,
1248 full_path, &pInfo,
1249 cifs_sb->local_nls,
Jeff Layton67750fb2008-05-09 22:28:02 +00001250 cifs_sb->mnt_cifs_flags &
1251 CIFS_MOUNT_MAP_SPECIAL_CHR);
Jeff Layton6b37faa2008-10-06 21:54:41 +00001252 if (tmprc == 0)
1253 cifsInode->cifsAttrs = dosattrs;
Jeff Layton67750fb2008-05-09 22:28:02 +00001254 }
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001255 if (direntry->d_inode) {
Jeff Laytonb0fd30d2008-05-22 09:33:34 -04001256 if (cifs_sb->mnt_cifs_flags &
1257 CIFS_MOUNT_DYNPERM)
1258 direntry->d_inode->i_mode =
1259 (mode | S_IFDIR);
Steve French4e94a102008-05-23 18:22:46 +00001260
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001261 if (cifs_sb->mnt_cifs_flags &
Steve French6473a552005-11-29 20:20:10 -08001262 CIFS_MOUNT_SET_UID) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001263 direntry->d_inode->i_uid =
David Howellsa001e5b2008-11-14 10:38:47 +11001264 current_fsuid();
Jeff Layton95089912008-08-06 04:39:02 +00001265 if (inode->i_mode & S_ISGID)
1266 direntry->d_inode->i_gid =
1267 inode->i_gid;
1268 else
1269 direntry->d_inode->i_gid =
David Howellsa001e5b2008-11-14 10:38:47 +11001270 current_fsgid();
Steve French6473a552005-11-29 20:20:10 -08001271 }
1272 }
Steve French2a138ebb2005-11-29 21:22:19 -08001273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001275mkdir_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 kfree(full_path);
1277 FreeXid(xid);
1278 return rc;
1279}
1280
1281int cifs_rmdir(struct inode *inode, struct dentry *direntry)
1282{
1283 int rc = 0;
1284 int xid;
1285 struct cifs_sb_info *cifs_sb;
1286 struct cifsTconInfo *pTcon;
1287 char *full_path = NULL;
1288 struct cifsInodeInfo *cifsInode;
1289
Steve French26a21b92006-05-31 18:05:34 +00001290 cFYI(1, ("cifs_rmdir, inode = 0x%p", inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 xid = GetXid();
1293
1294 cifs_sb = CIFS_SB(inode->i_sb);
1295 pTcon = cifs_sb->tcon;
1296
Steve French7f573562005-08-30 11:32:14 -07001297 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 if (full_path == NULL) {
1299 FreeXid(xid);
1300 return -ENOMEM;
1301 }
1302
Steve French737b7582005-04-28 22:41:06 -07001303 rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls,
1304 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 if (!rc) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001307 drop_nlink(inode);
Steve French3677db12007-02-26 16:46:11 +00001308 spin_lock(&direntry->d_inode->i_lock);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001309 i_size_write(direntry->d_inode, 0);
Dave Hansence71ec32006-09-30 23:29:06 -07001310 clear_nlink(direntry->d_inode);
Steve French3677db12007-02-26 16:46:11 +00001311 spin_unlock(&direntry->d_inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313
1314 cifsInode = CIFS_I(direntry->d_inode);
1315 cifsInode->time = 0; /* force revalidate to go get info when
1316 needed */
Steve French42c24542009-01-13 22:03:55 +00001317
1318 cifsInode = CIFS_I(inode);
1319 cifsInode->time = 0; /* force revalidate to get parent dir info
1320 since cached search results now invalid */
1321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
1323 current_fs_time(inode->i_sb);
1324
1325 kfree(full_path);
1326 FreeXid(xid);
1327 return rc;
1328}
1329
Steve Frenchee2fd962008-09-23 18:23:33 +00001330static int
1331cifs_do_rename(int xid, struct dentry *from_dentry, const char *fromPath,
1332 struct dentry *to_dentry, const char *toPath)
1333{
1334 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
1335 struct cifsTconInfo *pTcon = cifs_sb->tcon;
1336 __u16 srcfid;
1337 int oplock, rc;
1338
1339 /* try path-based rename first */
1340 rc = CIFSSMBRename(xid, pTcon, fromPath, toPath, cifs_sb->local_nls,
1341 cifs_sb->mnt_cifs_flags &
1342 CIFS_MOUNT_MAP_SPECIAL_CHR);
1343
1344 /*
1345 * don't bother with rename by filehandle unless file is busy and
1346 * source Note that cross directory moves do not work with
1347 * rename by filehandle to various Windows servers.
1348 */
1349 if (rc == 0 || rc != -ETXTBSY)
1350 return rc;
1351
1352 /* open the file to be renamed -- we need DELETE perms */
1353 rc = CIFSSMBOpen(xid, pTcon, fromPath, FILE_OPEN, DELETE,
1354 CREATE_NOT_DIR, &srcfid, &oplock, NULL,
1355 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
1356 CIFS_MOUNT_MAP_SPECIAL_CHR);
1357
1358 if (rc == 0) {
1359 rc = CIFSSMBRenameOpenFile(xid, pTcon, srcfid,
1360 (const char *) to_dentry->d_name.name,
1361 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
1362 CIFS_MOUNT_MAP_SPECIAL_CHR);
1363
1364 CIFSSMBClose(xid, pTcon, srcfid);
1365 }
1366
1367 return rc;
1368}
1369
Jeff Layton14121bd2008-10-20 14:45:22 -04001370int cifs_rename(struct inode *source_dir, struct dentry *source_dentry,
1371 struct inode *target_dir, struct dentry *target_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Steve Frenchee2fd962008-09-23 18:23:33 +00001373 char *fromName = NULL;
1374 char *toName = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 struct cifs_sb_info *cifs_sb_source;
1376 struct cifs_sb_info *cifs_sb_target;
Jeff Layton14121bd2008-10-20 14:45:22 -04001377 struct cifsTconInfo *tcon;
Steve Frenchee2fd962008-09-23 18:23:33 +00001378 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
1379 FILE_UNIX_BASIC_INFO *info_buf_target;
Jeff Layton8d281ef2008-10-22 13:57:01 -04001380 int xid, rc, tmprc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Jeff Layton14121bd2008-10-20 14:45:22 -04001382 cifs_sb_target = CIFS_SB(target_dir->i_sb);
1383 cifs_sb_source = CIFS_SB(source_dir->i_sb);
1384 tcon = cifs_sb_source->tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Steve Frenchee2fd962008-09-23 18:23:33 +00001386 xid = GetXid();
1387
1388 /*
1389 * BB: this might be allowed if same server, but different share.
1390 * Consider adding support for this
1391 */
Jeff Layton14121bd2008-10-20 14:45:22 -04001392 if (tcon != cifs_sb_target->tcon) {
Steve Frenchee2fd962008-09-23 18:23:33 +00001393 rc = -EXDEV;
1394 goto cifs_rename_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
1396
Steve Frenchee2fd962008-09-23 18:23:33 +00001397 /*
1398 * we already have the rename sem so we do not need to
1399 * grab it again here to protect the path integrity
1400 */
Jeff Layton14121bd2008-10-20 14:45:22 -04001401 fromName = build_path_from_dentry(source_dentry);
Steve Frenchee2fd962008-09-23 18:23:33 +00001402 if (fromName == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 rc = -ENOMEM;
1404 goto cifs_rename_exit;
1405 }
1406
Jeff Layton14121bd2008-10-20 14:45:22 -04001407 toName = build_path_from_dentry(target_dentry);
Steve Frenchee2fd962008-09-23 18:23:33 +00001408 if (toName == NULL) {
1409 rc = -ENOMEM;
1410 goto cifs_rename_exit;
1411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Jeff Layton14121bd2008-10-20 14:45:22 -04001413 rc = cifs_do_rename(xid, source_dentry, fromName,
1414 target_dentry, toName);
Steve Frenchee2fd962008-09-23 18:23:33 +00001415
Jeff Layton14121bd2008-10-20 14:45:22 -04001416 if (rc == -EEXIST && tcon->unix_ext) {
Steve Frenchee2fd962008-09-23 18:23:33 +00001417 /*
Jeff Layton14121bd2008-10-20 14:45:22 -04001418 * Are src and dst hardlinks of same inode? We can
1419 * only tell with unix extensions enabled
Steve Frenchee2fd962008-09-23 18:23:33 +00001420 */
Jeff Layton14121bd2008-10-20 14:45:22 -04001421 info_buf_source =
1422 kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO),
1423 GFP_KERNEL);
1424 if (info_buf_source == NULL) {
1425 rc = -ENOMEM;
1426 goto cifs_rename_exit;
1427 }
1428
1429 info_buf_target = info_buf_source + 1;
Jeff Layton8d281ef2008-10-22 13:57:01 -04001430 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, fromName,
Jeff Layton14121bd2008-10-20 14:45:22 -04001431 info_buf_source,
1432 cifs_sb_source->local_nls,
1433 cifs_sb_source->mnt_cifs_flags &
1434 CIFS_MOUNT_MAP_SPECIAL_CHR);
Jeff Layton8d281ef2008-10-22 13:57:01 -04001435 if (tmprc != 0)
Jeff Layton14121bd2008-10-20 14:45:22 -04001436 goto unlink_target;
1437
Jeff Layton8d281ef2008-10-22 13:57:01 -04001438 tmprc = CIFSSMBUnixQPathInfo(xid, tcon,
Jeff Layton14121bd2008-10-20 14:45:22 -04001439 toName, info_buf_target,
1440 cifs_sb_target->local_nls,
1441 /* remap based on source sb */
1442 cifs_sb_source->mnt_cifs_flags &
1443 CIFS_MOUNT_MAP_SPECIAL_CHR);
1444
Jeff Layton8d281ef2008-10-22 13:57:01 -04001445 if (tmprc == 0 && (info_buf_source->UniqueId ==
Jeff Laytonae6884a2008-11-03 14:05:08 -05001446 info_buf_target->UniqueId)) {
Jeff Layton14121bd2008-10-20 14:45:22 -04001447 /* same file, POSIX says that this is a noop */
Jeff Laytonae6884a2008-11-03 14:05:08 -05001448 rc = 0;
Jeff Layton14121bd2008-10-20 14:45:22 -04001449 goto cifs_rename_exit;
Jeff Laytonae6884a2008-11-03 14:05:08 -05001450 }
Jeff Layton14121bd2008-10-20 14:45:22 -04001451 } /* else ... BB we could add the same check for Windows by
1452 checking the UniqueId via FILE_INTERNAL_INFO */
1453
Jeff Layton14121bd2008-10-20 14:45:22 -04001454unlink_target:
Jeff Layton8d281ef2008-10-22 13:57:01 -04001455 if ((rc == -EACCES) || (rc == -EEXIST)) {
1456 tmprc = cifs_unlink(target_dir, target_dentry);
Jeff Layton14121bd2008-10-20 14:45:22 -04001457 if (tmprc)
1458 goto cifs_rename_exit;
1459
Jeff Layton14121bd2008-10-20 14:45:22 -04001460 rc = cifs_do_rename(xid, source_dentry, fromName,
1461 target_dentry, toName);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
1463
1464cifs_rename_exit:
Steve Frenchee2fd962008-09-23 18:23:33 +00001465 kfree(info_buf_source);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 kfree(fromName);
1467 kfree(toName);
1468 FreeXid(xid);
1469 return rc;
1470}
1471
1472int cifs_revalidate(struct dentry *direntry)
1473{
1474 int xid;
Jeff Laytoncea21802007-11-20 23:19:03 +00001475 int rc = 0, wbrc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 char *full_path;
1477 struct cifs_sb_info *cifs_sb;
1478 struct cifsInodeInfo *cifsInode;
1479 loff_t local_size;
1480 struct timespec local_mtime;
Steve French4b18f2a2008-04-29 00:06:05 +00001481 bool invalidate_inode = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 if (direntry->d_inode == NULL)
1484 return -ENOENT;
1485
1486 cifsInode = CIFS_I(direntry->d_inode);
1487
1488 if (cifsInode == NULL)
1489 return -ENOENT;
1490
1491 /* no sense revalidating inode info on file that no one can write */
1492 if (CIFS_I(direntry->d_inode)->clientCanCacheRead)
1493 return rc;
1494
1495 xid = GetXid();
1496
1497 cifs_sb = CIFS_SB(direntry->d_sb);
1498
1499 /* can not safely grab the rename sem here if rename calls revalidate
1500 since that would deadlock */
Steve French7f573562005-08-30 11:32:14 -07001501 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (full_path == NULL) {
1503 FreeXid(xid);
1504 return -ENOMEM;
1505 }
1506 cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld "
1507 "jiffies %ld", full_path, direntry->d_inode,
1508 direntry->d_inode->i_count.counter, direntry,
1509 direntry->d_time, jiffies));
1510
1511 if (cifsInode->time == 0) {
1512 /* was set to zero previously to force revalidate */
1513 } else if (time_before(jiffies, cifsInode->time + HZ) &&
1514 lookupCacheEnabled) {
1515 if ((S_ISREG(direntry->d_inode->i_mode) == 0) ||
1516 (direntry->d_inode->i_nlink == 1)) {
1517 kfree(full_path);
1518 FreeXid(xid);
1519 return rc;
1520 } else {
1521 cFYI(1, ("Have to revalidate file due to hardlinks"));
1522 }
1523 }
1524
1525 /* save mtime and size */
1526 local_mtime = direntry->d_inode->i_mtime;
1527 local_size = direntry->d_inode->i_size;
1528
Steve Frenchc18c8422007-07-18 23:21:09 +00001529 if (cifs_sb->tcon->unix_ext) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path,
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001531 direntry->d_sb, xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (rc) {
1533 cFYI(1, ("error on getting revalidate info %d", rc));
1534/* if (rc != -ENOENT)
1535 rc = 0; */ /* BB should we cache info on
1536 certain errors? */
1537 }
1538 } else {
1539 rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL,
Steve French8b1327f2008-03-14 22:37:16 +00001540 direntry->d_sb, xid, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (rc) {
1542 cFYI(1, ("error on getting revalidate info %d", rc));
1543/* if (rc != -ENOENT)
1544 rc = 0; */ /* BB should we cache info on
1545 certain errors? */
1546 }
1547 }
1548 /* should we remap certain errors, access denied?, to zero */
1549
1550 /* if not oplocked, we invalidate inode pages if mtime or file size
1551 had changed on server */
1552
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001553 if (timespec_equal(&local_mtime, &direntry->d_inode->i_mtime) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 (local_size == direntry->d_inode->i_size)) {
1555 cFYI(1, ("cifs_revalidate - inode unchanged"));
1556 } else {
1557 /* file may have changed on server */
1558 if (cifsInode->clientCanCacheRead) {
1559 /* no need to invalidate inode pages since we were the
1560 only ones who could have modified the file and the
1561 server copy is staler than ours */
1562 } else {
Steve French4b18f2a2008-04-29 00:06:05 +00001563 invalidate_inode = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 }
1565 }
1566
1567 /* can not grab this sem since kernel filesys locking documentation
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001568 indicates i_mutex may be taken by the kernel on lookup and rename
1569 which could deadlock if we grab the i_mutex here as well */
1570/* mutex_lock(&direntry->d_inode->i_mutex);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 /* need to write out dirty pages here */
1572 if (direntry->d_inode->i_mapping) {
1573 /* do we need to lock inode until after invalidate completes
1574 below? */
Jeff Laytoncea21802007-11-20 23:19:03 +00001575 wbrc = filemap_fdatawrite(direntry->d_inode->i_mapping);
1576 if (wbrc)
1577 CIFS_I(direntry->d_inode)->write_behind_rc = wbrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 }
1579 if (invalidate_inode) {
Steve French3abb9272005-11-28 08:16:13 -08001580 /* shrink_dcache not necessary now that cifs dentry ops
1581 are exported for negative dentries */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001582/* if (S_ISDIR(direntry->d_inode->i_mode))
Steve French3abb9272005-11-28 08:16:13 -08001583 shrink_dcache_parent(direntry); */
1584 if (S_ISREG(direntry->d_inode->i_mode)) {
Suresh Jayaraman9e96af82008-08-05 14:38:40 +05301585 if (direntry->d_inode->i_mapping) {
Jeff Laytoncea21802007-11-20 23:19:03 +00001586 wbrc = filemap_fdatawait(direntry->d_inode->i_mapping);
1587 if (wbrc)
1588 CIFS_I(direntry->d_inode)->write_behind_rc = wbrc;
Suresh Jayaraman9e96af82008-08-05 14:38:40 +05301589 }
Steve French3abb9272005-11-28 08:16:13 -08001590 /* may eventually have to do this for open files too */
1591 if (list_empty(&(cifsInode->openFileList))) {
1592 /* changed on server - flush read ahead pages */
1593 cFYI(1, ("Invalidating read ahead data on "
1594 "closed file"));
1595 invalidate_remote_inode(direntry->d_inode);
1596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 }
1598 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001599/* mutex_unlock(&direntry->d_inode->i_mutex); */
Steve French50c2f752007-07-13 00:33:32 +00001600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 kfree(full_path);
1602 FreeXid(xid);
1603 return rc;
1604}
1605
1606int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1607 struct kstat *stat)
1608{
1609 int err = cifs_revalidate(dentry);
Steve French5fe14c82006-11-07 19:26:33 +00001610 if (!err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 generic_fillattr(dentry->d_inode, stat);
Steve French5fe14c82006-11-07 19:26:33 +00001612 stat->blksize = CIFS_MAX_MSGSIZE;
1613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 return err;
1615}
1616
1617static int cifs_truncate_page(struct address_space *mapping, loff_t from)
1618{
1619 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1620 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
1621 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 int rc = 0;
1623
1624 page = grab_cache_page(mapping, index);
1625 if (!page)
1626 return -ENOMEM;
1627
Christoph Lametereebd2aa2008-02-04 22:28:29 -08001628 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 unlock_page(page);
1630 page_cache_release(page);
1631 return rc;
1632}
1633
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001634static int cifs_vmtruncate(struct inode *inode, loff_t offset)
Steve French3677db12007-02-26 16:46:11 +00001635{
1636 struct address_space *mapping = inode->i_mapping;
1637 unsigned long limit;
1638
Steve Frenchba6a46a2007-02-26 20:06:29 +00001639 spin_lock(&inode->i_lock);
Steve French3677db12007-02-26 16:46:11 +00001640 if (inode->i_size < offset)
1641 goto do_expand;
1642 /*
1643 * truncation of in-use swapfiles is disallowed - it would cause
1644 * subsequent swapout to scribble on the now-freed blocks.
1645 */
Steve Frenchba6a46a2007-02-26 20:06:29 +00001646 if (IS_SWAPFILE(inode)) {
1647 spin_unlock(&inode->i_lock);
Steve French3677db12007-02-26 16:46:11 +00001648 goto out_busy;
Steve Frenchba6a46a2007-02-26 20:06:29 +00001649 }
Steve French3677db12007-02-26 16:46:11 +00001650 i_size_write(inode, offset);
1651 spin_unlock(&inode->i_lock);
Steve French8064ab42007-08-22 22:12:07 +00001652 /*
1653 * unmap_mapping_range is called twice, first simply for efficiency
1654 * so that truncate_inode_pages does fewer single-page unmaps. However
1655 * after this first call, and before truncate_inode_pages finishes,
1656 * it is possible for private pages to be COWed, which remain after
1657 * truncate_inode_pages finishes, hence the second unmap_mapping_range
1658 * call must be made for correctness.
1659 */
Steve French3677db12007-02-26 16:46:11 +00001660 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
1661 truncate_inode_pages(mapping, offset);
Steve French8064ab42007-08-22 22:12:07 +00001662 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
Steve French3677db12007-02-26 16:46:11 +00001663 goto out_truncate;
1664
1665do_expand:
1666 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
Steve Frenchba6a46a2007-02-26 20:06:29 +00001667 if (limit != RLIM_INFINITY && offset > limit) {
1668 spin_unlock(&inode->i_lock);
Steve French3677db12007-02-26 16:46:11 +00001669 goto out_sig;
Steve Frenchba6a46a2007-02-26 20:06:29 +00001670 }
1671 if (offset > inode->i_sb->s_maxbytes) {
1672 spin_unlock(&inode->i_lock);
Steve French3677db12007-02-26 16:46:11 +00001673 goto out_big;
Steve Frenchba6a46a2007-02-26 20:06:29 +00001674 }
Steve French3677db12007-02-26 16:46:11 +00001675 i_size_write(inode, offset);
Steve Frenchba6a46a2007-02-26 20:06:29 +00001676 spin_unlock(&inode->i_lock);
Steve French3677db12007-02-26 16:46:11 +00001677out_truncate:
Al Viroacfa4382008-12-04 10:06:33 -05001678 if (inode->i_op->truncate)
Steve French3677db12007-02-26 16:46:11 +00001679 inode->i_op->truncate(inode);
1680 return 0;
1681out_sig:
1682 send_sig(SIGXFSZ, current, 0);
1683out_big:
1684 return -EFBIG;
1685out_busy:
1686 return -ETXTBSY;
1687}
1688
Jeff Layton8efdbde2008-07-23 21:28:12 +00001689static int
1690cifs_set_file_size(struct inode *inode, struct iattr *attrs,
1691 int xid, char *full_path)
1692{
1693 int rc;
1694 struct cifsFileInfo *open_file;
1695 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1696 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1697 struct cifsTconInfo *pTcon = cifs_sb->tcon;
1698
1699 /*
1700 * To avoid spurious oplock breaks from server, in the case of
1701 * inodes that we already have open, avoid doing path based
1702 * setting of file size if we can do it by handle.
1703 * This keeps our caching token (oplock) and avoids timeouts
1704 * when the local oplock break takes longer to flush
1705 * writebehind data than the SMB timeout for the SetPathInfo
1706 * request would allow
1707 */
1708 open_file = find_writable_file(cifsInode);
1709 if (open_file) {
1710 __u16 nfid = open_file->netfid;
1711 __u32 npid = open_file->pid;
1712 rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size, nfid,
1713 npid, false);
1714 atomic_dec(&open_file->wrtPending);
1715 cFYI(1, ("SetFSize for attrs rc = %d", rc));
1716 if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
1717 unsigned int bytes_written;
1718 rc = CIFSSMBWrite(xid, pTcon, nfid, 0, attrs->ia_size,
1719 &bytes_written, NULL, NULL, 1);
1720 cFYI(1, ("Wrt seteof rc %d", rc));
1721 }
1722 } else
1723 rc = -EINVAL;
1724
1725 if (rc != 0) {
1726 /* Set file size by pathname rather than by handle
1727 either because no valid, writeable file handle for
1728 it was found or because there was an error setting
1729 it by handle */
1730 rc = CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size,
1731 false, cifs_sb->local_nls,
1732 cifs_sb->mnt_cifs_flags &
1733 CIFS_MOUNT_MAP_SPECIAL_CHR);
1734 cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc));
1735 if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
1736 __u16 netfid;
1737 int oplock = 0;
1738
1739 rc = SMBLegacyOpen(xid, pTcon, full_path,
1740 FILE_OPEN, GENERIC_WRITE,
1741 CREATE_NOT_DIR, &netfid, &oplock, NULL,
1742 cifs_sb->local_nls,
1743 cifs_sb->mnt_cifs_flags &
1744 CIFS_MOUNT_MAP_SPECIAL_CHR);
1745 if (rc == 0) {
1746 unsigned int bytes_written;
1747 rc = CIFSSMBWrite(xid, pTcon, netfid, 0,
1748 attrs->ia_size,
1749 &bytes_written, NULL,
1750 NULL, 1);
1751 cFYI(1, ("wrt seteof rc %d", rc));
1752 CIFSSMBClose(xid, pTcon, netfid);
1753 }
1754 }
1755 }
1756
1757 if (rc == 0) {
1758 rc = cifs_vmtruncate(inode, attrs->ia_size);
1759 cifs_truncate_page(inode->i_mapping, inode->i_size);
1760 }
1761
1762 return rc;
1763}
1764
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04001765static int
1766cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
1767{
1768 int rc;
1769 int xid;
1770 char *full_path = NULL;
1771 struct inode *inode = direntry->d_inode;
1772 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1773 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1774 struct cifsTconInfo *pTcon = cifs_sb->tcon;
1775 struct cifs_unix_set_info_args *args = NULL;
1776
1777 cFYI(1, ("setattr_unix on file %s attrs->ia_valid=0x%x",
1778 direntry->d_name.name, attrs->ia_valid));
1779
1780 xid = GetXid();
1781
1782 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) {
1783 /* check if we have permission to change attrs */
1784 rc = inode_change_ok(inode, attrs);
1785 if (rc < 0)
1786 goto out;
1787 else
1788 rc = 0;
1789 }
1790
1791 full_path = build_path_from_dentry(direntry);
1792 if (full_path == NULL) {
1793 rc = -ENOMEM;
1794 goto out;
1795 }
1796
Jeff Layton0f4d6342009-03-26 13:35:37 -04001797 /*
1798 * Attempt to flush data before changing attributes. We need to do
1799 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
1800 * ownership or mode then we may also need to do this. Here, we take
1801 * the safe way out and just do the flush on all setattr requests. If
1802 * the flush returns error, store it to report later and continue.
1803 *
1804 * BB: This should be smarter. Why bother flushing pages that
1805 * will be truncated anyway? Also, should we error out here if
1806 * the flush returns error?
1807 */
1808 rc = filemap_write_and_wait(inode->i_mapping);
1809 if (rc != 0) {
1810 cifsInode->write_behind_rc = rc;
1811 rc = 0;
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04001812 }
1813
1814 if (attrs->ia_valid & ATTR_SIZE) {
1815 rc = cifs_set_file_size(inode, attrs, xid, full_path);
1816 if (rc != 0)
1817 goto out;
1818 }
1819
1820 /* skip mode change if it's just for clearing setuid/setgid */
1821 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
1822 attrs->ia_valid &= ~ATTR_MODE;
1823
1824 args = kmalloc(sizeof(*args), GFP_KERNEL);
1825 if (args == NULL) {
1826 rc = -ENOMEM;
1827 goto out;
1828 }
1829
1830 /* set up the struct */
1831 if (attrs->ia_valid & ATTR_MODE)
1832 args->mode = attrs->ia_mode;
1833 else
1834 args->mode = NO_CHANGE_64;
1835
1836 if (attrs->ia_valid & ATTR_UID)
1837 args->uid = attrs->ia_uid;
1838 else
1839 args->uid = NO_CHANGE_64;
1840
1841 if (attrs->ia_valid & ATTR_GID)
1842 args->gid = attrs->ia_gid;
1843 else
1844 args->gid = NO_CHANGE_64;
1845
1846 if (attrs->ia_valid & ATTR_ATIME)
1847 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
1848 else
1849 args->atime = NO_CHANGE_64;
1850
1851 if (attrs->ia_valid & ATTR_MTIME)
1852 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
1853 else
1854 args->mtime = NO_CHANGE_64;
1855
1856 if (attrs->ia_valid & ATTR_CTIME)
1857 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
1858 else
1859 args->ctime = NO_CHANGE_64;
1860
1861 args->device = 0;
1862 rc = CIFSSMBUnixSetInfo(xid, pTcon, full_path, args,
1863 cifs_sb->local_nls,
1864 cifs_sb->mnt_cifs_flags &
1865 CIFS_MOUNT_MAP_SPECIAL_CHR);
1866
1867 if (!rc)
1868 rc = inode_setattr(inode, attrs);
1869out:
1870 kfree(args);
1871 kfree(full_path);
1872 FreeXid(xid);
1873 return rc;
1874}
1875
Jeff Layton0510eeb2008-08-02 07:26:12 -04001876static int
1877cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
1879 int xid;
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04001880 struct inode *inode = direntry->d_inode;
1881 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04001882 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 char *full_path = NULL;
1884 int rc = -EACCES;
Jeff Laytonfeb3e202008-08-02 07:26:12 -04001885 __u32 dosattr = 0;
Jeff Layton4e1e7fb2008-08-02 07:26:12 -04001886 __u64 mode = NO_CHANGE_64;
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04001887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 xid = GetXid();
1889
Steve French39798772006-05-31 22:40:51 +00001890 cFYI(1, ("setattr on file %s attrs->iavalid 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 direntry->d_name.name, attrs->ia_valid));
Steve French6473a552005-11-29 20:20:10 -08001892
Steve French2a138ebb2005-11-29 21:22:19 -08001893 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) {
Steve French6473a552005-11-29 20:20:10 -08001894 /* check if we have permission to change attrs */
Jeff Layton02eadef2008-05-09 21:26:11 +00001895 rc = inode_change_ok(inode, attrs);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001896 if (rc < 0) {
Steve French6473a552005-11-29 20:20:10 -08001897 FreeXid(xid);
1898 return rc;
1899 } else
1900 rc = 0;
1901 }
Steve French50c2f752007-07-13 00:33:32 +00001902
Steve French7f573562005-08-30 11:32:14 -07001903 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (full_path == NULL) {
1905 FreeXid(xid);
1906 return -ENOMEM;
1907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Jeff Layton0f4d6342009-03-26 13:35:37 -04001909 /*
1910 * Attempt to flush data before changing attributes. We need to do
1911 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
1912 * ownership or mode then we may also need to do this. Here, we take
1913 * the safe way out and just do the flush on all setattr requests. If
1914 * the flush returns error, store it to report later and continue.
1915 *
1916 * BB: This should be smarter. Why bother flushing pages that
1917 * will be truncated anyway? Also, should we error out here if
1918 * the flush returns error?
1919 */
1920 rc = filemap_write_and_wait(inode->i_mapping);
1921 if (rc != 0) {
1922 cifsInode->write_behind_rc = rc;
1923 rc = 0;
Steve French50531442008-03-14 19:21:31 +00001924 }
Jeff Laytoncea21802007-11-20 23:19:03 +00001925
Steve French50531442008-03-14 19:21:31 +00001926 if (attrs->ia_valid & ATTR_SIZE) {
Jeff Layton8efdbde2008-07-23 21:28:12 +00001927 rc = cifs_set_file_size(inode, attrs, xid, full_path);
1928 if (rc != 0)
Steve Frenche30dcf32005-09-20 20:49:16 -07001929 goto cifs_setattr_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 }
Jeff Layton4ca691a2008-05-22 09:33:34 -04001931
1932 /*
1933 * Without unix extensions we can't send ownership changes to the
1934 * server, so silently ignore them. This is consistent with how
1935 * local DOS/Windows filesystems behave (VFAT, NTFS, etc). With
1936 * CIFSACL support + proper Windows to Unix idmapping, we may be
1937 * able to support this in the future.
1938 */
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04001939 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
Jeff Layton4ca691a2008-05-22 09:33:34 -04001940 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Jeff Laytond32c4f22007-10-18 03:05:22 -07001942 /* skip mode change if it's just for clearing setuid/setgid */
1943 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
1944 attrs->ia_valid &= ~ATTR_MODE;
1945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 if (attrs->ia_valid & ATTR_MODE) {
Jeff Layton51328612008-05-22 09:33:34 -04001947 cFYI(1, ("Mode changed to 0%o", attrs->ia_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 mode = attrs->ia_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 }
1950
Jeff Layton3fe5c1d2008-08-02 07:26:12 -04001951 if (attrs->ia_valid & ATTR_MODE) {
Steve Frenchcdbce9c2005-11-19 21:04:52 -08001952 rc = 0;
Steve French97837582007-12-31 07:47:21 +00001953#ifdef CONFIG_CIFS_EXPERIMENTAL
1954 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
Jeff Layton02eadef2008-05-09 21:26:11 +00001955 rc = mode_to_acl(inode, full_path, mode);
Jeff Layton51328612008-05-22 09:33:34 -04001956 else
Steve French97837582007-12-31 07:47:21 +00001957#endif
Jeff Layton51328612008-05-22 09:33:34 -04001958 if (((mode & S_IWUGO) == 0) &&
1959 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
Jeff Laytonfeb3e202008-08-02 07:26:12 -04001960
1961 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
1962
Jeff Layton51328612008-05-22 09:33:34 -04001963 /* fix up mode if we're not using dynperm */
1964 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
1965 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
1966 } else if ((mode & S_IWUGO) &&
1967 (cifsInode->cifsAttrs & ATTR_READONLY)) {
Jeff Laytonfeb3e202008-08-02 07:26:12 -04001968
1969 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
1970 /* Attributes of 0 are ignored */
1971 if (dosattr == 0)
1972 dosattr |= ATTR_NORMAL;
Jeff Layton51328612008-05-22 09:33:34 -04001973
1974 /* reset local inode permissions to normal */
1975 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
1976 attrs->ia_mode &= ~(S_IALLUGO);
1977 if (S_ISDIR(inode->i_mode))
1978 attrs->ia_mode |=
1979 cifs_sb->mnt_dir_mode;
1980 else
1981 attrs->ia_mode |=
1982 cifs_sb->mnt_file_mode;
1983 }
1984 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
1985 /* ignore mode change - ATTR_READONLY hasn't changed */
1986 attrs->ia_valid &= ~ATTR_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 }
1989
Jeff Laytonfeb3e202008-08-02 07:26:12 -04001990 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
1991 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
1992 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
1993 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Steve Frenche30dcf32005-09-20 20:49:16 -07001995 /* Even if error on time set, no sense failing the call if
1996 the server would set the time to a reasonable value anyway,
1997 and this check ensures that we are not being called from
1998 sys_utimes in which case we ought to fail the call back to
1999 the user when the server rejects the call */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00002000 if ((rc) && (attrs->ia_valid &
Jeff Laytonfeb3e202008-08-02 07:26:12 -04002001 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
Steve Frenche30dcf32005-09-20 20:49:16 -07002002 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 }
2004
2005 /* do not need local check to inode_check_ok since the server does
2006 that */
2007 if (!rc)
Jeff Layton02eadef2008-05-09 21:26:11 +00002008 rc = inode_setattr(inode, attrs);
Steve Frenche30dcf32005-09-20 20:49:16 -07002009cifs_setattr_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 kfree(full_path);
2011 FreeXid(xid);
2012 return rc;
2013}
2014
Jeff Layton0510eeb2008-08-02 07:26:12 -04002015int
2016cifs_setattr(struct dentry *direntry, struct iattr *attrs)
2017{
2018 struct inode *inode = direntry->d_inode;
2019 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2020 struct cifsTconInfo *pTcon = cifs_sb->tcon;
2021
2022 if (pTcon->unix_ext)
2023 return cifs_setattr_unix(direntry, attrs);
2024
2025 return cifs_setattr_nounix(direntry, attrs);
2026
2027 /* BB: add cifs_setattr_legacy for really old servers */
2028}
2029
Steve French99ee4db2007-02-27 05:35:17 +00002030#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031void cifs_delete_inode(struct inode *inode)
2032{
Steve French26a21b92006-05-31 18:05:34 +00002033 cFYI(1, ("In cifs_delete_inode, inode = 0x%p", inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 /* may have to add back in if and when safe distributed caching of
2035 directories added e.g. via FindNotify */
2036}
Steve French99ee4db2007-02-27 05:35:17 +00002037#endif