blob: ffc7305841b3c27173f8890a82ad5c9a5c5666c6 [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;
213 char buf[8];
214 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,
235 8 /* length */, 0 /* offset */,
236 &bytes_read, &pbuf);
237 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 French9e294f12005-11-17 16:59:21 -0800241 } else if(memcmp("IntxCHR", pbuf, 8) == 0) {
242 cFYI(1,("Char device"));
Steve French3020a1f2005-11-18 11:31:10 -0800243 inode->i_mode |= S_IFCHR;
Steve French9e294f12005-11-17 16:59:21 -0800244 } else if(memcmp("IntxLNK", pbuf, 7) == 0) {
245 cFYI(1,("Symlink"));
Steve French3020a1f2005-11-18 11:31:10 -0800246 inode->i_mode |= S_IFLNK;
247 }
248 } else {
249 inode->i_mode |= S_IFREG; /* then it is a file */
250 rc = -EOPNOTSUPP; /* or some unknown SFU type */
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800251 }
252
253 CIFSSMBClose(xid, pTcon, netfid);
254
255
256 /* inode->i_rdev = MKDEV(le64_to_cpu(DevMajor),
257 le64_to_cpu(DevMinor) & MINORMASK);*/
258/* inode->i_mode |= S_IFBLK; */
259 }
260 return rc;
261
262}
263
Steve French9e294f12005-11-17 16:59:21 -0800264#define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
265
266static int get_sfu_uid_mode(struct inode * inode,
267 const unsigned char *path,
268 struct cifs_sb_info *cifs_sb, int xid)
269{
Steve French3020a1f2005-11-18 11:31:10 -0800270#ifdef CONFIG_CIFS_XATTR
Steve French9e294f12005-11-17 16:59:21 -0800271 ssize_t rc;
272 char ea_value[4];
273 __u32 mode;
274
275 rc = CIFSSMBQueryEA(xid, cifs_sb->tcon, path, "SETFILEBITS",
276 ea_value, 4 /* size of buf */, cifs_sb->local_nls,
277 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
278 if(rc < 0)
279 return (int)rc;
280 else if (rc > 3) {
281 mode = le32_to_cpu(*((__le32 *)ea_value));
Steve French3020a1f2005-11-18 11:31:10 -0800282 cFYI(1,("special bits 0%o org mode 0%o", mode, inode->i_mode));
Steve French9e294f12005-11-17 16:59:21 -0800283 inode->i_mode = (mode & SFBITS_MASK) | inode->i_mode;
284 cFYI(1,("special mode bits 0%o", mode));
285 return 0;
286 } else {
287 return 0;
288 }
Steve French3020a1f2005-11-18 11:31:10 -0800289#else
290 return -EOPNOTSUPP;
291#endif
292
Steve French9e294f12005-11-17 16:59:21 -0800293
294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296int cifs_get_inode_info(struct inode **pinode,
297 const unsigned char *search_path, FILE_ALL_INFO *pfindData,
298 struct super_block *sb, int xid)
299{
300 int rc = 0;
301 struct cifsTconInfo *pTcon;
302 struct inode *inode;
303 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
304 char *tmp_path;
305 char *buf = NULL;
306
307 pTcon = cifs_sb->tcon;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800308 cFYI(1,("Getting info on %s", search_path));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700310 if ((pfindData == NULL) && (*pinode != NULL)) {
311 if (CIFS_I(*pinode)->clientCanCacheRead) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 cFYI(1,("No need to revalidate cached inode sizes"));
313 return rc;
314 }
315 }
316
317 /* if file info not passed in then get it from server */
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700318 if (pfindData == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700320 if (buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return -ENOMEM;
322 pfindData = (FILE_ALL_INFO *)buf;
323 /* could do find first instead but this returns more info */
324 rc = CIFSSMBQPathInfo(xid, pTcon, search_path, pfindData,
Steve French6b8edfe2005-08-23 20:26:03 -0700325 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
Steve French737b7582005-04-28 22:41:06 -0700326 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve French6b8edfe2005-08-23 20:26:03 -0700327 /* BB optimize code so we do not make the above call
328 when server claims no NT SMB support and the above call
329 failed at least once - set flag in tcon or mount */
330 if((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
331 rc = SMBQueryInformation(xid, pTcon, search_path,
332 pfindData, cifs_sb->local_nls,
333 cifs_sb->mnt_cifs_flags &
334 CIFS_MOUNT_MAP_SPECIAL_CHR);
335 }
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338 /* dump_mem("\nQPathInfo return data",&findData, sizeof(findData)); */
339 if (rc) {
340 if (rc == -EREMOTE) {
341 tmp_path =
342 kmalloc(strnlen
343 (pTcon->treeName,
344 MAX_TREE_SIZE + 1) +
345 strnlen(search_path, MAX_PATHCONF) + 1,
346 GFP_KERNEL);
347 if (tmp_path == NULL) {
348 kfree(buf);
349 return -ENOMEM;
350 }
351
352 strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE);
353 strncat(tmp_path, search_path, MAX_PATHCONF);
354 rc = connect_to_dfs_path(xid, pTcon->ses,
355 /* treename + */ tmp_path,
Steve French737b7582005-04-28 22:41:06 -0700356 cifs_sb->local_nls,
357 cifs_sb->mnt_cifs_flags &
358 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 kfree(tmp_path);
360 /* BB fix up inode etc. */
361 } else if (rc) {
362 kfree(buf);
363 return rc;
364 }
365 } else {
366 struct cifsInodeInfo *cifsInfo;
367 __u32 attr = le32_to_cpu(pfindData->Attributes);
368
369 /* get new inode */
370 if (*pinode == NULL) {
371 *pinode = new_inode(sb);
372 if (*pinode == NULL)
373 return -ENOMEM;
374 /* Is an i_ino of zero legal? Can we use that to check
375 if the server supports returning inode numbers? Are
376 there other sanity checks we can use to ensure that
377 the server is really filling in that field? */
378
379 /* We can not use the IndexNumber field by default from
380 Windows or Samba (in ALL_INFO buf) but we can request
381 it explicitly. It may not be unique presumably if
382 the server has multiple devices mounted under one
383 share */
384
385 /* There may be higher info levels that work but are
386 there Windows server or network appliances for which
387 IndexNumber field is not guaranteed unique? */
388
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700389 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM){
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 int rc1 = 0;
391 __u64 inode_num;
392
393 rc1 = CIFSGetSrvInodeNumber(xid, pTcon,
394 search_path, &inode_num,
Steve French737b7582005-04-28 22:41:06 -0700395 cifs_sb->local_nls,
396 cifs_sb->mnt_cifs_flags &
397 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700398 if (rc1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 cFYI(1,("GetSrvInodeNum rc %d", rc1));
400 /* BB EOPNOSUPP disable SERVER_INUM? */
401 } else /* do we need cast or hash to ino? */
402 (*pinode)->i_ino = inode_num;
403 } /* else ino incremented to unique num in new_inode*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 insert_inode_hash(*pinode);
405 }
406 inode = *pinode;
407 cifsInfo = CIFS_I(inode);
408 cifsInfo->cifsAttrs = attr;
Steve French3020a1f2005-11-18 11:31:10 -0800409 cFYI(1, ("Old time %ld ", cifsInfo->time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 cifsInfo->time = jiffies;
Steve French3020a1f2005-11-18 11:31:10 -0800411 cFYI(1, ("New time %ld ", cifsInfo->time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 /* blksize needs to be multiple of two. So safer to default to
414 blksize and blkbits set in superblock so 2**blkbits and blksize
415 will match rather than setting to:
416 (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
417
418 /* Linux can not store file creation time unfortunately so we ignore it */
419 inode->i_atime =
420 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime));
421 inode->i_mtime =
422 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
423 inode->i_ctime =
424 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
Steve French3020a1f2005-11-18 11:31:10 -0800425 cFYI(0, ("Attributes came in as 0x%x ", attr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 /* set default mode. will override for dirs below */
428 if (atomic_read(&cifsInfo->inUse) == 0)
429 /* new inode, can safely set these fields */
430 inode->i_mode = cifs_sb->mnt_file_mode;
Steve French3020a1f2005-11-18 11:31:10 -0800431 else /* since we set the inode type below we need to mask off
432 to avoid strange results if type changes and both get orred in */
433 inode->i_mode &= ~S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434/* if (attr & ATTR_REPARSE) */
435 /* We no longer handle these as symlinks because we could not
436 follow them due to the absolute path with drive letter */
437 if (attr & ATTR_DIRECTORY) {
438 /* override default perms since we do not do byte range locking
439 on dirs */
440 inode->i_mode = cifs_sb->mnt_dir_mode;
441 inode->i_mode |= S_IFDIR;
Steve Frencheda3c0292005-07-21 15:20:28 -0700442 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
443 (cifsInfo->cifsAttrs & ATTR_SYSTEM) &&
444 /* No need to le64 convert size of zero */
445 (pfindData->EndOfFile == 0)) {
446 inode->i_mode = cifs_sb->mnt_file_mode;
447 inode->i_mode |= S_IFIFO;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800448/* BB Finish for SFU style symlinks and devices */
449 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
450 (cifsInfo->cifsAttrs & ATTR_SYSTEM)) {
451 if (decode_sfu_inode(inode,
452 le64_to_cpu(pfindData->EndOfFile),
453 search_path,
454 cifs_sb, xid)) {
455 cFYI(1,("Unrecognized sfu inode type"));
456 }
Steve French3020a1f2005-11-18 11:31:10 -0800457 cFYI(1,("sfu mode 0%o",inode->i_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 } else {
459 inode->i_mode |= S_IFREG;
460 /* treat the dos attribute of read-only as read-only
461 mode e.g. 555 */
462 if (cifsInfo->cifsAttrs & ATTR_READONLY)
463 inode->i_mode &= ~(S_IWUGO);
464 /* BB add code here -
465 validate if device or weird share or device type? */
466 }
467 if (is_size_safe_to_change(cifsInfo)) {
468 /* can not safely change the file size here if the
469 client is writing to it due to potential races */
470 i_size_write(inode,le64_to_cpu(pfindData->EndOfFile));
471
472 /* 512 bytes (2**9) is the fake blocksize that must be
473 used for this calculation */
474 inode->i_blocks = (512 - 1 + le64_to_cpu(
475 pfindData->AllocationSize)) >> 9;
476 }
477
478 inode->i_nlink = le32_to_cpu(pfindData->NumberOfLinks);
479
480 /* BB fill in uid and gid here? with help from winbind?
481 or retrieve from NTFS stream extended attribute */
Steve French9e294f12005-11-17 16:59:21 -0800482 if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
483 /* fill in uid, gid, mode from server ACL */
484 get_sfu_uid_mode(inode, search_path, cifs_sb, xid);
485 } else if (atomic_read(&cifsInfo->inUse) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 inode->i_uid = cifs_sb->mnt_uid;
487 inode->i_gid = cifs_sb->mnt_gid;
488 /* set so we do not keep refreshing these fields with
489 bad data after user has changed them in memory */
490 atomic_set(&cifsInfo->inUse,1);
491 }
492
493 if (S_ISREG(inode->i_mode)) {
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800494 cFYI(1, ("File inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 inode->i_op = &cifs_file_inode_ops;
Steve French8b94bcb2005-11-11 11:41:00 -0800496 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
497 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
498 inode->i_fop =
499 &cifs_file_direct_nobrl_ops;
500 else
501 inode->i_fop = &cifs_file_direct_ops;
502 } else if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
503 inode->i_fop = &cifs_file_nobrl_ops;
504 else /* not direct, send byte range locks */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 inode->i_fop = &cifs_file_ops;
Steve French8b94bcb2005-11-11 11:41:00 -0800506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 inode->i_data.a_ops = &cifs_addr_ops;
Steve Frenchbfa0d752005-08-31 21:50:37 -0700508 if(pTcon->ses->server->maxBuf <
509 4096 + MAX_CIFS_HDR_SIZE)
510 inode->i_data.a_ops->readpages = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 } else if (S_ISDIR(inode->i_mode)) {
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800512 cFYI(1, ("Directory inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 inode->i_op = &cifs_dir_inode_ops;
514 inode->i_fop = &cifs_dir_ops;
515 } else if (S_ISLNK(inode->i_mode)) {
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800516 cFYI(1, ("Symbolic Link inode"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 inode->i_op = &cifs_symlink_inode_ops;
518 } else {
519 init_special_inode(inode, inode->i_mode,
520 inode->i_rdev);
521 }
522 }
523 kfree(buf);
524 return rc;
525}
526
527/* gets root inode */
528void cifs_read_inode(struct inode *inode)
529{
530 int xid;
531 struct cifs_sb_info *cifs_sb;
532
533 cifs_sb = CIFS_SB(inode->i_sb);
534 xid = GetXid();
535 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)
536 cifs_get_inode_info_unix(&inode, "", inode->i_sb,xid);
537 else
538 cifs_get_inode_info(&inode, "", NULL, inode->i_sb,xid);
539 /* can not call macro FreeXid here since in a void func */
540 _FreeXid(xid);
541}
542
543int cifs_unlink(struct inode *inode, struct dentry *direntry)
544{
545 int rc = 0;
546 int xid;
547 struct cifs_sb_info *cifs_sb;
548 struct cifsTconInfo *pTcon;
549 char *full_path = NULL;
550 struct cifsInodeInfo *cifsInode;
551 FILE_BASIC_INFO *pinfo_buf;
552
Steve French3020a1f2005-11-18 11:31:10 -0800553 cFYI(1, ("cifs_unlink, inode = 0x%p with ", inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 xid = GetXid();
556
557 cifs_sb = CIFS_SB(inode->i_sb);
558 pTcon = cifs_sb->tcon;
559
560 /* Unlink can be called from rename so we can not grab the sem here
561 since we deadlock otherwise */
562/* down(&direntry->d_sb->s_vfs_rename_sem);*/
Steve French7f573562005-08-30 11:32:14 -0700563 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/* up(&direntry->d_sb->s_vfs_rename_sem);*/
565 if (full_path == NULL) {
566 FreeXid(xid);
567 return -ENOMEM;
568 }
Steve French737b7582005-04-28 22:41:06 -0700569 rc = CIFSSMBDelFile(xid, pTcon, full_path, cifs_sb->local_nls,
570 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 if (!rc) {
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700573 if (direntry->d_inode)
Steve Frenchb2aeb9d2005-05-17 13:16:18 -0500574 direntry->d_inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 } else if (rc == -ENOENT) {
576 d_drop(direntry);
577 } else if (rc == -ETXTBSY) {
578 int oplock = FALSE;
579 __u16 netfid;
580
581 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, DELETE,
582 CREATE_NOT_DIR | CREATE_DELETE_ON_CLOSE,
Steve French737b7582005-04-28 22:41:06 -0700583 &netfid, &oplock, NULL, cifs_sb->local_nls,
584 cifs_sb->mnt_cifs_flags &
585 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (rc==0) {
587 CIFSSMBRenameOpenFile(xid, pTcon, netfid, NULL,
Steve French737b7582005-04-28 22:41:06 -0700588 cifs_sb->local_nls,
589 cifs_sb->mnt_cifs_flags &
590 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 CIFSSMBClose(xid, pTcon, netfid);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700592 if (direntry->d_inode)
Steve Frenchb2aeb9d2005-05-17 13:16:18 -0500593 direntry->d_inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595 } else if (rc == -EACCES) {
596 /* try only if r/o attribute set in local lookup data? */
597 pinfo_buf = kmalloc(sizeof(FILE_BASIC_INFO), GFP_KERNEL);
598 if (pinfo_buf) {
599 memset(pinfo_buf, 0, sizeof(FILE_BASIC_INFO));
600 /* ATTRS set to normal clears r/o bit */
601 pinfo_buf->Attributes = cpu_to_le32(ATTR_NORMAL);
602 if (!(pTcon->ses->flags & CIFS_SES_NT4))
603 rc = CIFSSMBSetTimes(xid, pTcon, full_path,
604 pinfo_buf,
Steve French737b7582005-04-28 22:41:06 -0700605 cifs_sb->local_nls,
606 cifs_sb->mnt_cifs_flags &
607 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 else
609 rc = -EOPNOTSUPP;
610
611 if (rc == -EOPNOTSUPP) {
612 int oplock = FALSE;
613 __u16 netfid;
614 /* rc = CIFSSMBSetAttrLegacy(xid, pTcon,
615 full_path,
616 (__u16)ATTR_NORMAL,
617 cifs_sb->local_nls);
618 For some strange reason it seems that NT4 eats the
619 old setattr call without actually setting the
620 attributes so on to the third attempted workaround
621 */
622
623 /* BB could scan to see if we already have it open
624 and pass in pid of opener to function */
625 rc = CIFSSMBOpen(xid, pTcon, full_path,
626 FILE_OPEN, SYNCHRONIZE |
627 FILE_WRITE_ATTRIBUTES, 0,
628 &netfid, &oplock, NULL,
Steve French737b7582005-04-28 22:41:06 -0700629 cifs_sb->local_nls,
630 cifs_sb->mnt_cifs_flags &
631 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 if (rc==0) {
633 rc = CIFSSMBSetFileTimes(xid, pTcon,
634 pinfo_buf,
635 netfid);
636 CIFSSMBClose(xid, pTcon, netfid);
637 }
638 }
639 kfree(pinfo_buf);
640 }
641 if (rc==0) {
Steve French737b7582005-04-28 22:41:06 -0700642 rc = CIFSSMBDelFile(xid, pTcon, full_path,
643 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) {
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700647 if (direntry->d_inode)
Steve Frenchb2aeb9d2005-05-17 13:16:18 -0500648 direntry->d_inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 } else if (rc == -ETXTBSY) {
650 int oplock = FALSE;
651 __u16 netfid;
652
653 rc = CIFSSMBOpen(xid, pTcon, full_path,
654 FILE_OPEN, DELETE,
655 CREATE_NOT_DIR |
656 CREATE_DELETE_ON_CLOSE,
657 &netfid, &oplock, NULL,
Steve French737b7582005-04-28 22:41:06 -0700658 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==0) {
662 CIFSSMBRenameOpenFile(xid, pTcon,
663 netfid, NULL,
Steve French737b7582005-04-28 22:41:06 -0700664 cifs_sb->local_nls,
665 cifs_sb->mnt_cifs_flags &
666 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 CIFSSMBClose(xid, pTcon, netfid);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700668 if (direntry->d_inode)
Steve Frenchb2aeb9d2005-05-17 13:16:18 -0500669 direntry->d_inode->i_nlink--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671 /* BB if rc = -ETXTBUSY goto the rename logic BB */
672 }
673 }
674 }
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700675 if (direntry->d_inode) {
Steve Frenchb2aeb9d2005-05-17 13:16:18 -0500676 cifsInode = CIFS_I(direntry->d_inode);
677 cifsInode->time = 0; /* will force revalidate to get info
678 when needed */
679 direntry->d_inode->i_ctime = current_fs_time(inode->i_sb);
680 }
681 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 cifsInode = CIFS_I(inode);
683 cifsInode->time = 0; /* force revalidate of dir as well */
684
685 kfree(full_path);
686 FreeXid(xid);
687 return rc;
688}
689
690int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode)
691{
692 int rc = 0;
693 int xid;
694 struct cifs_sb_info *cifs_sb;
695 struct cifsTconInfo *pTcon;
696 char *full_path = NULL;
697 struct inode *newinode = NULL;
698
699 cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p ", mode, inode));
700
701 xid = GetXid();
702
703 cifs_sb = CIFS_SB(inode->i_sb);
704 pTcon = cifs_sb->tcon;
705
706 down(&inode->i_sb->s_vfs_rename_sem);
Steve French7f573562005-08-30 11:32:14 -0700707 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 up(&inode->i_sb->s_vfs_rename_sem);
709 if (full_path == NULL) {
710 FreeXid(xid);
711 return -ENOMEM;
712 }
713 /* BB add setting the equivalent of mode via CreateX w/ACLs */
Steve French737b7582005-04-28 22:41:06 -0700714 rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls,
715 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (rc) {
717 cFYI(1, ("cifs_mkdir returned 0x%x ", rc));
718 d_drop(direntry);
719 } else {
720 inode->i_nlink++;
721 if (pTcon->ses->capabilities & CAP_UNIX)
722 rc = cifs_get_inode_info_unix(&newinode, full_path,
723 inode->i_sb,xid);
724 else
725 rc = cifs_get_inode_info(&newinode, full_path, NULL,
726 inode->i_sb,xid);
727
Steve Frenchb92327f2005-08-22 20:09:43 -0700728 if (pTcon->nocase)
729 direntry->d_op = &cifs_ci_dentry_ops;
730 else
731 direntry->d_op = &cifs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 d_instantiate(direntry, newinode);
733 if (direntry->d_inode)
734 direntry->d_inode->i_nlink = 2;
735 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX)
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700736 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
738 mode,
739 (__u64)current->euid,
740 (__u64)current->egid,
741 0 /* dev_t */,
Steve French737b7582005-04-28 22:41:06 -0700742 cifs_sb->local_nls,
743 cifs_sb->mnt_cifs_flags &
744 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 } else {
746 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
747 mode, (__u64)-1,
748 (__u64)-1, 0 /* dev_t */,
Steve French737b7582005-04-28 22:41:06 -0700749 cifs_sb->local_nls,
750 cifs_sb->mnt_cifs_flags &
751 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753 else {
754 /* BB to be implemented via Windows secrty descriptors
755 eg CIFSSMBWinSetPerms(xid, pTcon, full_path, mode,
756 -1, -1, local_nls); */
757 }
758 }
759 kfree(full_path);
760 FreeXid(xid);
761 return rc;
762}
763
764int cifs_rmdir(struct inode *inode, struct dentry *direntry)
765{
766 int rc = 0;
767 int xid;
768 struct cifs_sb_info *cifs_sb;
769 struct cifsTconInfo *pTcon;
770 char *full_path = NULL;
771 struct cifsInodeInfo *cifsInode;
772
Steve French3020a1f2005-11-18 11:31:10 -0800773 cFYI(1, ("cifs_rmdir, inode = 0x%p with ", inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 xid = GetXid();
776
777 cifs_sb = CIFS_SB(inode->i_sb);
778 pTcon = cifs_sb->tcon;
779
780 down(&inode->i_sb->s_vfs_rename_sem);
Steve French7f573562005-08-30 11:32:14 -0700781 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 up(&inode->i_sb->s_vfs_rename_sem);
783 if (full_path == NULL) {
784 FreeXid(xid);
785 return -ENOMEM;
786 }
787
Steve French737b7582005-04-28 22:41:06 -0700788 rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls,
789 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 if (!rc) {
792 inode->i_nlink--;
793 i_size_write(direntry->d_inode,0);
794 direntry->d_inode->i_nlink = 0;
795 }
796
797 cifsInode = CIFS_I(direntry->d_inode);
798 cifsInode->time = 0; /* force revalidate to go get info when
799 needed */
800 direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
801 current_fs_time(inode->i_sb);
802
803 kfree(full_path);
804 FreeXid(xid);
805 return rc;
806}
807
808int cifs_rename(struct inode *source_inode, struct dentry *source_direntry,
809 struct inode *target_inode, struct dentry *target_direntry)
810{
811 char *fromName;
812 char *toName;
813 struct cifs_sb_info *cifs_sb_source;
814 struct cifs_sb_info *cifs_sb_target;
815 struct cifsTconInfo *pTcon;
816 int xid;
817 int rc = 0;
818
819 xid = GetXid();
820
821 cifs_sb_target = CIFS_SB(target_inode->i_sb);
822 cifs_sb_source = CIFS_SB(source_inode->i_sb);
823 pTcon = cifs_sb_source->tcon;
824
825 if (pTcon != cifs_sb_target->tcon) {
826 FreeXid(xid);
827 return -EXDEV; /* BB actually could be allowed if same server,
828 but different share.
829 Might eventually add support for this */
830 }
831
832 /* we already have the rename sem so we do not need to grab it again
833 here to protect the path integrity */
Steve French7f573562005-08-30 11:32:14 -0700834 fromName = build_path_from_dentry(source_direntry);
835 toName = build_path_from_dentry(target_direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if ((fromName == NULL) || (toName == NULL)) {
837 rc = -ENOMEM;
838 goto cifs_rename_exit;
839 }
840
841 rc = CIFSSMBRename(xid, pTcon, fromName, toName,
Steve French737b7582005-04-28 22:41:06 -0700842 cifs_sb_source->local_nls,
843 cifs_sb_source->mnt_cifs_flags &
844 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (rc == -EEXIST) {
846 /* check if they are the same file because rename of hardlinked
847 files is a noop */
848 FILE_UNIX_BASIC_INFO *info_buf_source;
849 FILE_UNIX_BASIC_INFO *info_buf_target;
850
851 info_buf_source =
852 kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
853 if (info_buf_source != NULL) {
854 info_buf_target = info_buf_source + 1;
855 rc = CIFSSMBUnixQPathInfo(xid, pTcon, fromName,
Steve French737b7582005-04-28 22:41:06 -0700856 info_buf_source, 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 == 0) {
860 rc = CIFSSMBUnixQPathInfo(xid, pTcon, toName,
861 info_buf_target,
Steve French737b7582005-04-28 22:41:06 -0700862 cifs_sb_target->local_nls,
863 /* remap based on source sb */
864 cifs_sb_source->mnt_cifs_flags &
865 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867 if ((rc == 0) &&
868 (info_buf_source->UniqueId ==
869 info_buf_target->UniqueId)) {
870 /* do not rename since the files are hardlinked which
871 is a noop */
872 } else {
873 /* we either can not tell the files are hardlinked
874 (as with Windows servers) or files are not
875 hardlinked so delete the target manually before
876 renaming to follow POSIX rather than Windows
877 semantics */
878 cifs_unlink(target_inode, target_direntry);
879 rc = CIFSSMBRename(xid, pTcon, fromName,
880 toName,
Steve French737b7582005-04-28 22:41:06 -0700881 cifs_sb_source->local_nls,
882 cifs_sb_source->mnt_cifs_flags
883 & CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885 kfree(info_buf_source);
886 } /* if we can not get memory just leave rc as EEXIST */
887 }
888
889 if (rc) {
890 cFYI(1, ("rename rc %d", rc));
891 }
892
893 if ((rc == -EIO) || (rc == -EEXIST)) {
894 int oplock = FALSE;
895 __u16 netfid;
896
897 /* BB FIXME Is Generic Read correct for rename? */
898 /* if renaming directory - we should not say CREATE_NOT_DIR,
899 need to test renaming open directory, also GENERIC_READ
900 might not right be right access to request */
901 rc = CIFSSMBOpen(xid, pTcon, fromName, FILE_OPEN, GENERIC_READ,
902 CREATE_NOT_DIR, &netfid, &oplock, NULL,
Steve French737b7582005-04-28 22:41:06 -0700903 cifs_sb_source->local_nls,
904 cifs_sb_source->mnt_cifs_flags &
905 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (rc==0) {
907 CIFSSMBRenameOpenFile(xid, pTcon, netfid, toName,
Steve French737b7582005-04-28 22:41:06 -0700908 cifs_sb_source->local_nls,
909 cifs_sb_source->mnt_cifs_flags &
910 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 CIFSSMBClose(xid, pTcon, netfid);
912 }
913 }
914
915cifs_rename_exit:
916 kfree(fromName);
917 kfree(toName);
918 FreeXid(xid);
919 return rc;
920}
921
922int cifs_revalidate(struct dentry *direntry)
923{
924 int xid;
925 int rc = 0;
926 char *full_path;
927 struct cifs_sb_info *cifs_sb;
928 struct cifsInodeInfo *cifsInode;
929 loff_t local_size;
930 struct timespec local_mtime;
931 int invalidate_inode = FALSE;
932
933 if (direntry->d_inode == NULL)
934 return -ENOENT;
935
936 cifsInode = CIFS_I(direntry->d_inode);
937
938 if (cifsInode == NULL)
939 return -ENOENT;
940
941 /* no sense revalidating inode info on file that no one can write */
942 if (CIFS_I(direntry->d_inode)->clientCanCacheRead)
943 return rc;
944
945 xid = GetXid();
946
947 cifs_sb = CIFS_SB(direntry->d_sb);
948
949 /* can not safely grab the rename sem here if rename calls revalidate
950 since that would deadlock */
Steve French7f573562005-08-30 11:32:14 -0700951 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (full_path == NULL) {
953 FreeXid(xid);
954 return -ENOMEM;
955 }
956 cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld "
957 "jiffies %ld", full_path, direntry->d_inode,
958 direntry->d_inode->i_count.counter, direntry,
959 direntry->d_time, jiffies));
960
961 if (cifsInode->time == 0) {
962 /* was set to zero previously to force revalidate */
963 } else if (time_before(jiffies, cifsInode->time + HZ) &&
964 lookupCacheEnabled) {
965 if ((S_ISREG(direntry->d_inode->i_mode) == 0) ||
966 (direntry->d_inode->i_nlink == 1)) {
967 kfree(full_path);
968 FreeXid(xid);
969 return rc;
970 } else {
971 cFYI(1, ("Have to revalidate file due to hardlinks"));
972 }
973 }
974
975 /* save mtime and size */
976 local_mtime = direntry->d_inode->i_mtime;
977 local_size = direntry->d_inode->i_size;
978
979 if (cifs_sb->tcon->ses->capabilities & CAP_UNIX) {
980 rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path,
981 direntry->d_sb,xid);
982 if (rc) {
983 cFYI(1, ("error on getting revalidate info %d", rc));
984/* if (rc != -ENOENT)
985 rc = 0; */ /* BB should we cache info on
986 certain errors? */
987 }
988 } else {
989 rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL,
990 direntry->d_sb,xid);
991 if (rc) {
992 cFYI(1, ("error on getting revalidate info %d", rc));
993/* if (rc != -ENOENT)
994 rc = 0; */ /* BB should we cache info on
995 certain errors? */
996 }
997 }
998 /* should we remap certain errors, access denied?, to zero */
999
1000 /* if not oplocked, we invalidate inode pages if mtime or file size
1001 had changed on server */
1002
1003 if (timespec_equal(&local_mtime,&direntry->d_inode->i_mtime) &&
1004 (local_size == direntry->d_inode->i_size)) {
1005 cFYI(1, ("cifs_revalidate - inode unchanged"));
1006 } else {
1007 /* file may have changed on server */
1008 if (cifsInode->clientCanCacheRead) {
1009 /* no need to invalidate inode pages since we were the
1010 only ones who could have modified the file and the
1011 server copy is staler than ours */
1012 } else {
1013 invalidate_inode = TRUE;
1014 }
1015 }
1016
1017 /* can not grab this sem since kernel filesys locking documentation
1018 indicates i_sem may be taken by the kernel on lookup and rename
1019 which could deadlock if we grab the i_sem here as well */
1020/* down(&direntry->d_inode->i_sem);*/
1021 /* need to write out dirty pages here */
1022 if (direntry->d_inode->i_mapping) {
1023 /* do we need to lock inode until after invalidate completes
1024 below? */
1025 filemap_fdatawrite(direntry->d_inode->i_mapping);
1026 }
1027 if (invalidate_inode) {
1028 if (direntry->d_inode->i_mapping)
1029 filemap_fdatawait(direntry->d_inode->i_mapping);
1030 /* may eventually have to do this for open files too */
1031 if (list_empty(&(cifsInode->openFileList))) {
1032 /* Has changed on server - flush read ahead pages */
1033 cFYI(1, ("Invalidating read ahead data on "
1034 "closed file"));
1035 invalidate_remote_inode(direntry->d_inode);
1036 }
1037 }
1038/* up(&direntry->d_inode->i_sem); */
1039
1040 kfree(full_path);
1041 FreeXid(xid);
1042 return rc;
1043}
1044
1045int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1046 struct kstat *stat)
1047{
1048 int err = cifs_revalidate(dentry);
1049 if (!err)
1050 generic_fillattr(dentry->d_inode, stat);
1051 return err;
1052}
1053
1054static int cifs_truncate_page(struct address_space *mapping, loff_t from)
1055{
1056 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1057 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
1058 struct page *page;
1059 char *kaddr;
1060 int rc = 0;
1061
1062 page = grab_cache_page(mapping, index);
1063 if (!page)
1064 return -ENOMEM;
1065
1066 kaddr = kmap_atomic(page, KM_USER0);
1067 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1068 flush_dcache_page(page);
1069 kunmap_atomic(kaddr, KM_USER0);
1070 unlock_page(page);
1071 page_cache_release(page);
1072 return rc;
1073}
1074
1075int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
1076{
1077 int xid;
1078 struct cifs_sb_info *cifs_sb;
1079 struct cifsTconInfo *pTcon;
1080 char *full_path = NULL;
1081 int rc = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 struct cifsFileInfo *open_file = NULL;
1083 FILE_BASIC_INFO time_buf;
1084 int set_time = FALSE;
1085 __u64 mode = 0xFFFFFFFFFFFFFFFFULL;
1086 __u64 uid = 0xFFFFFFFFFFFFFFFFULL;
1087 __u64 gid = 0xFFFFFFFFFFFFFFFFULL;
1088 struct cifsInodeInfo *cifsInode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 xid = GetXid();
1091
Steve French3020a1f2005-11-18 11:31:10 -08001092 cFYI(1, ("In cifs_setattr, name = %s attrs->iavalid 0x%x ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 direntry->d_name.name, attrs->ia_valid));
1094 cifs_sb = CIFS_SB(direntry->d_inode->i_sb);
1095 pTcon = cifs_sb->tcon;
1096
1097 down(&direntry->d_sb->s_vfs_rename_sem);
Steve French7f573562005-08-30 11:32:14 -07001098 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 up(&direntry->d_sb->s_vfs_rename_sem);
1100 if (full_path == NULL) {
1101 FreeXid(xid);
1102 return -ENOMEM;
1103 }
1104 cifsInode = CIFS_I(direntry->d_inode);
1105
1106 /* BB check if we need to refresh inode from server now ? BB */
1107
1108 /* need to flush data before changing file size on server */
1109 filemap_fdatawrite(direntry->d_inode->i_mapping);
1110 filemap_fdatawait(direntry->d_inode->i_mapping);
1111
1112 if (attrs->ia_valid & ATTR_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 /* To avoid spurious oplock breaks from server, in the case of
1114 inodes that we already have open, avoid doing path based
1115 setting of file size if we can do it by handle.
1116 This keeps our caching token (oplock) and avoids timeouts
1117 when the local oplock break takes longer to flush
1118 writebehind data than the SMB timeout for the SetPathInfo
1119 request would allow */
Steve French6148a742005-10-05 12:23:19 -07001120 open_file = find_writable_file(cifsInode);
1121 if (open_file) {
1122 __u16 nfid = open_file->netfid;
1123 __u32 npid = open_file->pid;
1124 rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size,
1125 nfid, npid, FALSE);
Steve French23e7dd72005-10-20 13:44:56 -07001126 atomic_dec(&open_file->wrtPending);
Steve French6148a742005-10-05 12:23:19 -07001127 cFYI(1,("SetFSize for attrs rc = %d", rc));
1128 if(rc == -EINVAL) {
1129 int bytes_written;
1130 rc = CIFSSMBWrite(xid, pTcon,
1131 nfid, 0, attrs->ia_size,
1132 &bytes_written, NULL, NULL,
1133 1 /* 45 seconds */);
1134 cFYI(1,("Wrt seteof rc %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (rc != 0) {
1138 /* Set file size by pathname rather than by handle
1139 either because no valid, writeable file handle for
1140 it was found or because there was an error setting
1141 it by handle */
1142 rc = CIFSSMBSetEOF(xid, pTcon, full_path,
1143 attrs->ia_size, FALSE,
Steve French737b7582005-04-28 22:41:06 -07001144 cifs_sb->local_nls,
1145 cifs_sb->mnt_cifs_flags &
1146 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve Frenche30dcf32005-09-20 20:49:16 -07001147 cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc));
1148 if(rc == -EINVAL) {
1149 __u16 netfid;
1150 int oplock = FALSE;
1151
1152 rc = SMBLegacyOpen(xid, pTcon, full_path,
1153 FILE_OPEN,
1154 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1155 CREATE_NOT_DIR, &netfid, &oplock,
1156 NULL, cifs_sb->local_nls,
1157 cifs_sb->mnt_cifs_flags &
1158 CIFS_MOUNT_MAP_SPECIAL_CHR);
1159 if (rc==0) {
1160 int bytes_written;
1161 rc = CIFSSMBWrite(xid, pTcon,
1162 netfid, 0,
1163 attrs->ia_size,
1164 &bytes_written, NULL,
1165 NULL, 1 /* 45 sec */);
1166 cFYI(1,("wrt seteof rc %d",rc));
1167 CIFSSMBClose(xid, pTcon, netfid);
1168 }
1169
1170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172
1173 /* Server is ok setting allocation size implicitly - no need
1174 to call:
1175 CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, TRUE,
1176 cifs_sb->local_nls);
1177 */
1178
1179 if (rc == 0) {
1180 rc = vmtruncate(direntry->d_inode, attrs->ia_size);
1181 cifs_truncate_page(direntry->d_inode->i_mapping,
1182 direntry->d_inode->i_size);
Steve Frenche30dcf32005-09-20 20:49:16 -07001183 } else
1184 goto cifs_setattr_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186 if (attrs->ia_valid & ATTR_UID) {
Steve Frenche30dcf32005-09-20 20:49:16 -07001187 cFYI(1, ("UID changed to %d", attrs->ia_uid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 uid = attrs->ia_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
1190 if (attrs->ia_valid & ATTR_GID) {
Steve Frenche30dcf32005-09-20 20:49:16 -07001191 cFYI(1, ("GID changed to %d", attrs->ia_gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 gid = attrs->ia_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
1194
1195 time_buf.Attributes = 0;
1196 if (attrs->ia_valid & ATTR_MODE) {
Steve Frenche30dcf32005-09-20 20:49:16 -07001197 cFYI(1, ("Mode changed to 0x%x", attrs->ia_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 mode = attrs->ia_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
1200
1201 if ((cifs_sb->tcon->ses->capabilities & CAP_UNIX)
1202 && (attrs->ia_valid & (ATTR_MODE | ATTR_GID | ATTR_UID)))
1203 rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode, uid, gid,
Steve French737b7582005-04-28 22:41:06 -07001204 0 /* dev_t */, cifs_sb->local_nls,
1205 cifs_sb->mnt_cifs_flags &
1206 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 else if (attrs->ia_valid & ATTR_MODE) {
1208 if ((mode & S_IWUGO) == 0) /* not writeable */ {
1209 if ((cifsInode->cifsAttrs & ATTR_READONLY) == 0)
1210 time_buf.Attributes =
1211 cpu_to_le32(cifsInode->cifsAttrs |
1212 ATTR_READONLY);
1213 } else if ((mode & S_IWUGO) == S_IWUGO) {
1214 if (cifsInode->cifsAttrs & ATTR_READONLY)
1215 time_buf.Attributes =
1216 cpu_to_le32(cifsInode->cifsAttrs &
1217 (~ATTR_READONLY));
1218 }
1219 /* BB to be implemented -
1220 via Windows security descriptors or streams */
1221 /* CIFSSMBWinSetPerms(xid, pTcon, full_path, mode, uid, gid,
1222 cifs_sb->local_nls); */
1223 }
1224
1225 if (attrs->ia_valid & ATTR_ATIME) {
1226 set_time = TRUE;
1227 time_buf.LastAccessTime =
1228 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1229 } else
1230 time_buf.LastAccessTime = 0;
1231
1232 if (attrs->ia_valid & ATTR_MTIME) {
1233 set_time = TRUE;
1234 time_buf.LastWriteTime =
1235 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1236 } else
1237 time_buf.LastWriteTime = 0;
Steve Frenche30dcf32005-09-20 20:49:16 -07001238 /* Do not set ctime explicitly unless other time
1239 stamps are changed explicitly (i.e. by utime()
1240 since we would then have a mix of client and
1241 server times */
1242
1243 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 set_time = TRUE;
Steve Frenche30dcf32005-09-20 20:49:16 -07001245 /* Although Samba throws this field away
1246 it may be useful to Windows - but we do
1247 not want to set ctime unless some other
1248 timestamp is changing */
1249 cFYI(1, ("CIFS - CTIME changed "));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 time_buf.ChangeTime =
1251 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1252 } else
1253 time_buf.ChangeTime = 0;
1254
1255 if (set_time || time_buf.Attributes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 time_buf.CreationTime = 0; /* do not change */
1257 /* In the future we should experiment - try setting timestamps
1258 via Handle (SetFileInfo) instead of by path */
1259 if (!(pTcon->ses->flags & CIFS_SES_NT4))
1260 rc = CIFSSMBSetTimes(xid, pTcon, full_path, &time_buf,
Steve French737b7582005-04-28 22:41:06 -07001261 cifs_sb->local_nls,
1262 cifs_sb->mnt_cifs_flags &
1263 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 else
1265 rc = -EOPNOTSUPP;
1266
1267 if (rc == -EOPNOTSUPP) {
1268 int oplock = FALSE;
1269 __u16 netfid;
1270
1271 cFYI(1, ("calling SetFileInfo since SetPathInfo for "
1272 "times not supported by this server"));
1273 /* BB we could scan to see if we already have it open
1274 and pass in pid of opener to function */
1275 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN,
1276 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1277 CREATE_NOT_DIR, &netfid, &oplock,
Steve French737b7582005-04-28 22:41:06 -07001278 NULL, cifs_sb->local_nls,
1279 cifs_sb->mnt_cifs_flags &
1280 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (rc==0) {
1282 rc = CIFSSMBSetFileTimes(xid, pTcon, &time_buf,
1283 netfid);
1284 CIFSSMBClose(xid, pTcon, netfid);
1285 } else {
1286 /* BB For even older servers we could convert time_buf
1287 into old DOS style which uses two second
1288 granularity */
1289
1290 /* rc = CIFSSMBSetTimesLegacy(xid, pTcon, full_path,
1291 &time_buf, cifs_sb->local_nls); */
1292 }
1293 }
Steve Frenche30dcf32005-09-20 20:49:16 -07001294 /* Even if error on time set, no sense failing the call if
1295 the server would set the time to a reasonable value anyway,
1296 and this check ensures that we are not being called from
1297 sys_utimes in which case we ought to fail the call back to
1298 the user when the server rejects the call */
1299 if((rc) && (attrs->ia_valid &&
1300 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
1301 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
1303
1304 /* do not need local check to inode_check_ok since the server does
1305 that */
1306 if (!rc)
1307 rc = inode_setattr(direntry->d_inode, attrs);
Steve Frenche30dcf32005-09-20 20:49:16 -07001308cifs_setattr_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 kfree(full_path);
1310 FreeXid(xid);
1311 return rc;
1312}
1313
1314void cifs_delete_inode(struct inode *inode)
1315{
1316 cFYI(1, ("In cifs_delete_inode, inode = 0x%p ", inode));
1317 /* may have to add back in if and when safe distributed caching of
1318 directories added e.g. via FindNotify */
1319}