blob: f65310cc60a187e9384ebcf1b7ac183de641a5b6 [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) {
Steve Frenchec637e32005-12-12 20:53:18 -0800232 int buf_type = CIFS_NO_BUFFER;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800233 /* Read header */
234 rc = CIFSSMBRead(xid, pTcon,
235 netfid,
Steve French86c96b42005-11-18 20:25:31 -0800236 24 /* length */, 0 /* offset */,
Steve Frenchec637e32005-12-12 20:53:18 -0800237 &bytes_read, &pbuf, &buf_type);
Steve French86c96b42005-11-18 20:25:31 -0800238 if((rc == 0) && (bytes_read >= 8)) {
Steve French9e294f12005-11-17 16:59:21 -0800239 if(memcmp("IntxBLK", pbuf, 8) == 0) {
240 cFYI(1,("Block device"));
Steve French3020a1f2005-11-18 11:31:10 -0800241 inode->i_mode |= S_IFBLK;
Steve French86c96b42005-11-18 20:25:31 -0800242 if(bytes_read == 24) {
243 /* we have enough to decode dev num */
244 __u64 mjr; /* major */
245 __u64 mnr; /* minor */
246 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
247 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
248 inode->i_rdev = MKDEV(mjr, mnr);
249 }
Steve French9e294f12005-11-17 16:59:21 -0800250 } else if(memcmp("IntxCHR", pbuf, 8) == 0) {
251 cFYI(1,("Char device"));
Steve French3020a1f2005-11-18 11:31:10 -0800252 inode->i_mode |= S_IFCHR;
Steve French86c96b42005-11-18 20:25:31 -0800253 if(bytes_read == 24) {
254 /* we have enough to decode dev num */
255 __u64 mjr; /* major */
256 __u64 mnr; /* minor */
257 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
258 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
259 inode->i_rdev = MKDEV(mjr, mnr);
260 }
Steve French9e294f12005-11-17 16:59:21 -0800261 } else if(memcmp("IntxLNK", pbuf, 7) == 0) {
262 cFYI(1,("Symlink"));
Steve French3020a1f2005-11-18 11:31:10 -0800263 inode->i_mode |= S_IFLNK;
Steve French86c96b42005-11-18 20:25:31 -0800264 } else {
265 inode->i_mode |= S_IFREG; /* file? */
266 rc = -EOPNOTSUPP;
267 }
Steve French3020a1f2005-11-18 11:31:10 -0800268 } else {
269 inode->i_mode |= S_IFREG; /* then it is a file */
270 rc = -EOPNOTSUPP; /* or some unknown SFU type */
Steve Frenchec637e32005-12-12 20:53:18 -0800271 }
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800272 CIFSSMBClose(xid, pTcon, netfid);
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800273 }
274 return rc;
275
276}
277
Steve French9e294f12005-11-17 16:59:21 -0800278#define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
279
280static int get_sfu_uid_mode(struct inode * inode,
281 const unsigned char *path,
282 struct cifs_sb_info *cifs_sb, int xid)
283{
Steve French3020a1f2005-11-18 11:31:10 -0800284#ifdef CONFIG_CIFS_XATTR
Steve French9e294f12005-11-17 16:59:21 -0800285 ssize_t rc;
286 char ea_value[4];
287 __u32 mode;
288
289 rc = CIFSSMBQueryEA(xid, cifs_sb->tcon, path, "SETFILEBITS",
290 ea_value, 4 /* size of buf */, cifs_sb->local_nls,
291 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
292 if(rc < 0)
293 return (int)rc;
294 else if (rc > 3) {
295 mode = le32_to_cpu(*((__le32 *)ea_value));
Steve Frenchc119b872005-11-18 12:27:27 -0800296 inode->i_mode &= ~SFBITS_MASK;
Steve French3020a1f2005-11-18 11:31:10 -0800297 cFYI(1,("special bits 0%o org mode 0%o", mode, inode->i_mode));
Steve French9e294f12005-11-17 16:59:21 -0800298 inode->i_mode = (mode & SFBITS_MASK) | inode->i_mode;
299 cFYI(1,("special mode bits 0%o", mode));
300 return 0;
301 } else {
302 return 0;
303 }
Steve French3020a1f2005-11-18 11:31:10 -0800304#else
305 return -EOPNOTSUPP;
306#endif
307
Steve French9e294f12005-11-17 16:59:21 -0800308
309}
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311int cifs_get_inode_info(struct inode **pinode,
312 const unsigned char *search_path, FILE_ALL_INFO *pfindData,
313 struct super_block *sb, int xid)
314{
315 int rc = 0;
316 struct cifsTconInfo *pTcon;
317 struct inode *inode;
318 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
319 char *tmp_path;
320 char *buf = NULL;
321
322 pTcon = cifs_sb->tcon;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800323 cFYI(1,("Getting info on %s", search_path));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700325 if ((pfindData == NULL) && (*pinode != NULL)) {
326 if (CIFS_I(*pinode)->clientCanCacheRead) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 cFYI(1,("No need to revalidate cached inode sizes"));
328 return rc;
329 }
330 }
331
332 /* if file info not passed in then get it from server */
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700333 if (pfindData == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700335 if (buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return -ENOMEM;
337 pfindData = (FILE_ALL_INFO *)buf;
338 /* could do find first instead but this returns more info */
339 rc = CIFSSMBQPathInfo(xid, pTcon, search_path, pfindData,
Steve French6b8edfe2005-08-23 20:26:03 -0700340 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
Steve French737b7582005-04-28 22:41:06 -0700341 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve French6b8edfe2005-08-23 20:26:03 -0700342 /* BB optimize code so we do not make the above call
343 when server claims no NT SMB support and the above call
344 failed at least once - set flag in tcon or mount */
345 if((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
346 rc = SMBQueryInformation(xid, pTcon, search_path,
347 pfindData, cifs_sb->local_nls,
348 cifs_sb->mnt_cifs_flags &
349 CIFS_MOUNT_MAP_SPECIAL_CHR);
350 }
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353 /* dump_mem("\nQPathInfo return data",&findData, sizeof(findData)); */
354 if (rc) {
355 if (rc == -EREMOTE) {
356 tmp_path =
357 kmalloc(strnlen
358 (pTcon->treeName,
359 MAX_TREE_SIZE + 1) +
360 strnlen(search_path, MAX_PATHCONF) + 1,
361 GFP_KERNEL);
362 if (tmp_path == NULL) {
363 kfree(buf);
364 return -ENOMEM;
365 }
366
367 strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE);
368 strncat(tmp_path, search_path, MAX_PATHCONF);
369 rc = connect_to_dfs_path(xid, pTcon->ses,
370 /* treename + */ tmp_path,
Steve French737b7582005-04-28 22:41:06 -0700371 cifs_sb->local_nls,
372 cifs_sb->mnt_cifs_flags &
373 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 kfree(tmp_path);
375 /* BB fix up inode etc. */
376 } else if (rc) {
377 kfree(buf);
378 return rc;
379 }
380 } else {
381 struct cifsInodeInfo *cifsInfo;
382 __u32 attr = le32_to_cpu(pfindData->Attributes);
383
384 /* get new inode */
385 if (*pinode == NULL) {
386 *pinode = new_inode(sb);
387 if (*pinode == NULL)
388 return -ENOMEM;
389 /* Is an i_ino of zero legal? Can we use that to check
390 if the server supports returning inode numbers? Are
391 there other sanity checks we can use to ensure that
392 the server is really filling in that field? */
393
394 /* We can not use the IndexNumber field by default from
395 Windows or Samba (in ALL_INFO buf) but we can request
396 it explicitly. It may not be unique presumably if
397 the server has multiple devices mounted under one
398 share */
399
400 /* There may be higher info levels that work but are
401 there Windows server or network appliances for which
402 IndexNumber field is not guaranteed unique? */
403
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700404 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 int rc1 = 0;
406 __u64 inode_num;
407
408 rc1 = CIFSGetSrvInodeNumber(xid, pTcon,
409 search_path, &inode_num,
Steve French737b7582005-04-28 22:41:06 -0700410 cifs_sb->local_nls,
411 cifs_sb->mnt_cifs_flags &
412 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700413 if (rc1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 cFYI(1,("GetSrvInodeNum rc %d", rc1));
415 /* BB EOPNOSUPP disable SERVER_INUM? */
416 } else /* do we need cast or hash to ino? */
417 (*pinode)->i_ino = inode_num;
418 } /* else ino incremented to unique num in new_inode*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 insert_inode_hash(*pinode);
420 }
421 inode = *pinode;
422 cifsInfo = CIFS_I(inode);
423 cifsInfo->cifsAttrs = attr;
Steve French3020a1f2005-11-18 11:31:10 -0800424 cFYI(1, ("Old time %ld ", cifsInfo->time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 cifsInfo->time = jiffies;
Steve French3020a1f2005-11-18 11:31:10 -0800426 cFYI(1, ("New time %ld ", cifsInfo->time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 /* blksize needs to be multiple of two. So safer to default to
429 blksize and blkbits set in superblock so 2**blkbits and blksize
430 will match rather than setting to:
431 (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
432
433 /* Linux can not store file creation time unfortunately so we ignore it */
434 inode->i_atime =
435 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
436 inode->i_mtime =
437 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
438 inode->i_ctime =
439 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
Steve French3020a1f2005-11-18 11:31:10 -0800440 cFYI(0, ("Attributes came in as 0x%x ", attr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 /* set default mode. will override for dirs below */
443 if (atomic_read(&cifsInfo->inUse) == 0)
444 /* new inode, can safely set these fields */
445 inode->i_mode = cifs_sb->mnt_file_mode;
Steve French3020a1f2005-11-18 11:31:10 -0800446 else /* since we set the inode type below we need to mask off
447 to avoid strange results if type changes and both get orred in */
448 inode->i_mode &= ~S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449/* if (attr & ATTR_REPARSE) */
450 /* We no longer handle these as symlinks because we could not
451 follow them due to the absolute path with drive letter */
452 if (attr & ATTR_DIRECTORY) {
453 /* override default perms since we do not do byte range locking
454 on dirs */
455 inode->i_mode = cifs_sb->mnt_dir_mode;
456 inode->i_mode |= S_IFDIR;
Steve Frencheda3c0292005-07-21 15:20:28 -0700457 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
458 (cifsInfo->cifsAttrs & ATTR_SYSTEM) &&
459 /* No need to le64 convert size of zero */
460 (pfindData->EndOfFile == 0)) {
461 inode->i_mode = cifs_sb->mnt_file_mode;
462 inode->i_mode |= S_IFIFO;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800463/* BB Finish for SFU style symlinks and devices */
464 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
465 (cifsInfo->cifsAttrs & ATTR_SYSTEM)) {
466 if (decode_sfu_inode(inode,
467 le64_to_cpu(pfindData->EndOfFile),
468 search_path,
469 cifs_sb, xid)) {
470 cFYI(1,("Unrecognized sfu inode type"));
471 }
Steve French3020a1f2005-11-18 11:31:10 -0800472 cFYI(1,("sfu mode 0%o",inode->i_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 } else {
474 inode->i_mode |= S_IFREG;
475 /* treat the dos attribute of read-only as read-only
476 mode e.g. 555 */
477 if (cifsInfo->cifsAttrs & ATTR_READONLY)
478 inode->i_mode &= ~(S_IWUGO);
479 /* BB add code here -
480 validate if device or weird share or device type? */
481 }
482 if (is_size_safe_to_change(cifsInfo)) {
483 /* can not safely change the file size here if the
484 client is writing to it due to potential races */
485 i_size_write(inode,le64_to_cpu(pfindData->EndOfFile));
486
487 /* 512 bytes (2**9) is the fake blocksize that must be
488 used for this calculation */
489 inode->i_blocks = (512 - 1 + le64_to_cpu(
490 pfindData->AllocationSize)) >> 9;
491 }
492
493 inode->i_nlink = le32_to_cpu(pfindData->NumberOfLinks);
494
495 /* BB fill in uid and gid here? with help from winbind?
496 or retrieve from NTFS stream extended attribute */
Steve French9e294f12005-11-17 16:59:21 -0800497 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
498 /* fill in uid, gid, mode from server ACL */
499 get_sfu_uid_mode(inode, search_path, cifs_sb, xid);
500 } else if (atomic_read(&cifsInfo->inUse) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 inode->i_uid = cifs_sb->mnt_uid;
502 inode->i_gid = cifs_sb->mnt_gid;
503 /* set so we do not keep refreshing these fields with
504 bad data after user has changed them in memory */
505 atomic_set(&cifsInfo->inUse,1);
506 }
507
508 if (S_ISREG(inode->i_mode)) {
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800509 cFYI(1, ("File inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 inode->i_op = &cifs_file_inode_ops;
Steve French8b94bcb2005-11-11 11:41:00 -0800511 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
512 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
513 inode->i_fop =
514 &cifs_file_direct_nobrl_ops;
515 else
516 inode->i_fop = &cifs_file_direct_ops;
517 } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
518 inode->i_fop = &cifs_file_nobrl_ops;
519 else /* not direct, send byte range locks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 inode->i_fop = &cifs_file_ops;
Steve French8b94bcb2005-11-11 11:41:00 -0800521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 inode->i_data.a_ops = &cifs_addr_ops;
Steve Frenchbfa0d752005-08-31 21:50:37 -0700523 if(pTcon->ses->server->maxBuf <
524 4096 + MAX_CIFS_HDR_SIZE)
525 inode->i_data.a_ops->readpages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 } else if (S_ISDIR(inode->i_mode)) {
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800527 cFYI(1, ("Directory inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 inode->i_op = &cifs_dir_inode_ops;
529 inode->i_fop = &cifs_dir_ops;
530 } else if (S_ISLNK(inode->i_mode)) {
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800531 cFYI(1, ("Symbolic Link inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 inode->i_op = &cifs_symlink_inode_ops;
533 } else {
534 init_special_inode(inode, inode->i_mode,
535 inode->i_rdev);
536 }
537 }
538 kfree(buf);
539 return rc;
540}
541
542/* gets root inode */
543void cifs_read_inode(struct inode *inode)
544{
545 int xid;
546 struct cifs_sb_info *cifs_sb;
547
548 cifs_sb = CIFS_SB(inode->i_sb);
549 xid = GetXid();
550 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)
551 cifs_get_inode_info_unix(&inode, "", inode->i_sb,xid);
552 else
553 cifs_get_inode_info(&inode, "", NULL, inode->i_sb,xid);
554 /* can not call macro FreeXid here since in a void func */
555 _FreeXid(xid);
556}
557
558int cifs_unlink(struct inode *inode, struct dentry *direntry)
559{
560 int rc = 0;
561 int xid;
562 struct cifs_sb_info *cifs_sb;
563 struct cifsTconInfo *pTcon;
564 char *full_path = NULL;
565 struct cifsInodeInfo *cifsInode;
566 FILE_BASIC_INFO *pinfo_buf;
567
Steve French3020a1f2005-11-18 11:31:10 -0800568 cFYI(1, ("cifs_unlink, inode = 0x%p with ", inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 xid = GetXid();
571
572 cifs_sb = CIFS_SB(inode->i_sb);
573 pTcon = cifs_sb->tcon;
574
575 /* Unlink can be called from rename so we can not grab the sem here
576 since we deadlock otherwise */
577/* down(&direntry->d_sb->s_vfs_rename_sem);*/
Steve French7f573562005-08-30 11:32:14 -0700578 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579/* up(&direntry->d_sb->s_vfs_rename_sem);*/
580 if (full_path == NULL) {
581 FreeXid(xid);
582 return -ENOMEM;
583 }
Steve French737b7582005-04-28 22:41:06 -0700584 rc = CIFSSMBDelFile(xid, pTcon, full_path, cifs_sb->local_nls,
585 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 if (!rc) {
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700588 if (direntry->d_inode)
Steve Frenchb2aeb9d2005-05-17 13:16:18 -0500589 direntry->d_inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 } else if (rc == -ENOENT) {
591 d_drop(direntry);
592 } else if (rc == -ETXTBSY) {
593 int oplock = FALSE;
594 __u16 netfid;
595
596 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, DELETE,
597 CREATE_NOT_DIR | CREATE_DELETE_ON_CLOSE,
Steve French737b7582005-04-28 22:41:06 -0700598 &netfid, &oplock, NULL, cifs_sb->local_nls,
599 cifs_sb->mnt_cifs_flags &
600 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (rc==0) {
602 CIFSSMBRenameOpenFile(xid, pTcon, netfid, NULL,
Steve French737b7582005-04-28 22:41:06 -0700603 cifs_sb->local_nls,
604 cifs_sb->mnt_cifs_flags &
605 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 CIFSSMBClose(xid, pTcon, netfid);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700607 if (direntry->d_inode)
Steve Frenchb2aeb9d2005-05-17 13:16:18 -0500608 direntry->d_inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610 } else if (rc == -EACCES) {
611 /* try only if r/o attribute set in local lookup data? */
612 pinfo_buf = kmalloc(sizeof(FILE_BASIC_INFO), GFP_KERNEL);
613 if (pinfo_buf) {
614 memset(pinfo_buf, 0, sizeof(FILE_BASIC_INFO));
615 /* ATTRS set to normal clears r/o bit */
616 pinfo_buf->Attributes = cpu_to_le32(ATTR_NORMAL);
617 if (!(pTcon->ses->flags & CIFS_SES_NT4))
618 rc = CIFSSMBSetTimes(xid, pTcon, full_path,
619 pinfo_buf,
Steve French737b7582005-04-28 22:41:06 -0700620 cifs_sb->local_nls,
621 cifs_sb->mnt_cifs_flags &
622 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 else
624 rc = -EOPNOTSUPP;
625
626 if (rc == -EOPNOTSUPP) {
627 int oplock = FALSE;
628 __u16 netfid;
629 /* rc = CIFSSMBSetAttrLegacy(xid, pTcon,
630 full_path,
631 (__u16)ATTR_NORMAL,
632 cifs_sb->local_nls);
633 For some strange reason it seems that NT4 eats the
634 old setattr call without actually setting the
635 attributes so on to the third attempted workaround
636 */
637
638 /* BB could scan to see if we already have it open
639 and pass in pid of opener to function */
640 rc = CIFSSMBOpen(xid, pTcon, full_path,
641 FILE_OPEN, SYNCHRONIZE |
642 FILE_WRITE_ATTRIBUTES, 0,
643 &netfid, &oplock, NULL,
Steve French737b7582005-04-28 22:41:06 -0700644 cifs_sb->local_nls,
645 cifs_sb->mnt_cifs_flags &
646 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (rc==0) {
648 rc = CIFSSMBSetFileTimes(xid, pTcon,
649 pinfo_buf,
650 netfid);
651 CIFSSMBClose(xid, pTcon, netfid);
652 }
653 }
654 kfree(pinfo_buf);
655 }
656 if (rc==0) {
Steve French737b7582005-04-28 22:41:06 -0700657 rc = CIFSSMBDelFile(xid, pTcon, full_path,
658 cifs_sb->local_nls,
659 cifs_sb->mnt_cifs_flags &
660 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (!rc) {
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700662 if (direntry->d_inode)
Steve Frenchb2aeb9d2005-05-17 13:16:18 -0500663 direntry->d_inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 } else if (rc == -ETXTBSY) {
665 int oplock = FALSE;
666 __u16 netfid;
667
668 rc = CIFSSMBOpen(xid, pTcon, full_path,
669 FILE_OPEN, DELETE,
670 CREATE_NOT_DIR |
671 CREATE_DELETE_ON_CLOSE,
672 &netfid, &oplock, NULL,
Steve French737b7582005-04-28 22:41:06 -0700673 cifs_sb->local_nls,
674 cifs_sb->mnt_cifs_flags &
675 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (rc==0) {
677 CIFSSMBRenameOpenFile(xid, pTcon,
678 netfid, NULL,
Steve French737b7582005-04-28 22:41:06 -0700679 cifs_sb->local_nls,
680 cifs_sb->mnt_cifs_flags &
681 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 CIFSSMBClose(xid, pTcon, netfid);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700683 if (direntry->d_inode)
Steve Frenchb2aeb9d2005-05-17 13:16:18 -0500684 direntry->d_inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686 /* BB if rc = -ETXTBUSY goto the rename logic BB */
687 }
688 }
689 }
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700690 if (direntry->d_inode) {
Steve Frenchb2aeb9d2005-05-17 13:16:18 -0500691 cifsInode = CIFS_I(direntry->d_inode);
692 cifsInode->time = 0; /* will force revalidate to get info
693 when needed */
694 direntry->d_inode->i_ctime = current_fs_time(inode->i_sb);
695 }
696 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 cifsInode = CIFS_I(inode);
698 cifsInode->time = 0; /* force revalidate of dir as well */
699
700 kfree(full_path);
701 FreeXid(xid);
702 return rc;
703}
704
705int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode)
706{
707 int rc = 0;
708 int xid;
709 struct cifs_sb_info *cifs_sb;
710 struct cifsTconInfo *pTcon;
711 char *full_path = NULL;
712 struct inode *newinode = NULL;
713
Steve French6473a552005-11-29 20:20:10 -0800714 cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p", mode, inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 xid = GetXid();
717
718 cifs_sb = CIFS_SB(inode->i_sb);
719 pTcon = cifs_sb->tcon;
720
721 down(&inode->i_sb->s_vfs_rename_sem);
Steve French7f573562005-08-30 11:32:14 -0700722 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 up(&inode->i_sb->s_vfs_rename_sem);
724 if (full_path == NULL) {
725 FreeXid(xid);
726 return -ENOMEM;
727 }
728 /* BB add setting the equivalent of mode via CreateX w/ACLs */
Steve French737b7582005-04-28 22:41:06 -0700729 rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls,
730 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (rc) {
732 cFYI(1, ("cifs_mkdir returned 0x%x ", rc));
733 d_drop(direntry);
734 } else {
735 inode->i_nlink++;
736 if (pTcon->ses->capabilities & CAP_UNIX)
737 rc = cifs_get_inode_info_unix(&newinode, full_path,
738 inode->i_sb,xid);
739 else
740 rc = cifs_get_inode_info(&newinode, full_path, NULL,
741 inode->i_sb,xid);
742
Steve Frenchb92327f2005-08-22 20:09:43 -0700743 if (pTcon->nocase)
744 direntry->d_op = &cifs_ci_dentry_ops;
745 else
746 direntry->d_op = &cifs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 d_instantiate(direntry, newinode);
748 if (direntry->d_inode)
749 direntry->d_inode->i_nlink = 2;
750 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700751 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
753 mode,
Steve French83451872005-12-01 17:12:59 -0800754 (__u64)current->fsuid,
755 (__u64)current->fsgid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 0 /* dev_t */,
Steve French737b7582005-04-28 22:41:06 -0700757 cifs_sb->local_nls,
758 cifs_sb->mnt_cifs_flags &
759 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 } else {
761 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
762 mode, (__u64)-1,
763 (__u64)-1, 0 /* dev_t */,
Steve French737b7582005-04-28 22:41:06 -0700764 cifs_sb->local_nls,
765 cifs_sb->mnt_cifs_flags &
766 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768 else {
769 /* BB to be implemented via Windows secrty descriptors
770 eg CIFSSMBWinSetPerms(xid, pTcon, full_path, mode,
771 -1, -1, local_nls); */
Steve French6473a552005-11-29 20:20:10 -0800772 if(direntry->d_inode) {
773 direntry->d_inode->i_mode = mode;
Steve French25741b32005-11-29 22:38:43 -0800774 direntry->d_inode->i_mode |= S_IFDIR;
Steve French6473a552005-11-29 20:20:10 -0800775 if(cifs_sb->mnt_cifs_flags &
776 CIFS_MOUNT_SET_UID) {
777 direntry->d_inode->i_uid =
778 current->fsuid;
779 direntry->d_inode->i_gid =
780 current->fsgid;
781 }
782 }
Steve French2a138ebb2005-11-29 21:22:19 -0800783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785 kfree(full_path);
786 FreeXid(xid);
787 return rc;
788}
789
790int cifs_rmdir(struct inode *inode, struct dentry *direntry)
791{
792 int rc = 0;
793 int xid;
794 struct cifs_sb_info *cifs_sb;
795 struct cifsTconInfo *pTcon;
796 char *full_path = NULL;
797 struct cifsInodeInfo *cifsInode;
798
Steve French3020a1f2005-11-18 11:31:10 -0800799 cFYI(1, ("cifs_rmdir, inode = 0x%p with ", inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 xid = GetXid();
802
803 cifs_sb = CIFS_SB(inode->i_sb);
804 pTcon = cifs_sb->tcon;
805
806 down(&inode->i_sb->s_vfs_rename_sem);
Steve French7f573562005-08-30 11:32:14 -0700807 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 up(&inode->i_sb->s_vfs_rename_sem);
809 if (full_path == NULL) {
810 FreeXid(xid);
811 return -ENOMEM;
812 }
813
Steve French737b7582005-04-28 22:41:06 -0700814 rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls,
815 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 if (!rc) {
818 inode->i_nlink--;
819 i_size_write(direntry->d_inode,0);
820 direntry->d_inode->i_nlink = 0;
821 }
822
823 cifsInode = CIFS_I(direntry->d_inode);
824 cifsInode->time = 0; /* force revalidate to go get info when
825 needed */
826 direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
827 current_fs_time(inode->i_sb);
828
829 kfree(full_path);
830 FreeXid(xid);
831 return rc;
832}
833
834int cifs_rename(struct inode *source_inode, struct dentry *source_direntry,
835 struct inode *target_inode, struct dentry *target_direntry)
836{
837 char *fromName;
838 char *toName;
839 struct cifs_sb_info *cifs_sb_source;
840 struct cifs_sb_info *cifs_sb_target;
841 struct cifsTconInfo *pTcon;
842 int xid;
843 int rc = 0;
844
845 xid = GetXid();
846
847 cifs_sb_target = CIFS_SB(target_inode->i_sb);
848 cifs_sb_source = CIFS_SB(source_inode->i_sb);
849 pTcon = cifs_sb_source->tcon;
850
851 if (pTcon != cifs_sb_target->tcon) {
852 FreeXid(xid);
853 return -EXDEV; /* BB actually could be allowed if same server,
854 but different share.
855 Might eventually add support for this */
856 }
857
858 /* we already have the rename sem so we do not need to grab it again
859 here to protect the path integrity */
Steve French7f573562005-08-30 11:32:14 -0700860 fromName = build_path_from_dentry(source_direntry);
861 toName = build_path_from_dentry(target_direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if ((fromName == NULL) || (toName == NULL)) {
863 rc = -ENOMEM;
864 goto cifs_rename_exit;
865 }
866
867 rc = CIFSSMBRename(xid, pTcon, fromName, toName,
Steve French737b7582005-04-28 22:41:06 -0700868 cifs_sb_source->local_nls,
869 cifs_sb_source->mnt_cifs_flags &
870 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (rc == -EEXIST) {
872 /* check if they are the same file because rename of hardlinked
873 files is a noop */
874 FILE_UNIX_BASIC_INFO *info_buf_source;
875 FILE_UNIX_BASIC_INFO *info_buf_target;
876
877 info_buf_source =
878 kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
879 if (info_buf_source != NULL) {
880 info_buf_target = info_buf_source + 1;
881 rc = CIFSSMBUnixQPathInfo(xid, pTcon, fromName,
Steve French737b7582005-04-28 22:41:06 -0700882 info_buf_source, cifs_sb_source->local_nls,
883 cifs_sb_source->mnt_cifs_flags &
884 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (rc == 0) {
886 rc = CIFSSMBUnixQPathInfo(xid, pTcon, toName,
887 info_buf_target,
Steve French737b7582005-04-28 22:41:06 -0700888 cifs_sb_target->local_nls,
889 /* remap based on source sb */
890 cifs_sb_source->mnt_cifs_flags &
891 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893 if ((rc == 0) &&
894 (info_buf_source->UniqueId ==
895 info_buf_target->UniqueId)) {
896 /* do not rename since the files are hardlinked which
897 is a noop */
898 } else {
899 /* we either can not tell the files are hardlinked
900 (as with Windows servers) or files are not
901 hardlinked so delete the target manually before
902 renaming to follow POSIX rather than Windows
903 semantics */
904 cifs_unlink(target_inode, target_direntry);
905 rc = CIFSSMBRename(xid, pTcon, fromName,
906 toName,
Steve French737b7582005-04-28 22:41:06 -0700907 cifs_sb_source->local_nls,
908 cifs_sb_source->mnt_cifs_flags
909 & CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911 kfree(info_buf_source);
912 } /* if we can not get memory just leave rc as EEXIST */
913 }
914
915 if (rc) {
916 cFYI(1, ("rename rc %d", rc));
917 }
918
919 if ((rc == -EIO) || (rc == -EEXIST)) {
920 int oplock = FALSE;
921 __u16 netfid;
922
923 /* BB FIXME Is Generic Read correct for rename? */
924 /* if renaming directory - we should not say CREATE_NOT_DIR,
925 need to test renaming open directory, also GENERIC_READ
926 might not right be right access to request */
927 rc = CIFSSMBOpen(xid, pTcon, fromName, FILE_OPEN, GENERIC_READ,
928 CREATE_NOT_DIR, &netfid, &oplock, NULL,
Steve French737b7582005-04-28 22:41:06 -0700929 cifs_sb_source->local_nls,
930 cifs_sb_source->mnt_cifs_flags &
931 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (rc==0) {
933 CIFSSMBRenameOpenFile(xid, pTcon, netfid, toName,
Steve French737b7582005-04-28 22:41:06 -0700934 cifs_sb_source->local_nls,
935 cifs_sb_source->mnt_cifs_flags &
936 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 CIFSSMBClose(xid, pTcon, netfid);
938 }
939 }
940
941cifs_rename_exit:
942 kfree(fromName);
943 kfree(toName);
944 FreeXid(xid);
945 return rc;
946}
947
948int cifs_revalidate(struct dentry *direntry)
949{
950 int xid;
951 int rc = 0;
952 char *full_path;
953 struct cifs_sb_info *cifs_sb;
954 struct cifsInodeInfo *cifsInode;
955 loff_t local_size;
956 struct timespec local_mtime;
957 int invalidate_inode = FALSE;
958
959 if (direntry->d_inode == NULL)
960 return -ENOENT;
961
962 cifsInode = CIFS_I(direntry->d_inode);
963
964 if (cifsInode == NULL)
965 return -ENOENT;
966
967 /* no sense revalidating inode info on file that no one can write */
968 if (CIFS_I(direntry->d_inode)->clientCanCacheRead)
969 return rc;
970
971 xid = GetXid();
972
973 cifs_sb = CIFS_SB(direntry->d_sb);
974
975 /* can not safely grab the rename sem here if rename calls revalidate
976 since that would deadlock */
Steve French7f573562005-08-30 11:32:14 -0700977 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (full_path == NULL) {
979 FreeXid(xid);
980 return -ENOMEM;
981 }
982 cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld "
983 "jiffies %ld", full_path, direntry->d_inode,
984 direntry->d_inode->i_count.counter, direntry,
985 direntry->d_time, jiffies));
986
987 if (cifsInode->time == 0) {
988 /* was set to zero previously to force revalidate */
989 } else if (time_before(jiffies, cifsInode->time + HZ) &&
990 lookupCacheEnabled) {
991 if ((S_ISREG(direntry->d_inode->i_mode) == 0) ||
992 (direntry->d_inode->i_nlink == 1)) {
993 kfree(full_path);
994 FreeXid(xid);
995 return rc;
996 } else {
997 cFYI(1, ("Have to revalidate file due to hardlinks"));
998 }
999 }
1000
1001 /* save mtime and size */
1002 local_mtime = direntry->d_inode->i_mtime;
1003 local_size = direntry->d_inode->i_size;
1004
1005 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX) {
1006 rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path,
1007 direntry->d_sb,xid);
1008 if (rc) {
1009 cFYI(1, ("error on getting revalidate info %d", rc));
1010/* if (rc != -ENOENT)
1011 rc = 0; */ /* BB should we cache info on
1012 certain errors? */
1013 }
1014 } else {
1015 rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL,
1016 direntry->d_sb,xid);
1017 if (rc) {
1018 cFYI(1, ("error on getting revalidate info %d", rc));
1019/* if (rc != -ENOENT)
1020 rc = 0; */ /* BB should we cache info on
1021 certain errors? */
1022 }
1023 }
1024 /* should we remap certain errors, access denied?, to zero */
1025
1026 /* if not oplocked, we invalidate inode pages if mtime or file size
1027 had changed on server */
1028
1029 if (timespec_equal(&local_mtime,&direntry->d_inode->i_mtime) &&
1030 (local_size == direntry->d_inode->i_size)) {
1031 cFYI(1, ("cifs_revalidate - inode unchanged"));
1032 } else {
1033 /* file may have changed on server */
1034 if (cifsInode->clientCanCacheRead) {
1035 /* no need to invalidate inode pages since we were the
1036 only ones who could have modified the file and the
1037 server copy is staler than ours */
1038 } else {
1039 invalidate_inode = TRUE;
1040 }
1041 }
1042
1043 /* can not grab this sem since kernel filesys locking documentation
1044 indicates i_sem may be taken by the kernel on lookup and rename
1045 which could deadlock if we grab the i_sem here as well */
1046/* down(&direntry->d_inode->i_sem);*/
1047 /* need to write out dirty pages here */
1048 if (direntry->d_inode->i_mapping) {
1049 /* do we need to lock inode until after invalidate completes
1050 below? */
1051 filemap_fdatawrite(direntry->d_inode->i_mapping);
1052 }
1053 if (invalidate_inode) {
Steve French3abb9272005-11-28 08:16:13 -08001054 /* shrink_dcache not necessary now that cifs dentry ops
1055 are exported for negative dentries */
1056/* if(S_ISDIR(direntry->d_inode->i_mode))
1057 shrink_dcache_parent(direntry); */
1058 if (S_ISREG(direntry->d_inode->i_mode)) {
1059 if (direntry->d_inode->i_mapping)
1060 filemap_fdatawait(direntry->d_inode->i_mapping);
1061 /* may eventually have to do this for open files too */
1062 if (list_empty(&(cifsInode->openFileList))) {
1063 /* changed on server - flush read ahead pages */
1064 cFYI(1, ("Invalidating read ahead data on "
1065 "closed file"));
1066 invalidate_remote_inode(direntry->d_inode);
1067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069 }
1070/* up(&direntry->d_inode->i_sem); */
1071
1072 kfree(full_path);
1073 FreeXid(xid);
1074 return rc;
1075}
1076
1077int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1078 struct kstat *stat)
1079{
1080 int err = cifs_revalidate(dentry);
1081 if (!err)
1082 generic_fillattr(dentry->d_inode, stat);
1083 return err;
1084}
1085
1086static int cifs_truncate_page(struct address_space *mapping, loff_t from)
1087{
1088 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1089 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
1090 struct page *page;
1091 char *kaddr;
1092 int rc = 0;
1093
1094 page = grab_cache_page(mapping, index);
1095 if (!page)
1096 return -ENOMEM;
1097
1098 kaddr = kmap_atomic(page, KM_USER0);
1099 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1100 flush_dcache_page(page);
1101 kunmap_atomic(kaddr, KM_USER0);
1102 unlock_page(page);
1103 page_cache_release(page);
1104 return rc;
1105}
1106
1107int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
1108{
1109 int xid;
1110 struct cifs_sb_info *cifs_sb;
1111 struct cifsTconInfo *pTcon;
1112 char *full_path = NULL;
1113 int rc = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 struct cifsFileInfo *open_file = NULL;
1115 FILE_BASIC_INFO time_buf;
1116 int set_time = FALSE;
1117 __u64 mode = 0xFFFFFFFFFFFFFFFFULL;
1118 __u64 uid = 0xFFFFFFFFFFFFFFFFULL;
1119 __u64 gid = 0xFFFFFFFFFFFFFFFFULL;
1120 struct cifsInodeInfo *cifsInode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 xid = GetXid();
1123
Steve French3020a1f2005-11-18 11:31:10 -08001124 cFYI(1, ("In cifs_setattr, name = %s attrs->iavalid 0x%x ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 direntry->d_name.name, attrs->ia_valid));
Steve French6473a552005-11-29 20:20:10 -08001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 cifs_sb = CIFS_SB(direntry->d_inode->i_sb);
1128 pTcon = cifs_sb->tcon;
1129
Steve French2a138ebb2005-11-29 21:22:19 -08001130 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) {
Steve French6473a552005-11-29 20:20:10 -08001131 /* check if we have permission to change attrs */
1132 rc = inode_change_ok(direntry->d_inode, attrs);
1133 if(rc < 0) {
1134 FreeXid(xid);
1135 return rc;
1136 } else
1137 rc = 0;
1138 }
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 down(&direntry->d_sb->s_vfs_rename_sem);
Steve French7f573562005-08-30 11:32:14 -07001141 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 up(&direntry->d_sb->s_vfs_rename_sem);
1143 if (full_path == NULL) {
1144 FreeXid(xid);
1145 return -ENOMEM;
1146 }
1147 cifsInode = CIFS_I(direntry->d_inode);
1148
1149 /* BB check if we need to refresh inode from server now ? BB */
1150
1151 /* need to flush data before changing file size on server */
1152 filemap_fdatawrite(direntry->d_inode->i_mapping);
1153 filemap_fdatawait(direntry->d_inode->i_mapping);
1154
1155 if (attrs->ia_valid & ATTR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 /* To avoid spurious oplock breaks from server, in the case of
1157 inodes that we already have open, avoid doing path based
1158 setting of file size if we can do it by handle.
1159 This keeps our caching token (oplock) and avoids timeouts
1160 when the local oplock break takes longer to flush
1161 writebehind data than the SMB timeout for the SetPathInfo
1162 request would allow */
Steve French6148a742005-10-05 12:23:19 -07001163 open_file = find_writable_file(cifsInode);
1164 if (open_file) {
1165 __u16 nfid = open_file->netfid;
1166 __u32 npid = open_file->pid;
1167 rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size,
1168 nfid, npid, FALSE);
Steve French23e7dd72005-10-20 13:44:56 -07001169 atomic_dec(&open_file->wrtPending);
Steve French6148a742005-10-05 12:23:19 -07001170 cFYI(1,("SetFSize for attrs rc = %d", rc));
1171 if(rc == -EINVAL) {
1172 int bytes_written;
1173 rc = CIFSSMBWrite(xid, pTcon,
1174 nfid, 0, attrs->ia_size,
1175 &bytes_written, NULL, NULL,
1176 1 /* 45 seconds */);
1177 cFYI(1,("Wrt seteof rc %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
Steve French6473a552005-11-29 20:20:10 -08001179 } else
1180 rc = -EINVAL;
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (rc != 0) {
1183 /* Set file size by pathname rather than by handle
1184 either because no valid, writeable file handle for
1185 it was found or because there was an error setting
1186 it by handle */
1187 rc = CIFSSMBSetEOF(xid, pTcon, full_path,
1188 attrs->ia_size, FALSE,
Steve French737b7582005-04-28 22:41:06 -07001189 cifs_sb->local_nls,
1190 cifs_sb->mnt_cifs_flags &
1191 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve Frenche30dcf32005-09-20 20:49:16 -07001192 cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc));
1193 if(rc == -EINVAL) {
1194 __u16 netfid;
1195 int oplock = FALSE;
1196
1197 rc = SMBLegacyOpen(xid, pTcon, full_path,
1198 FILE_OPEN,
1199 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1200 CREATE_NOT_DIR, &netfid, &oplock,
1201 NULL, cifs_sb->local_nls,
1202 cifs_sb->mnt_cifs_flags &
1203 CIFS_MOUNT_MAP_SPECIAL_CHR);
1204 if (rc==0) {
1205 int bytes_written;
1206 rc = CIFSSMBWrite(xid, pTcon,
1207 netfid, 0,
1208 attrs->ia_size,
1209 &bytes_written, NULL,
1210 NULL, 1 /* 45 sec */);
1211 cFYI(1,("wrt seteof rc %d",rc));
1212 CIFSSMBClose(xid, pTcon, netfid);
1213 }
1214
1215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217
1218 /* Server is ok setting allocation size implicitly - no need
1219 to call:
1220 CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, TRUE,
1221 cifs_sb->local_nls);
1222 */
1223
1224 if (rc == 0) {
1225 rc = vmtruncate(direntry->d_inode, attrs->ia_size);
1226 cifs_truncate_page(direntry->d_inode->i_mapping,
1227 direntry->d_inode->i_size);
Steve Frenche30dcf32005-09-20 20:49:16 -07001228 } else
1229 goto cifs_setattr_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231 if (attrs->ia_valid & ATTR_UID) {
Steve Frenche30dcf32005-09-20 20:49:16 -07001232 cFYI(1, ("UID changed to %d", attrs->ia_uid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 uid = attrs->ia_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235 if (attrs->ia_valid & ATTR_GID) {
Steve Frenche30dcf32005-09-20 20:49:16 -07001236 cFYI(1, ("GID changed to %d", attrs->ia_gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 gid = attrs->ia_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239
1240 time_buf.Attributes = 0;
1241 if (attrs->ia_valid & ATTR_MODE) {
Steve Frenche30dcf32005-09-20 20:49:16 -07001242 cFYI(1, ("Mode changed to 0x%x", attrs->ia_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 mode = attrs->ia_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
1245
1246 if ((cifs_sb->tcon->ses->capabilities & CAP_UNIX)
1247 && (attrs->ia_valid & (ATTR_MODE | ATTR_GID | ATTR_UID)))
1248 rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode, uid, gid,
Steve French737b7582005-04-28 22:41:06 -07001249 0 /* dev_t */, cifs_sb->local_nls,
1250 cifs_sb->mnt_cifs_flags &
1251 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 else if (attrs->ia_valid & ATTR_MODE) {
Steve Frenchcdbce9c2005-11-19 21:04:52 -08001253 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if ((mode & S_IWUGO) == 0) /* not writeable */ {
1255 if ((cifsInode->cifsAttrs & ATTR_READONLY) == 0)
1256 time_buf.Attributes =
1257 cpu_to_le32(cifsInode->cifsAttrs |
1258 ATTR_READONLY);
1259 } else if ((mode & S_IWUGO) == S_IWUGO) {
1260 if (cifsInode->cifsAttrs & ATTR_READONLY)
1261 time_buf.Attributes =
1262 cpu_to_le32(cifsInode->cifsAttrs &
1263 (~ATTR_READONLY));
1264 }
1265 /* BB to be implemented -
1266 via Windows security descriptors or streams */
1267 /* CIFSSMBWinSetPerms(xid, pTcon, full_path, mode, uid, gid,
1268 cifs_sb->local_nls); */
1269 }
1270
1271 if (attrs->ia_valid & ATTR_ATIME) {
1272 set_time = TRUE;
1273 time_buf.LastAccessTime =
1274 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1275 } else
1276 time_buf.LastAccessTime = 0;
1277
1278 if (attrs->ia_valid & ATTR_MTIME) {
1279 set_time = TRUE;
1280 time_buf.LastWriteTime =
1281 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1282 } else
1283 time_buf.LastWriteTime = 0;
Steve Frenche30dcf32005-09-20 20:49:16 -07001284 /* Do not set ctime explicitly unless other time
1285 stamps are changed explicitly (i.e. by utime()
1286 since we would then have a mix of client and
1287 server times */
1288
1289 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 set_time = TRUE;
Steve Frenche30dcf32005-09-20 20:49:16 -07001291 /* Although Samba throws this field away
1292 it may be useful to Windows - but we do
1293 not want to set ctime unless some other
1294 timestamp is changing */
1295 cFYI(1, ("CIFS - CTIME changed "));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 time_buf.ChangeTime =
1297 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1298 } else
1299 time_buf.ChangeTime = 0;
1300
1301 if (set_time || time_buf.Attributes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 time_buf.CreationTime = 0; /* do not change */
1303 /* In the future we should experiment - try setting timestamps
1304 via Handle (SetFileInfo) instead of by path */
1305 if (!(pTcon->ses->flags & CIFS_SES_NT4))
1306 rc = CIFSSMBSetTimes(xid, pTcon, full_path, &time_buf,
Steve French737b7582005-04-28 22:41:06 -07001307 cifs_sb->local_nls,
1308 cifs_sb->mnt_cifs_flags &
1309 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 else
1311 rc = -EOPNOTSUPP;
1312
1313 if (rc == -EOPNOTSUPP) {
1314 int oplock = FALSE;
1315 __u16 netfid;
1316
1317 cFYI(1, ("calling SetFileInfo since SetPathInfo for "
1318 "times not supported by this server"));
1319 /* BB we could scan to see if we already have it open
1320 and pass in pid of opener to function */
1321 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN,
1322 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1323 CREATE_NOT_DIR, &netfid, &oplock,
Steve French737b7582005-04-28 22:41:06 -07001324 NULL, cifs_sb->local_nls,
1325 cifs_sb->mnt_cifs_flags &
1326 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (rc==0) {
1328 rc = CIFSSMBSetFileTimes(xid, pTcon, &time_buf,
1329 netfid);
1330 CIFSSMBClose(xid, pTcon, netfid);
1331 } else {
1332 /* BB For even older servers we could convert time_buf
1333 into old DOS style which uses two second
1334 granularity */
1335
1336 /* rc = CIFSSMBSetTimesLegacy(xid, pTcon, full_path,
1337 &time_buf, cifs_sb->local_nls); */
1338 }
1339 }
Steve Frenche30dcf32005-09-20 20:49:16 -07001340 /* Even if error on time set, no sense failing the call if
1341 the server would set the time to a reasonable value anyway,
1342 and this check ensures that we are not being called from
1343 sys_utimes in which case we ought to fail the call back to
1344 the user when the server rejects the call */
1345 if((rc) && (attrs->ia_valid &&
1346 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
1347 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349
1350 /* do not need local check to inode_check_ok since the server does
1351 that */
1352 if (!rc)
1353 rc = inode_setattr(direntry->d_inode, attrs);
Steve Frenche30dcf32005-09-20 20:49:16 -07001354cifs_setattr_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 kfree(full_path);
1356 FreeXid(xid);
1357 return rc;
1358}
1359
1360void cifs_delete_inode(struct inode *inode)
1361{
1362 cFYI(1, ("In cifs_delete_inode, inode = 0x%p ", inode));
1363 /* may have to add back in if and when safe distributed caching of
1364 directories added e.g. via FindNotify */
1365}