blob: d34325c887c4977bc2a6a0197372986423c0fd86 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/inode.c
3 *
4 * Copyright (C) International Business Machines Corp., 2002,2005
5 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21#include <linux/fs.h>
22#include <linux/buffer_head.h>
23#include <linux/stat.h>
24#include <linux/pagemap.h>
25#include <asm/div64.h>
26#include "cifsfs.h"
27#include "cifspdu.h"
28#include "cifsglob.h"
29#include "cifsproto.h"
30#include "cifs_debug.h"
31#include "cifs_fs_sb.h"
32
33int cifs_get_inode_info_unix(struct inode **pinode,
34 const unsigned char *search_path, struct super_block *sb, int xid)
35{
36 int rc = 0;
37 FILE_UNIX_BASIC_INFO findData;
38 struct cifsTconInfo *pTcon;
39 struct inode *inode;
40 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
41 char *tmp_path;
42
43 pTcon = cifs_sb->tcon;
Steve French3020a1f2005-11-18 11:31:10 -080044 cFYI(1, ("Getting info on %s ", search_path));
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 /* could have done a find first instead but this returns more info */
46 rc = CIFSSMBUnixQPathInfo(xid, pTcon, search_path, &findData,
Steve French737b7582005-04-28 22:41:06 -070047 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
48 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/* dump_mem("\nUnixQPathInfo return data", &findData,
50 sizeof(findData)); */
51 if (rc) {
52 if (rc == -EREMOTE) {
53 tmp_path =
54 kmalloc(strnlen(pTcon->treeName,
55 MAX_TREE_SIZE + 1) +
56 strnlen(search_path, MAX_PATHCONF) + 1,
57 GFP_KERNEL);
58 if (tmp_path == NULL) {
59 return -ENOMEM;
60 }
61 /* have to skip first of the double backslash of
62 UNC name */
63 strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE);
64 strncat(tmp_path, search_path, MAX_PATHCONF);
65 rc = connect_to_dfs_path(xid, pTcon->ses,
66 /* treename + */ tmp_path,
Steve French737b7582005-04-28 22:41:06 -070067 cifs_sb->local_nls,
68 cifs_sb->mnt_cifs_flags &
69 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 kfree(tmp_path);
71
72 /* BB fix up inode etc. */
73 } else if (rc) {
74 return rc;
75 }
76 } else {
77 struct cifsInodeInfo *cifsInfo;
78 __u32 type = le32_to_cpu(findData.Type);
79 __u64 num_of_bytes = le64_to_cpu(findData.NumOfBytes);
80 __u64 end_of_file = le64_to_cpu(findData.EndOfFile);
81
82 /* get new inode */
83 if (*pinode == NULL) {
84 *pinode = new_inode(sb);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -070085 if (*pinode == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return -ENOMEM;
87 /* Is an i_ino of zero legal? */
88 /* Are there sanity checks we can use to ensure that
89 the server is really filling in that field? */
Steve Frenchd0d2f2d2005-06-02 15:12:36 -070090 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 (*pinode)->i_ino =
92 (unsigned long)findData.UniqueId;
93 } /* note ino incremented to unique num in new_inode */
94 insert_inode_hash(*pinode);
95 }
96
97 inode = *pinode;
98 cifsInfo = CIFS_I(inode);
99
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800100 cFYI(1, ("Old time %ld ", cifsInfo->time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 cifsInfo->time = jiffies;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800102 cFYI(1, ("New time %ld ", cifsInfo->time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 /* this is ok to set on every inode revalidate */
104 atomic_set(&cifsInfo->inUse,1);
105
106 inode->i_atime =
107 cifs_NTtimeToUnix(le64_to_cpu(findData.LastAccessTime));
108 inode->i_mtime =
109 cifs_NTtimeToUnix(le64_to_cpu
110 (findData.LastModificationTime));
111 inode->i_ctime =
112 cifs_NTtimeToUnix(le64_to_cpu(findData.LastStatusChange));
113 inode->i_mode = le64_to_cpu(findData.Permissions);
Steve French3020a1f2005-11-18 11:31:10 -0800114 /* since we set the inode type below we need to mask off
115 to avoid strange results if bits set above */
116 inode->i_mode &= ~S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (type == UNIX_FILE) {
118 inode->i_mode |= S_IFREG;
119 } else if (type == UNIX_SYMLINK) {
120 inode->i_mode |= S_IFLNK;
121 } else if (type == UNIX_DIR) {
122 inode->i_mode |= S_IFDIR;
123 } else if (type == UNIX_CHARDEV) {
124 inode->i_mode |= S_IFCHR;
125 inode->i_rdev = MKDEV(le64_to_cpu(findData.DevMajor),
126 le64_to_cpu(findData.DevMinor) & MINORMASK);
127 } else if (type == UNIX_BLOCKDEV) {
128 inode->i_mode |= S_IFBLK;
129 inode->i_rdev = MKDEV(le64_to_cpu(findData.DevMajor),
130 le64_to_cpu(findData.DevMinor) & MINORMASK);
131 } else if (type == UNIX_FIFO) {
132 inode->i_mode |= S_IFIFO;
133 } else if (type == UNIX_SOCKET) {
134 inode->i_mode |= S_IFSOCK;
Steve French3020a1f2005-11-18 11:31:10 -0800135 } else {
136 /* safest to call it a file if we do not know */
137 inode->i_mode |= S_IFREG;
138 cFYI(1,("unknown type %d",type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 }
140 inode->i_uid = le64_to_cpu(findData.Uid);
141 inode->i_gid = le64_to_cpu(findData.Gid);
142 inode->i_nlink = le64_to_cpu(findData.Nlinks);
143
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700144 if (is_size_safe_to_change(cifsInfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 /* can not safely change the file size here if the
146 client is writing to it due to potential races */
147
148 i_size_write(inode, end_of_file);
149
150 /* blksize needs to be multiple of two. So safer to default to
151 blksize and blkbits set in superblock so 2**blkbits and blksize
152 will match rather than setting to:
153 (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
154
155 /* This seems incredibly stupid but it turns out that i_blocks
156 is not related to (i_size / i_blksize), instead 512 byte size
157 is required for calculating num blocks */
158
159 /* 512 bytes (2**9) is the fake blocksize that must be used */
160 /* for this calculation */
161 inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
162 }
163
164 if (num_of_bytes < end_of_file)
Steve French8b94bcb2005-11-11 11:41:00 -0800165 cFYI(1, ("allocation size less than end of file"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 cFYI(1,
167 ("Size %ld and blocks %ld",
168 (unsigned long) inode->i_size, inode->i_blocks));
169 if (S_ISREG(inode->i_mode)) {
Steve French8b94bcb2005-11-11 11:41:00 -0800170 cFYI(1, ("File inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 inode->i_op = &cifs_file_inode_ops;
Steve French8b94bcb2005-11-11 11:41:00 -0800172 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
173 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
174 inode->i_fop =
175 &cifs_file_direct_nobrl_ops;
176 else
177 inode->i_fop = &cifs_file_direct_ops;
178 } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
179 inode->i_fop = &cifs_file_nobrl_ops;
180 else /* not direct, send byte range locks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 inode->i_fop = &cifs_file_ops;
Steve French8b94bcb2005-11-11 11:41:00 -0800182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 inode->i_data.a_ops = &cifs_addr_ops;
Steve Frenchbfa0d752005-08-31 21:50:37 -0700184 /* check if server can support readpages */
185 if(pTcon->ses->server->maxBuf <
186 4096 + MAX_CIFS_HDR_SIZE)
187 inode->i_data.a_ops->readpages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 } else if (S_ISDIR(inode->i_mode)) {
Steve French8b94bcb2005-11-11 11:41:00 -0800189 cFYI(1, ("Directory inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 inode->i_op = &cifs_dir_inode_ops;
191 inode->i_fop = &cifs_dir_ops;
192 } else if (S_ISLNK(inode->i_mode)) {
Steve French8b94bcb2005-11-11 11:41:00 -0800193 cFYI(1, ("Symbolic Link inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 inode->i_op = &cifs_symlink_inode_ops;
195 /* tmp_inode->i_fop = */ /* do not need to set to anything */
196 } else {
Steve French8b94bcb2005-11-11 11:41:00 -0800197 cFYI(1, ("Init special inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 init_special_inode(inode, inode->i_mode,
199 inode->i_rdev);
200 }
201 }
202 return rc;
203}
204
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800205static int decode_sfu_inode(struct inode * inode, __u64 size,
206 const unsigned char *path,
207 struct cifs_sb_info *cifs_sb, int xid)
208{
209 int rc;
210 int oplock = FALSE;
211 __u16 netfid;
212 struct cifsTconInfo *pTcon = cifs_sb->tcon;
Steve French86c96b42005-11-18 20:25:31 -0800213 char buf[24];
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800214 unsigned int bytes_read;
215 char * pbuf;
216
217 pbuf = buf;
218
219 if(size == 0) {
220 inode->i_mode |= S_IFIFO;
221 return 0;
222 } else if (size < 8) {
223 return -EINVAL; /* EOPNOTSUPP? */
224 }
225
226 rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ,
227 CREATE_NOT_DIR, &netfid, &oplock, NULL,
228 cifs_sb->local_nls,
229 cifs_sb->mnt_cifs_flags &
230 CIFS_MOUNT_MAP_SPECIAL_CHR);
231 if (rc==0) {
232 /* Read header */
233 rc = CIFSSMBRead(xid, pTcon,
234 netfid,
Steve French86c96b42005-11-18 20:25:31 -0800235 24 /* length */, 0 /* offset */,
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800236 &bytes_read, &pbuf);
Steve French86c96b42005-11-18 20:25:31 -0800237 if((rc == 0) && (bytes_read >= 8)) {
Steve French9e294f12005-11-17 16:59:21 -0800238 if(memcmp("IntxBLK", pbuf, 8) == 0) {
239 cFYI(1,("Block device"));
Steve French3020a1f2005-11-18 11:31:10 -0800240 inode->i_mode |= S_IFBLK;
Steve French86c96b42005-11-18 20:25:31 -0800241 if(bytes_read == 24) {
242 /* we have enough to decode dev num */
243 __u64 mjr; /* major */
244 __u64 mnr; /* minor */
245 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
246 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
247 inode->i_rdev = MKDEV(mjr, mnr);
248 }
Steve French9e294f12005-11-17 16:59:21 -0800249 } else if(memcmp("IntxCHR", pbuf, 8) == 0) {
250 cFYI(1,("Char device"));
Steve French3020a1f2005-11-18 11:31:10 -0800251 inode->i_mode |= S_IFCHR;
Steve French86c96b42005-11-18 20:25:31 -0800252 if(bytes_read == 24) {
253 /* we have enough to decode dev num */
254 __u64 mjr; /* major */
255 __u64 mnr; /* minor */
256 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
257 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
258 inode->i_rdev = MKDEV(mjr, mnr);
259 }
Steve French9e294f12005-11-17 16:59:21 -0800260 } else if(memcmp("IntxLNK", pbuf, 7) == 0) {
261 cFYI(1,("Symlink"));
Steve French3020a1f2005-11-18 11:31:10 -0800262 inode->i_mode |= S_IFLNK;
Steve French86c96b42005-11-18 20:25:31 -0800263 } else {
264 inode->i_mode |= S_IFREG; /* file? */
265 rc = -EOPNOTSUPP;
266 }
Steve French3020a1f2005-11-18 11:31:10 -0800267 } else {
268 inode->i_mode |= S_IFREG; /* then it is a file */
269 rc = -EOPNOTSUPP; /* or some unknown SFU type */
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800270 }
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800271 CIFSSMBClose(xid, pTcon, netfid);
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800272 }
273 return rc;
274
275}
276
Steve French9e294f12005-11-17 16:59:21 -0800277#define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
278
279static int get_sfu_uid_mode(struct inode * inode,
280 const unsigned char *path,
281 struct cifs_sb_info *cifs_sb, int xid)
282{
Steve French3020a1f2005-11-18 11:31:10 -0800283#ifdef CONFIG_CIFS_XATTR
Steve French9e294f12005-11-17 16:59:21 -0800284 ssize_t rc;
285 char ea_value[4];
286 __u32 mode;
287
288 rc = CIFSSMBQueryEA(xid, cifs_sb->tcon, path, "SETFILEBITS",
289 ea_value, 4 /* size of buf */, cifs_sb->local_nls,
290 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
291 if(rc < 0)
292 return (int)rc;
293 else if (rc > 3) {
294 mode = le32_to_cpu(*((__le32 *)ea_value));
Steve Frenchc119b872005-11-18 12:27:27 -0800295 inode->i_mode &= ~SFBITS_MASK;
Steve French3020a1f2005-11-18 11:31:10 -0800296 cFYI(1,("special bits 0%o org mode 0%o", mode, inode->i_mode));
Steve French9e294f12005-11-17 16:59:21 -0800297 inode->i_mode = (mode & SFBITS_MASK) | inode->i_mode;
298 cFYI(1,("special mode bits 0%o", mode));
299 return 0;
300 } else {
301 return 0;
302 }
Steve French3020a1f2005-11-18 11:31:10 -0800303#else
304 return -EOPNOTSUPP;
305#endif
306
Steve French9e294f12005-11-17 16:59:21 -0800307
308}
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310int cifs_get_inode_info(struct inode **pinode,
311 const unsigned char *search_path, FILE_ALL_INFO *pfindData,
312 struct super_block *sb, int xid)
313{
314 int rc = 0;
315 struct cifsTconInfo *pTcon;
316 struct inode *inode;
317 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
318 char *tmp_path;
319 char *buf = NULL;
320
321 pTcon = cifs_sb->tcon;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800322 cFYI(1,("Getting info on %s", search_path));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700324 if ((pfindData == NULL) && (*pinode != NULL)) {
325 if (CIFS_I(*pinode)->clientCanCacheRead) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 cFYI(1,("No need to revalidate cached inode sizes"));
327 return rc;
328 }
329 }
330
331 /* if file info not passed in then get it from server */
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700332 if (pfindData == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700334 if (buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return -ENOMEM;
336 pfindData = (FILE_ALL_INFO *)buf;
337 /* could do find first instead but this returns more info */
338 rc = CIFSSMBQPathInfo(xid, pTcon, search_path, pfindData,
Steve French6b8edfe2005-08-23 20:26:03 -0700339 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
Steve French737b7582005-04-28 22:41:06 -0700340 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve French6b8edfe2005-08-23 20:26:03 -0700341 /* BB optimize code so we do not make the above call
342 when server claims no NT SMB support and the above call
343 failed at least once - set flag in tcon or mount */
344 if((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
345 rc = SMBQueryInformation(xid, pTcon, search_path,
346 pfindData, cifs_sb->local_nls,
347 cifs_sb->mnt_cifs_flags &
348 CIFS_MOUNT_MAP_SPECIAL_CHR);
349 }
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352 /* dump_mem("\nQPathInfo return data",&findData, sizeof(findData)); */
353 if (rc) {
354 if (rc == -EREMOTE) {
355 tmp_path =
356 kmalloc(strnlen
357 (pTcon->treeName,
358 MAX_TREE_SIZE + 1) +
359 strnlen(search_path, MAX_PATHCONF) + 1,
360 GFP_KERNEL);
361 if (tmp_path == NULL) {
362 kfree(buf);
363 return -ENOMEM;
364 }
365
366 strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE);
367 strncat(tmp_path, search_path, MAX_PATHCONF);
368 rc = connect_to_dfs_path(xid, pTcon->ses,
369 /* treename + */ tmp_path,
Steve French737b7582005-04-28 22:41:06 -0700370 cifs_sb->local_nls,
371 cifs_sb->mnt_cifs_flags &
372 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 kfree(tmp_path);
374 /* BB fix up inode etc. */
375 } else if (rc) {
376 kfree(buf);
377 return rc;
378 }
379 } else {
380 struct cifsInodeInfo *cifsInfo;
381 __u32 attr = le32_to_cpu(pfindData->Attributes);
382
383 /* get new inode */
384 if (*pinode == NULL) {
385 *pinode = new_inode(sb);
386 if (*pinode == NULL)
387 return -ENOMEM;
388 /* Is an i_ino of zero legal? Can we use that to check
389 if the server supports returning inode numbers? Are
390 there other sanity checks we can use to ensure that
391 the server is really filling in that field? */
392
393 /* We can not use the IndexNumber field by default from
394 Windows or Samba (in ALL_INFO buf) but we can request
395 it explicitly. It may not be unique presumably if
396 the server has multiple devices mounted under one
397 share */
398
399 /* There may be higher info levels that work but are
400 there Windows server or network appliances for which
401 IndexNumber field is not guaranteed unique? */
402
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700403 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 int rc1 = 0;
405 __u64 inode_num;
406
407 rc1 = CIFSGetSrvInodeNumber(xid, pTcon,
408 search_path, &inode_num,
Steve French737b7582005-04-28 22:41:06 -0700409 cifs_sb->local_nls,
410 cifs_sb->mnt_cifs_flags &
411 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700412 if (rc1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 cFYI(1,("GetSrvInodeNum rc %d", rc1));
414 /* BB EOPNOSUPP disable SERVER_INUM? */
415 } else /* do we need cast or hash to ino? */
416 (*pinode)->i_ino = inode_num;
417 } /* else ino incremented to unique num in new_inode*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 insert_inode_hash(*pinode);
419 }
420 inode = *pinode;
421 cifsInfo = CIFS_I(inode);
422 cifsInfo->cifsAttrs = attr;
Steve French3020a1f2005-11-18 11:31:10 -0800423 cFYI(1, ("Old time %ld ", cifsInfo->time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 cifsInfo->time = jiffies;
Steve French3020a1f2005-11-18 11:31:10 -0800425 cFYI(1, ("New time %ld ", cifsInfo->time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 /* blksize needs to be multiple of two. So safer to default to
428 blksize and blkbits set in superblock so 2**blkbits and blksize
429 will match rather than setting to:
430 (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
431
432 /* Linux can not store file creation time unfortunately so we ignore it */
433 inode->i_atime =
434 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
435 inode->i_mtime =
436 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
437 inode->i_ctime =
438 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
Steve French3020a1f2005-11-18 11:31:10 -0800439 cFYI(0, ("Attributes came in as 0x%x ", attr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 /* set default mode. will override for dirs below */
442 if (atomic_read(&cifsInfo->inUse) == 0)
443 /* new inode, can safely set these fields */
444 inode->i_mode = cifs_sb->mnt_file_mode;
Steve French3020a1f2005-11-18 11:31:10 -0800445 else /* since we set the inode type below we need to mask off
446 to avoid strange results if type changes and both get orred in */
447 inode->i_mode &= ~S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448/* if (attr & ATTR_REPARSE) */
449 /* We no longer handle these as symlinks because we could not
450 follow them due to the absolute path with drive letter */
451 if (attr & ATTR_DIRECTORY) {
452 /* override default perms since we do not do byte range locking
453 on dirs */
454 inode->i_mode = cifs_sb->mnt_dir_mode;
455 inode->i_mode |= S_IFDIR;
Steve Frencheda3c0292005-07-21 15:20:28 -0700456 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
457 (cifsInfo->cifsAttrs & ATTR_SYSTEM) &&
458 /* No need to le64 convert size of zero */
459 (pfindData->EndOfFile == 0)) {
460 inode->i_mode = cifs_sb->mnt_file_mode;
461 inode->i_mode |= S_IFIFO;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800462/* BB Finish for SFU style symlinks and devices */
463 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
464 (cifsInfo->cifsAttrs & ATTR_SYSTEM)) {
465 if (decode_sfu_inode(inode,
466 le64_to_cpu(pfindData->EndOfFile),
467 search_path,
468 cifs_sb, xid)) {
469 cFYI(1,("Unrecognized sfu inode type"));
470 }
Steve French3020a1f2005-11-18 11:31:10 -0800471 cFYI(1,("sfu mode 0%o",inode->i_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 } else {
473 inode->i_mode |= S_IFREG;
474 /* treat the dos attribute of read-only as read-only
475 mode e.g. 555 */
476 if (cifsInfo->cifsAttrs & ATTR_READONLY)
477 inode->i_mode &= ~(S_IWUGO);
478 /* BB add code here -
479 validate if device or weird share or device type? */
480 }
481 if (is_size_safe_to_change(cifsInfo)) {
482 /* can not safely change the file size here if the
483 client is writing to it due to potential races */
484 i_size_write(inode,le64_to_cpu(pfindData->EndOfFile));
485
486 /* 512 bytes (2**9) is the fake blocksize that must be
487 used for this calculation */
488 inode->i_blocks = (512 - 1 + le64_to_cpu(
489 pfindData->AllocationSize)) >> 9;
490 }
491
492 inode->i_nlink = le32_to_cpu(pfindData->NumberOfLinks);
493
494 /* BB fill in uid and gid here? with help from winbind?
495 or retrieve from NTFS stream extended attribute */
Steve French9e294f12005-11-17 16:59:21 -0800496 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
497 /* fill in uid, gid, mode from server ACL */
498 get_sfu_uid_mode(inode, search_path, cifs_sb, xid);
499 } else if (atomic_read(&cifsInfo->inUse) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 inode->i_uid = cifs_sb->mnt_uid;
501 inode->i_gid = cifs_sb->mnt_gid;
502 /* set so we do not keep refreshing these fields with
503 bad data after user has changed them in memory */
504 atomic_set(&cifsInfo->inUse,1);
505 }
506
507 if (S_ISREG(inode->i_mode)) {
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800508 cFYI(1, ("File inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 inode->i_op = &cifs_file_inode_ops;
Steve French8b94bcb2005-11-11 11:41:00 -0800510 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
511 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
512 inode->i_fop =
513 &cifs_file_direct_nobrl_ops;
514 else
515 inode->i_fop = &cifs_file_direct_ops;
516 } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
517 inode->i_fop = &cifs_file_nobrl_ops;
518 else /* not direct, send byte range locks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 inode->i_fop = &cifs_file_ops;
Steve French8b94bcb2005-11-11 11:41:00 -0800520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 inode->i_data.a_ops = &cifs_addr_ops;
Steve Frenchbfa0d752005-08-31 21:50:37 -0700522 if(pTcon->ses->server->maxBuf <
523 4096 + MAX_CIFS_HDR_SIZE)
524 inode->i_data.a_ops->readpages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 } else if (S_ISDIR(inode->i_mode)) {
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800526 cFYI(1, ("Directory inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 inode->i_op = &cifs_dir_inode_ops;
528 inode->i_fop = &cifs_dir_ops;
529 } else if (S_ISLNK(inode->i_mode)) {
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800530 cFYI(1, ("Symbolic Link inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 inode->i_op = &cifs_symlink_inode_ops;
532 } else {
533 init_special_inode(inode, inode->i_mode,
534 inode->i_rdev);
535 }
536 }
537 kfree(buf);
538 return rc;
539}
540
541/* gets root inode */
542void cifs_read_inode(struct inode *inode)
543{
544 int xid;
545 struct cifs_sb_info *cifs_sb;
546
547 cifs_sb = CIFS_SB(inode->i_sb);
548 xid = GetXid();
549 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)
550 cifs_get_inode_info_unix(&inode, "", inode->i_sb,xid);
551 else
552 cifs_get_inode_info(&inode, "", NULL, inode->i_sb,xid);
553 /* can not call macro FreeXid here since in a void func */
554 _FreeXid(xid);
555}
556
557int cifs_unlink(struct inode *inode, struct dentry *direntry)
558{
559 int rc = 0;
560 int xid;
561 struct cifs_sb_info *cifs_sb;
562 struct cifsTconInfo *pTcon;
563 char *full_path = NULL;
564 struct cifsInodeInfo *cifsInode;
565 FILE_BASIC_INFO *pinfo_buf;
566
Steve French3020a1f2005-11-18 11:31:10 -0800567 cFYI(1, ("cifs_unlink, inode = 0x%p with ", inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 xid = GetXid();
570
571 cifs_sb = CIFS_SB(inode->i_sb);
572 pTcon = cifs_sb->tcon;
573
574 /* Unlink can be called from rename so we can not grab the sem here
575 since we deadlock otherwise */
576/* down(&direntry->d_sb->s_vfs_rename_sem);*/
Steve French7f573562005-08-30 11:32:14 -0700577 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578/* up(&direntry->d_sb->s_vfs_rename_sem);*/
579 if (full_path == NULL) {
580 FreeXid(xid);
581 return -ENOMEM;
582 }
Steve French737b7582005-04-28 22:41:06 -0700583 rc = CIFSSMBDelFile(xid, pTcon, full_path, cifs_sb->local_nls,
584 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 if (!rc) {
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700587 if (direntry->d_inode)
Steve Frenchb2aeb9d2005-05-17 13:16:18 -0500588 direntry->d_inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 } else if (rc == -ENOENT) {
590 d_drop(direntry);
591 } else if (rc == -ETXTBSY) {
592 int oplock = FALSE;
593 __u16 netfid;
594
595 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, DELETE,
596 CREATE_NOT_DIR | CREATE_DELETE_ON_CLOSE,
Steve French737b7582005-04-28 22:41:06 -0700597 &netfid, &oplock, NULL, cifs_sb->local_nls,
598 cifs_sb->mnt_cifs_flags &
599 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (rc==0) {
601 CIFSSMBRenameOpenFile(xid, pTcon, netfid, NULL,
Steve French737b7582005-04-28 22:41:06 -0700602 cifs_sb->local_nls,
603 cifs_sb->mnt_cifs_flags &
604 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 CIFSSMBClose(xid, pTcon, netfid);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700606 if (direntry->d_inode)
Steve Frenchb2aeb9d2005-05-17 13:16:18 -0500607 direntry->d_inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609 } else if (rc == -EACCES) {
610 /* try only if r/o attribute set in local lookup data? */
611 pinfo_buf = kmalloc(sizeof(FILE_BASIC_INFO), GFP_KERNEL);
612 if (pinfo_buf) {
613 memset(pinfo_buf, 0, sizeof(FILE_BASIC_INFO));
614 /* ATTRS set to normal clears r/o bit */
615 pinfo_buf->Attributes = cpu_to_le32(ATTR_NORMAL);
616 if (!(pTcon->ses->flags & CIFS_SES_NT4))
617 rc = CIFSSMBSetTimes(xid, pTcon, full_path,
618 pinfo_buf,
Steve French737b7582005-04-28 22:41:06 -0700619 cifs_sb->local_nls,
620 cifs_sb->mnt_cifs_flags &
621 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 else
623 rc = -EOPNOTSUPP;
624
625 if (rc == -EOPNOTSUPP) {
626 int oplock = FALSE;
627 __u16 netfid;
628 /* rc = CIFSSMBSetAttrLegacy(xid, pTcon,
629 full_path,
630 (__u16)ATTR_NORMAL,
631 cifs_sb->local_nls);
632 For some strange reason it seems that NT4 eats the
633 old setattr call without actually setting the
634 attributes so on to the third attempted workaround
635 */
636
637 /* BB could scan to see if we already have it open
638 and pass in pid of opener to function */
639 rc = CIFSSMBOpen(xid, pTcon, full_path,
640 FILE_OPEN, SYNCHRONIZE |
641 FILE_WRITE_ATTRIBUTES, 0,
642 &netfid, &oplock, NULL,
Steve French737b7582005-04-28 22:41:06 -0700643 cifs_sb->local_nls,
644 cifs_sb->mnt_cifs_flags &
645 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (rc==0) {
647 rc = CIFSSMBSetFileTimes(xid, pTcon,
648 pinfo_buf,
649 netfid);
650 CIFSSMBClose(xid, pTcon, netfid);
651 }
652 }
653 kfree(pinfo_buf);
654 }
655 if (rc==0) {
Steve French737b7582005-04-28 22:41:06 -0700656 rc = CIFSSMBDelFile(xid, pTcon, full_path,
657 cifs_sb->local_nls,
658 cifs_sb->mnt_cifs_flags &
659 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (!rc) {
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700661 if (direntry->d_inode)
Steve Frenchb2aeb9d2005-05-17 13:16:18 -0500662 direntry->d_inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 } else if (rc == -ETXTBSY) {
664 int oplock = FALSE;
665 __u16 netfid;
666
667 rc = CIFSSMBOpen(xid, pTcon, full_path,
668 FILE_OPEN, DELETE,
669 CREATE_NOT_DIR |
670 CREATE_DELETE_ON_CLOSE,
671 &netfid, &oplock, NULL,
Steve French737b7582005-04-28 22:41:06 -0700672 cifs_sb->local_nls,
673 cifs_sb->mnt_cifs_flags &
674 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (rc==0) {
676 CIFSSMBRenameOpenFile(xid, pTcon,
677 netfid, NULL,
Steve French737b7582005-04-28 22:41:06 -0700678 cifs_sb->local_nls,
679 cifs_sb->mnt_cifs_flags &
680 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 CIFSSMBClose(xid, pTcon, netfid);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700682 if (direntry->d_inode)
Steve Frenchb2aeb9d2005-05-17 13:16:18 -0500683 direntry->d_inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685 /* BB if rc = -ETXTBUSY goto the rename logic BB */
686 }
687 }
688 }
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700689 if (direntry->d_inode) {
Steve Frenchb2aeb9d2005-05-17 13:16:18 -0500690 cifsInode = CIFS_I(direntry->d_inode);
691 cifsInode->time = 0; /* will force revalidate to get info
692 when needed */
693 direntry->d_inode->i_ctime = current_fs_time(inode->i_sb);
694 }
695 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 cifsInode = CIFS_I(inode);
697 cifsInode->time = 0; /* force revalidate of dir as well */
698
699 kfree(full_path);
700 FreeXid(xid);
701 return rc;
702}
703
704int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode)
705{
706 int rc = 0;
707 int xid;
708 struct cifs_sb_info *cifs_sb;
709 struct cifsTconInfo *pTcon;
710 char *full_path = NULL;
711 struct inode *newinode = NULL;
712
713 cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p ", mode, inode));
714
715 xid = GetXid();
716
717 cifs_sb = CIFS_SB(inode->i_sb);
718 pTcon = cifs_sb->tcon;
719
720 down(&inode->i_sb->s_vfs_rename_sem);
Steve French7f573562005-08-30 11:32:14 -0700721 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 up(&inode->i_sb->s_vfs_rename_sem);
723 if (full_path == NULL) {
724 FreeXid(xid);
725 return -ENOMEM;
726 }
727 /* BB add setting the equivalent of mode via CreateX w/ACLs */
Steve French737b7582005-04-28 22:41:06 -0700728 rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls,
729 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (rc) {
731 cFYI(1, ("cifs_mkdir returned 0x%x ", rc));
732 d_drop(direntry);
733 } else {
734 inode->i_nlink++;
735 if (pTcon->ses->capabilities & CAP_UNIX)
736 rc = cifs_get_inode_info_unix(&newinode, full_path,
737 inode->i_sb,xid);
738 else
739 rc = cifs_get_inode_info(&newinode, full_path, NULL,
740 inode->i_sb,xid);
741
Steve Frenchb92327f2005-08-22 20:09:43 -0700742 if (pTcon->nocase)
743 direntry->d_op = &cifs_ci_dentry_ops;
744 else
745 direntry->d_op = &cifs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 d_instantiate(direntry, newinode);
747 if (direntry->d_inode)
748 direntry->d_inode->i_nlink = 2;
749 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700750 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
752 mode,
753 (__u64)current->euid,
754 (__u64)current->egid,
755 0 /* dev_t */,
Steve French737b7582005-04-28 22:41:06 -0700756 cifs_sb->local_nls,
757 cifs_sb->mnt_cifs_flags &
758 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 } else {
760 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
761 mode, (__u64)-1,
762 (__u64)-1, 0 /* dev_t */,
Steve French737b7582005-04-28 22:41:06 -0700763 cifs_sb->local_nls,
764 cifs_sb->mnt_cifs_flags &
765 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767 else {
768 /* BB to be implemented via Windows secrty descriptors
769 eg CIFSSMBWinSetPerms(xid, pTcon, full_path, mode,
770 -1, -1, local_nls); */
771 }
772 }
773 kfree(full_path);
774 FreeXid(xid);
775 return rc;
776}
777
778int cifs_rmdir(struct inode *inode, struct dentry *direntry)
779{
780 int rc = 0;
781 int xid;
782 struct cifs_sb_info *cifs_sb;
783 struct cifsTconInfo *pTcon;
784 char *full_path = NULL;
785 struct cifsInodeInfo *cifsInode;
786
Steve French3020a1f2005-11-18 11:31:10 -0800787 cFYI(1, ("cifs_rmdir, inode = 0x%p with ", inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 xid = GetXid();
790
791 cifs_sb = CIFS_SB(inode->i_sb);
792 pTcon = cifs_sb->tcon;
793
794 down(&inode->i_sb->s_vfs_rename_sem);
Steve French7f573562005-08-30 11:32:14 -0700795 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 up(&inode->i_sb->s_vfs_rename_sem);
797 if (full_path == NULL) {
798 FreeXid(xid);
799 return -ENOMEM;
800 }
801
Steve French737b7582005-04-28 22:41:06 -0700802 rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls,
803 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 if (!rc) {
806 inode->i_nlink--;
807 i_size_write(direntry->d_inode,0);
808 direntry->d_inode->i_nlink = 0;
809 }
810
811 cifsInode = CIFS_I(direntry->d_inode);
812 cifsInode->time = 0; /* force revalidate to go get info when
813 needed */
814 direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
815 current_fs_time(inode->i_sb);
816
817 kfree(full_path);
818 FreeXid(xid);
819 return rc;
820}
821
822int cifs_rename(struct inode *source_inode, struct dentry *source_direntry,
823 struct inode *target_inode, struct dentry *target_direntry)
824{
825 char *fromName;
826 char *toName;
827 struct cifs_sb_info *cifs_sb_source;
828 struct cifs_sb_info *cifs_sb_target;
829 struct cifsTconInfo *pTcon;
830 int xid;
831 int rc = 0;
832
833 xid = GetXid();
834
835 cifs_sb_target = CIFS_SB(target_inode->i_sb);
836 cifs_sb_source = CIFS_SB(source_inode->i_sb);
837 pTcon = cifs_sb_source->tcon;
838
839 if (pTcon != cifs_sb_target->tcon) {
840 FreeXid(xid);
841 return -EXDEV; /* BB actually could be allowed if same server,
842 but different share.
843 Might eventually add support for this */
844 }
845
846 /* we already have the rename sem so we do not need to grab it again
847 here to protect the path integrity */
Steve French7f573562005-08-30 11:32:14 -0700848 fromName = build_path_from_dentry(source_direntry);
849 toName = build_path_from_dentry(target_direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if ((fromName == NULL) || (toName == NULL)) {
851 rc = -ENOMEM;
852 goto cifs_rename_exit;
853 }
854
855 rc = CIFSSMBRename(xid, pTcon, fromName, toName,
Steve French737b7582005-04-28 22:41:06 -0700856 cifs_sb_source->local_nls,
857 cifs_sb_source->mnt_cifs_flags &
858 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (rc == -EEXIST) {
860 /* check if they are the same file because rename of hardlinked
861 files is a noop */
862 FILE_UNIX_BASIC_INFO *info_buf_source;
863 FILE_UNIX_BASIC_INFO *info_buf_target;
864
865 info_buf_source =
866 kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
867 if (info_buf_source != NULL) {
868 info_buf_target = info_buf_source + 1;
869 rc = CIFSSMBUnixQPathInfo(xid, pTcon, fromName,
Steve French737b7582005-04-28 22:41:06 -0700870 info_buf_source, cifs_sb_source->local_nls,
871 cifs_sb_source->mnt_cifs_flags &
872 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (rc == 0) {
874 rc = CIFSSMBUnixQPathInfo(xid, pTcon, toName,
875 info_buf_target,
Steve French737b7582005-04-28 22:41:06 -0700876 cifs_sb_target->local_nls,
877 /* remap based on source sb */
878 cifs_sb_source->mnt_cifs_flags &
879 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881 if ((rc == 0) &&
882 (info_buf_source->UniqueId ==
883 info_buf_target->UniqueId)) {
884 /* do not rename since the files are hardlinked which
885 is a noop */
886 } else {
887 /* we either can not tell the files are hardlinked
888 (as with Windows servers) or files are not
889 hardlinked so delete the target manually before
890 renaming to follow POSIX rather than Windows
891 semantics */
892 cifs_unlink(target_inode, target_direntry);
893 rc = CIFSSMBRename(xid, pTcon, fromName,
894 toName,
Steve French737b7582005-04-28 22:41:06 -0700895 cifs_sb_source->local_nls,
896 cifs_sb_source->mnt_cifs_flags
897 & CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899 kfree(info_buf_source);
900 } /* if we can not get memory just leave rc as EEXIST */
901 }
902
903 if (rc) {
904 cFYI(1, ("rename rc %d", rc));
905 }
906
907 if ((rc == -EIO) || (rc == -EEXIST)) {
908 int oplock = FALSE;
909 __u16 netfid;
910
911 /* BB FIXME Is Generic Read correct for rename? */
912 /* if renaming directory - we should not say CREATE_NOT_DIR,
913 need to test renaming open directory, also GENERIC_READ
914 might not right be right access to request */
915 rc = CIFSSMBOpen(xid, pTcon, fromName, FILE_OPEN, GENERIC_READ,
916 CREATE_NOT_DIR, &netfid, &oplock, NULL,
Steve French737b7582005-04-28 22:41:06 -0700917 cifs_sb_source->local_nls,
918 cifs_sb_source->mnt_cifs_flags &
919 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (rc==0) {
921 CIFSSMBRenameOpenFile(xid, pTcon, netfid, toName,
Steve French737b7582005-04-28 22:41:06 -0700922 cifs_sb_source->local_nls,
923 cifs_sb_source->mnt_cifs_flags &
924 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 CIFSSMBClose(xid, pTcon, netfid);
926 }
927 }
928
929cifs_rename_exit:
930 kfree(fromName);
931 kfree(toName);
932 FreeXid(xid);
933 return rc;
934}
935
936int cifs_revalidate(struct dentry *direntry)
937{
938 int xid;
939 int rc = 0;
940 char *full_path;
941 struct cifs_sb_info *cifs_sb;
942 struct cifsInodeInfo *cifsInode;
943 loff_t local_size;
944 struct timespec local_mtime;
945 int invalidate_inode = FALSE;
946
947 if (direntry->d_inode == NULL)
948 return -ENOENT;
949
950 cifsInode = CIFS_I(direntry->d_inode);
951
952 if (cifsInode == NULL)
953 return -ENOENT;
954
955 /* no sense revalidating inode info on file that no one can write */
956 if (CIFS_I(direntry->d_inode)->clientCanCacheRead)
957 return rc;
958
959 xid = GetXid();
960
961 cifs_sb = CIFS_SB(direntry->d_sb);
962
963 /* can not safely grab the rename sem here if rename calls revalidate
964 since that would deadlock */
Steve French7f573562005-08-30 11:32:14 -0700965 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (full_path == NULL) {
967 FreeXid(xid);
968 return -ENOMEM;
969 }
970 cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld "
971 "jiffies %ld", full_path, direntry->d_inode,
972 direntry->d_inode->i_count.counter, direntry,
973 direntry->d_time, jiffies));
974
975 if (cifsInode->time == 0) {
976 /* was set to zero previously to force revalidate */
977 } else if (time_before(jiffies, cifsInode->time + HZ) &&
978 lookupCacheEnabled) {
979 if ((S_ISREG(direntry->d_inode->i_mode) == 0) ||
980 (direntry->d_inode->i_nlink == 1)) {
981 kfree(full_path);
982 FreeXid(xid);
983 return rc;
984 } else {
985 cFYI(1, ("Have to revalidate file due to hardlinks"));
986 }
987 }
988
989 /* save mtime and size */
990 local_mtime = direntry->d_inode->i_mtime;
991 local_size = direntry->d_inode->i_size;
992
993 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX) {
994 rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path,
995 direntry->d_sb,xid);
996 if (rc) {
997 cFYI(1, ("error on getting revalidate info %d", rc));
998/* if (rc != -ENOENT)
999 rc = 0; */ /* BB should we cache info on
1000 certain errors? */
1001 }
1002 } else {
1003 rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL,
1004 direntry->d_sb,xid);
1005 if (rc) {
1006 cFYI(1, ("error on getting revalidate info %d", rc));
1007/* if (rc != -ENOENT)
1008 rc = 0; */ /* BB should we cache info on
1009 certain errors? */
1010 }
1011 }
1012 /* should we remap certain errors, access denied?, to zero */
1013
1014 /* if not oplocked, we invalidate inode pages if mtime or file size
1015 had changed on server */
1016
1017 if (timespec_equal(&local_mtime,&direntry->d_inode->i_mtime) &&
1018 (local_size == direntry->d_inode->i_size)) {
1019 cFYI(1, ("cifs_revalidate - inode unchanged"));
1020 } else {
1021 /* file may have changed on server */
1022 if (cifsInode->clientCanCacheRead) {
1023 /* no need to invalidate inode pages since we were the
1024 only ones who could have modified the file and the
1025 server copy is staler than ours */
1026 } else {
1027 invalidate_inode = TRUE;
1028 }
1029 }
1030
1031 /* can not grab this sem since kernel filesys locking documentation
1032 indicates i_sem may be taken by the kernel on lookup and rename
1033 which could deadlock if we grab the i_sem here as well */
1034/* down(&direntry->d_inode->i_sem);*/
1035 /* need to write out dirty pages here */
1036 if (direntry->d_inode->i_mapping) {
1037 /* do we need to lock inode until after invalidate completes
1038 below? */
1039 filemap_fdatawrite(direntry->d_inode->i_mapping);
1040 }
1041 if (invalidate_inode) {
Steve French3abb9272005-11-28 08:16:13 -08001042 /* shrink_dcache not necessary now that cifs dentry ops
1043 are exported for negative dentries */
1044/* if(S_ISDIR(direntry->d_inode->i_mode))
1045 shrink_dcache_parent(direntry); */
1046 if (S_ISREG(direntry->d_inode->i_mode)) {
1047 if (direntry->d_inode->i_mapping)
1048 filemap_fdatawait(direntry->d_inode->i_mapping);
1049 /* may eventually have to do this for open files too */
1050 if (list_empty(&(cifsInode->openFileList))) {
1051 /* changed on server - flush read ahead pages */
1052 cFYI(1, ("Invalidating read ahead data on "
1053 "closed file"));
1054 invalidate_remote_inode(direntry->d_inode);
1055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057 }
1058/* up(&direntry->d_inode->i_sem); */
1059
1060 kfree(full_path);
1061 FreeXid(xid);
1062 return rc;
1063}
1064
1065int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1066 struct kstat *stat)
1067{
1068 int err = cifs_revalidate(dentry);
1069 if (!err)
1070 generic_fillattr(dentry->d_inode, stat);
1071 return err;
1072}
1073
1074static int cifs_truncate_page(struct address_space *mapping, loff_t from)
1075{
1076 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1077 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
1078 struct page *page;
1079 char *kaddr;
1080 int rc = 0;
1081
1082 page = grab_cache_page(mapping, index);
1083 if (!page)
1084 return -ENOMEM;
1085
1086 kaddr = kmap_atomic(page, KM_USER0);
1087 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1088 flush_dcache_page(page);
1089 kunmap_atomic(kaddr, KM_USER0);
1090 unlock_page(page);
1091 page_cache_release(page);
1092 return rc;
1093}
1094
1095int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
1096{
1097 int xid;
1098 struct cifs_sb_info *cifs_sb;
1099 struct cifsTconInfo *pTcon;
1100 char *full_path = NULL;
1101 int rc = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 struct cifsFileInfo *open_file = NULL;
1103 FILE_BASIC_INFO time_buf;
1104 int set_time = FALSE;
1105 __u64 mode = 0xFFFFFFFFFFFFFFFFULL;
1106 __u64 uid = 0xFFFFFFFFFFFFFFFFULL;
1107 __u64 gid = 0xFFFFFFFFFFFFFFFFULL;
1108 struct cifsInodeInfo *cifsInode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 xid = GetXid();
1111
Steve French3020a1f2005-11-18 11:31:10 -08001112 cFYI(1, ("In cifs_setattr, name = %s attrs->iavalid 0x%x ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 direntry->d_name.name, attrs->ia_valid));
1114 cifs_sb = CIFS_SB(direntry->d_inode->i_sb);
1115 pTcon = cifs_sb->tcon;
1116
1117 down(&direntry->d_sb->s_vfs_rename_sem);
Steve French7f573562005-08-30 11:32:14 -07001118 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 up(&direntry->d_sb->s_vfs_rename_sem);
1120 if (full_path == NULL) {
1121 FreeXid(xid);
1122 return -ENOMEM;
1123 }
1124 cifsInode = CIFS_I(direntry->d_inode);
1125
1126 /* BB check if we need to refresh inode from server now ? BB */
1127
1128 /* need to flush data before changing file size on server */
1129 filemap_fdatawrite(direntry->d_inode->i_mapping);
1130 filemap_fdatawait(direntry->d_inode->i_mapping);
1131
1132 if (attrs->ia_valid & ATTR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 /* To avoid spurious oplock breaks from server, in the case of
1134 inodes that we already have open, avoid doing path based
1135 setting of file size if we can do it by handle.
1136 This keeps our caching token (oplock) and avoids timeouts
1137 when the local oplock break takes longer to flush
1138 writebehind data than the SMB timeout for the SetPathInfo
1139 request would allow */
Steve French6148a742005-10-05 12:23:19 -07001140 open_file = find_writable_file(cifsInode);
1141 if (open_file) {
1142 __u16 nfid = open_file->netfid;
1143 __u32 npid = open_file->pid;
1144 rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size,
1145 nfid, npid, FALSE);
Steve French23e7dd72005-10-20 13:44:56 -07001146 atomic_dec(&open_file->wrtPending);
Steve French6148a742005-10-05 12:23:19 -07001147 cFYI(1,("SetFSize for attrs rc = %d", rc));
1148 if(rc == -EINVAL) {
1149 int bytes_written;
1150 rc = CIFSSMBWrite(xid, pTcon,
1151 nfid, 0, attrs->ia_size,
1152 &bytes_written, NULL, NULL,
1153 1 /* 45 seconds */);
1154 cFYI(1,("Wrt seteof rc %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (rc != 0) {
1158 /* Set file size by pathname rather than by handle
1159 either because no valid, writeable file handle for
1160 it was found or because there was an error setting
1161 it by handle */
1162 rc = CIFSSMBSetEOF(xid, pTcon, full_path,
1163 attrs->ia_size, FALSE,
Steve French737b7582005-04-28 22:41:06 -07001164 cifs_sb->local_nls,
1165 cifs_sb->mnt_cifs_flags &
1166 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve Frenche30dcf32005-09-20 20:49:16 -07001167 cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc));
1168 if(rc == -EINVAL) {
1169 __u16 netfid;
1170 int oplock = FALSE;
1171
1172 rc = SMBLegacyOpen(xid, pTcon, full_path,
1173 FILE_OPEN,
1174 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1175 CREATE_NOT_DIR, &netfid, &oplock,
1176 NULL, cifs_sb->local_nls,
1177 cifs_sb->mnt_cifs_flags &
1178 CIFS_MOUNT_MAP_SPECIAL_CHR);
1179 if (rc==0) {
1180 int bytes_written;
1181 rc = CIFSSMBWrite(xid, pTcon,
1182 netfid, 0,
1183 attrs->ia_size,
1184 &bytes_written, NULL,
1185 NULL, 1 /* 45 sec */);
1186 cFYI(1,("wrt seteof rc %d",rc));
1187 CIFSSMBClose(xid, pTcon, netfid);
1188 }
1189
1190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
1192
1193 /* Server is ok setting allocation size implicitly - no need
1194 to call:
1195 CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, TRUE,
1196 cifs_sb->local_nls);
1197 */
1198
1199 if (rc == 0) {
1200 rc = vmtruncate(direntry->d_inode, attrs->ia_size);
1201 cifs_truncate_page(direntry->d_inode->i_mapping,
1202 direntry->d_inode->i_size);
Steve Frenche30dcf32005-09-20 20:49:16 -07001203 } else
1204 goto cifs_setattr_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206 if (attrs->ia_valid & ATTR_UID) {
Steve Frenche30dcf32005-09-20 20:49:16 -07001207 cFYI(1, ("UID changed to %d", attrs->ia_uid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 uid = attrs->ia_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 }
1210 if (attrs->ia_valid & ATTR_GID) {
Steve Frenche30dcf32005-09-20 20:49:16 -07001211 cFYI(1, ("GID changed to %d", attrs->ia_gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 gid = attrs->ia_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214
1215 time_buf.Attributes = 0;
1216 if (attrs->ia_valid & ATTR_MODE) {
Steve Frenche30dcf32005-09-20 20:49:16 -07001217 cFYI(1, ("Mode changed to 0x%x", attrs->ia_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 mode = attrs->ia_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
1220
1221 if ((cifs_sb->tcon->ses->capabilities & CAP_UNIX)
1222 && (attrs->ia_valid & (ATTR_MODE | ATTR_GID | ATTR_UID)))
1223 rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode, uid, gid,
Steve French737b7582005-04-28 22:41:06 -07001224 0 /* dev_t */, cifs_sb->local_nls,
1225 cifs_sb->mnt_cifs_flags &
1226 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 else if (attrs->ia_valid & ATTR_MODE) {
Steve Frenchcdbce9c2005-11-19 21:04:52 -08001228 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if ((mode & S_IWUGO) == 0) /* not writeable */ {
1230 if ((cifsInode->cifsAttrs & ATTR_READONLY) == 0)
1231 time_buf.Attributes =
1232 cpu_to_le32(cifsInode->cifsAttrs |
1233 ATTR_READONLY);
1234 } else if ((mode & S_IWUGO) == S_IWUGO) {
1235 if (cifsInode->cifsAttrs & ATTR_READONLY)
1236 time_buf.Attributes =
1237 cpu_to_le32(cifsInode->cifsAttrs &
1238 (~ATTR_READONLY));
1239 }
1240 /* BB to be implemented -
1241 via Windows security descriptors or streams */
1242 /* CIFSSMBWinSetPerms(xid, pTcon, full_path, mode, uid, gid,
1243 cifs_sb->local_nls); */
1244 }
1245
1246 if (attrs->ia_valid & ATTR_ATIME) {
1247 set_time = TRUE;
1248 time_buf.LastAccessTime =
1249 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1250 } else
1251 time_buf.LastAccessTime = 0;
1252
1253 if (attrs->ia_valid & ATTR_MTIME) {
1254 set_time = TRUE;
1255 time_buf.LastWriteTime =
1256 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1257 } else
1258 time_buf.LastWriteTime = 0;
Steve Frenche30dcf32005-09-20 20:49:16 -07001259 /* Do not set ctime explicitly unless other time
1260 stamps are changed explicitly (i.e. by utime()
1261 since we would then have a mix of client and
1262 server times */
1263
1264 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 set_time = TRUE;
Steve Frenche30dcf32005-09-20 20:49:16 -07001266 /* Although Samba throws this field away
1267 it may be useful to Windows - but we do
1268 not want to set ctime unless some other
1269 timestamp is changing */
1270 cFYI(1, ("CIFS - CTIME changed "));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 time_buf.ChangeTime =
1272 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1273 } else
1274 time_buf.ChangeTime = 0;
1275
1276 if (set_time || time_buf.Attributes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 time_buf.CreationTime = 0; /* do not change */
1278 /* In the future we should experiment - try setting timestamps
1279 via Handle (SetFileInfo) instead of by path */
1280 if (!(pTcon->ses->flags & CIFS_SES_NT4))
1281 rc = CIFSSMBSetTimes(xid, pTcon, full_path, &time_buf,
Steve French737b7582005-04-28 22:41:06 -07001282 cifs_sb->local_nls,
1283 cifs_sb->mnt_cifs_flags &
1284 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 else
1286 rc = -EOPNOTSUPP;
1287
1288 if (rc == -EOPNOTSUPP) {
1289 int oplock = FALSE;
1290 __u16 netfid;
1291
1292 cFYI(1, ("calling SetFileInfo since SetPathInfo for "
1293 "times not supported by this server"));
1294 /* BB we could scan to see if we already have it open
1295 and pass in pid of opener to function */
1296 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN,
1297 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1298 CREATE_NOT_DIR, &netfid, &oplock,
Steve French737b7582005-04-28 22:41:06 -07001299 NULL, cifs_sb->local_nls,
1300 cifs_sb->mnt_cifs_flags &
1301 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (rc==0) {
1303 rc = CIFSSMBSetFileTimes(xid, pTcon, &time_buf,
1304 netfid);
1305 CIFSSMBClose(xid, pTcon, netfid);
1306 } else {
1307 /* BB For even older servers we could convert time_buf
1308 into old DOS style which uses two second
1309 granularity */
1310
1311 /* rc = CIFSSMBSetTimesLegacy(xid, pTcon, full_path,
1312 &time_buf, cifs_sb->local_nls); */
1313 }
1314 }
Steve Frenche30dcf32005-09-20 20:49:16 -07001315 /* Even if error on time set, no sense failing the call if
1316 the server would set the time to a reasonable value anyway,
1317 and this check ensures that we are not being called from
1318 sys_utimes in which case we ought to fail the call back to
1319 the user when the server rejects the call */
1320 if((rc) && (attrs->ia_valid &&
1321 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
1322 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
1324
1325 /* do not need local check to inode_check_ok since the server does
1326 that */
1327 if (!rc)
1328 rc = inode_setattr(direntry->d_inode, attrs);
Steve Frenche30dcf32005-09-20 20:49:16 -07001329cifs_setattr_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 kfree(full_path);
1331 FreeXid(xid);
1332 return rc;
1333}
1334
1335void cifs_delete_inode(struct inode *inode)
1336{
1337 cFYI(1, ("In cifs_delete_inode, inode = 0x%p ", inode));
1338 /* may have to add back in if and when safe distributed caching of
1339 directories added e.g. via FindNotify */
1340}