blob: 8c23fb330ea9ac663c46f8fd94ea67da41e43921 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/inode.c
3 *
Steve French2dd29d32007-04-23 22:07:35 +00004 * Copyright (C) International Business Machines Corp., 2002,2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/stat.h>
23#include <linux/pagemap.h>
24#include <asm/div64.h>
25#include "cifsfs.h"
26#include "cifspdu.h"
27#include "cifsglob.h"
28#include "cifsproto.h"
29#include "cifs_debug.h"
30#include "cifs_fs_sb.h"
31
Christoph Hellwig70eff552008-02-15 20:55:05 +000032
33static void cifs_set_ops(struct inode *inode)
34{
35 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
36
37 switch (inode->i_mode & S_IFMT) {
38 case S_IFREG:
39 inode->i_op = &cifs_file_inode_ops;
40 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) {
41 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
42 inode->i_fop = &cifs_file_direct_nobrl_ops;
43 else
44 inode->i_fop = &cifs_file_direct_ops;
45 } else if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL)
46 inode->i_fop = &cifs_file_nobrl_ops;
47 else { /* not direct, send byte range locks */
48 inode->i_fop = &cifs_file_ops;
49 }
50
51
52 /* check if server can support readpages */
53 if (cifs_sb->tcon->ses->server->maxBuf <
54 PAGE_CACHE_SIZE + MAX_CIFS_HDR_SIZE)
55 inode->i_data.a_ops = &cifs_addr_ops_smallbuf;
56 else
57 inode->i_data.a_ops = &cifs_addr_ops;
58 break;
59 case S_IFDIR:
60 inode->i_op = &cifs_dir_inode_ops;
61 inode->i_fop = &cifs_dir_ops;
62 break;
63 case S_IFLNK:
64 inode->i_op = &cifs_symlink_inode_ops;
65 break;
66 default:
67 init_special_inode(inode, inode->i_mode, inode->i_rdev);
68 break;
69 }
70}
71
Christoph Hellwig75f12982008-02-25 20:25:21 +000072static void cifs_unix_info_to_inode(struct inode *inode,
73 FILE_UNIX_BASIC_INFO *info, int force_uid_gid)
74{
75 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
76 struct cifsInodeInfo *cifsInfo = CIFS_I(inode);
77 __u64 num_of_bytes = le64_to_cpu(info->NumOfBytes);
78 __u64 end_of_file = le64_to_cpu(info->EndOfFile);
79
80 inode->i_atime = cifs_NTtimeToUnix(le64_to_cpu(info->LastAccessTime));
81 inode->i_mtime =
82 cifs_NTtimeToUnix(le64_to_cpu(info->LastModificationTime));
83 inode->i_ctime = cifs_NTtimeToUnix(le64_to_cpu(info->LastStatusChange));
84 inode->i_mode = le64_to_cpu(info->Permissions);
85
86 /*
87 * Since we set the inode type below we need to mask off
88 * to avoid strange results if bits set above.
89 */
90 inode->i_mode &= ~S_IFMT;
91 switch (le32_to_cpu(info->Type)) {
92 case UNIX_FILE:
93 inode->i_mode |= S_IFREG;
94 break;
95 case UNIX_SYMLINK:
96 inode->i_mode |= S_IFLNK;
97 break;
98 case UNIX_DIR:
99 inode->i_mode |= S_IFDIR;
100 break;
101 case UNIX_CHARDEV:
102 inode->i_mode |= S_IFCHR;
103 inode->i_rdev = MKDEV(le64_to_cpu(info->DevMajor),
104 le64_to_cpu(info->DevMinor) & MINORMASK);
105 break;
106 case UNIX_BLOCKDEV:
107 inode->i_mode |= S_IFBLK;
108 inode->i_rdev = MKDEV(le64_to_cpu(info->DevMajor),
109 le64_to_cpu(info->DevMinor) & MINORMASK);
110 break;
111 case UNIX_FIFO:
112 inode->i_mode |= S_IFIFO;
113 break;
114 case UNIX_SOCKET:
115 inode->i_mode |= S_IFSOCK;
116 break;
117 default:
118 /* safest to call it a file if we do not know */
119 inode->i_mode |= S_IFREG;
120 cFYI(1, ("unknown type %d", le32_to_cpu(info->Type)));
121 break;
122 }
123
124 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID) &&
125 !force_uid_gid)
126 inode->i_uid = cifs_sb->mnt_uid;
127 else
128 inode->i_uid = le64_to_cpu(info->Uid);
129
130 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID) &&
131 !force_uid_gid)
132 inode->i_gid = cifs_sb->mnt_gid;
133 else
134 inode->i_gid = le64_to_cpu(info->Gid);
135
136 inode->i_nlink = le64_to_cpu(info->Nlinks);
137
138 spin_lock(&inode->i_lock);
139 if (is_size_safe_to_change(cifsInfo, end_of_file)) {
140 /*
141 * We can not safely change the file size here if the client
142 * is writing to it due to potential races.
143 */
144 i_size_write(inode, end_of_file);
145
146 /*
147 * i_blocks is not related to (i_size / i_blksize),
148 * but instead 512 byte (2**9) size is required for
149 * calculating num blocks.
150 */
151 inode->i_blocks = (512 - 1 + num_of_bytes) >> 9;
152 }
153 spin_unlock(&inode->i_lock);
154}
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156int cifs_get_inode_info_unix(struct inode **pinode,
157 const unsigned char *search_path, struct super_block *sb, int xid)
158{
159 int rc = 0;
160 FILE_UNIX_BASIC_INFO findData;
161 struct cifsTconInfo *pTcon;
162 struct inode *inode;
163 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
164 char *tmp_path;
165
166 pTcon = cifs_sb->tcon;
Steve French26a21b92006-05-31 18:05:34 +0000167 cFYI(1, ("Getting info on %s", search_path));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 /* could have done a find first instead but this returns more info */
169 rc = CIFSSMBUnixQPathInfo(xid, pTcon, search_path, &findData,
Steve French737b7582005-04-28 22:41:06 -0700170 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
171 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/* dump_mem("\nUnixQPathInfo return data", &findData,
173 sizeof(findData)); */
174 if (rc) {
175 if (rc == -EREMOTE) {
176 tmp_path =
177 kmalloc(strnlen(pTcon->treeName,
178 MAX_TREE_SIZE + 1) +
179 strnlen(search_path, MAX_PATHCONF) + 1,
180 GFP_KERNEL);
Steve Frenchf6d09982008-01-08 23:18:22 +0000181 if (tmp_path == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return -ENOMEM;
Steve Frenchf6d09982008-01-08 23:18:22 +0000183
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000184 /* have to skip first of the double backslash of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 UNC name */
186 strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE);
187 strncat(tmp_path, search_path, MAX_PATHCONF);
188 rc = connect_to_dfs_path(xid, pTcon->ses,
189 /* treename + */ tmp_path,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000190 cifs_sb->local_nls,
191 cifs_sb->mnt_cifs_flags &
Steve French737b7582005-04-28 22:41:06 -0700192 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 kfree(tmp_path);
194
195 /* BB fix up inode etc. */
196 } else if (rc) {
197 return rc;
198 }
199 } else {
200 struct cifsInodeInfo *cifsInfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 __u64 num_of_bytes = le64_to_cpu(findData.NumOfBytes);
202 __u64 end_of_file = le64_to_cpu(findData.EndOfFile);
203
204 /* get new inode */
205 if (*pinode == NULL) {
206 *pinode = new_inode(sb);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000207 if (*pinode == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return -ENOMEM;
209 /* Is an i_ino of zero legal? */
210 /* Are there sanity checks we can use to ensure that
211 the server is really filling in that field? */
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700212 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 (*pinode)->i_ino =
214 (unsigned long)findData.UniqueId;
215 } /* note ino incremented to unique num in new_inode */
Steve French4523cc32007-04-30 20:13:06 +0000216 if (sb->s_flags & MS_NOATIME)
Steve French1b2b2122007-02-17 04:30:54 +0000217 (*pinode)->i_flags |= S_NOATIME | S_NOCMTIME;
Steve French50c2f752007-07-13 00:33:32 +0000218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 insert_inode_hash(*pinode);
220 }
221
222 inode = *pinode;
223 cifsInfo = CIFS_I(inode);
224
Steve French26a21b92006-05-31 18:05:34 +0000225 cFYI(1, ("Old time %ld", cifsInfo->time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 cifsInfo->time = jiffies;
Steve French26a21b92006-05-31 18:05:34 +0000227 cFYI(1, ("New time %ld", cifsInfo->time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /* this is ok to set on every inode revalidate */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000229 atomic_set(&cifsInfo->inUse, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Christoph Hellwig75f12982008-02-25 20:25:21 +0000231 cifs_unix_info_to_inode(inode, &findData, 0);
Steve French50c2f752007-07-13 00:33:32 +0000232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 if (num_of_bytes < end_of_file)
Steve French8b94bcb2005-11-11 11:41:00 -0800235 cFYI(1, ("allocation size less than end of file"));
Andrew Morton5515eff2006-03-26 01:37:53 -0800236 cFYI(1, ("Size %ld and blocks %llu",
237 (unsigned long) inode->i_size,
238 (unsigned long long)inode->i_blocks));
Steve French8b94bcb2005-11-11 11:41:00 -0800239
Christoph Hellwig70eff552008-02-15 20:55:05 +0000240 cifs_set_ops(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242 return rc;
243}
244
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000245static int decode_sfu_inode(struct inode *inode, __u64 size,
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800246 const unsigned char *path,
247 struct cifs_sb_info *cifs_sb, int xid)
248{
249 int rc;
250 int oplock = FALSE;
251 __u16 netfid;
252 struct cifsTconInfo *pTcon = cifs_sb->tcon;
Steve French86c96b42005-11-18 20:25:31 -0800253 char buf[24];
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800254 unsigned int bytes_read;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000255 char *pbuf;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800256
257 pbuf = buf;
258
Steve French4523cc32007-04-30 20:13:06 +0000259 if (size == 0) {
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800260 inode->i_mode |= S_IFIFO;
261 return 0;
262 } else if (size < 8) {
263 return -EINVAL; /* EOPNOTSUPP? */
264 }
Steve French50c2f752007-07-13 00:33:32 +0000265
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800266 rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ,
267 CREATE_NOT_DIR, &netfid, &oplock, NULL,
268 cifs_sb->local_nls,
269 cifs_sb->mnt_cifs_flags &
270 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000271 if (rc == 0) {
Steve Frenchec637e32005-12-12 20:53:18 -0800272 int buf_type = CIFS_NO_BUFFER;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800273 /* Read header */
274 rc = CIFSSMBRead(xid, pTcon,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000275 netfid,
Steve French86c96b42005-11-18 20:25:31 -0800276 24 /* length */, 0 /* offset */,
Steve Frenchec637e32005-12-12 20:53:18 -0800277 &bytes_read, &pbuf, &buf_type);
Steve French4523cc32007-04-30 20:13:06 +0000278 if ((rc == 0) && (bytes_read >= 8)) {
279 if (memcmp("IntxBLK", pbuf, 8) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000280 cFYI(1, ("Block device"));
Steve French3020a1f2005-11-18 11:31:10 -0800281 inode->i_mode |= S_IFBLK;
Steve French4523cc32007-04-30 20:13:06 +0000282 if (bytes_read == 24) {
Steve French86c96b42005-11-18 20:25:31 -0800283 /* we have enough to decode dev num */
284 __u64 mjr; /* major */
285 __u64 mnr; /* minor */
286 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
287 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
288 inode->i_rdev = MKDEV(mjr, mnr);
289 }
Steve French4523cc32007-04-30 20:13:06 +0000290 } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000291 cFYI(1, ("Char device"));
Steve French3020a1f2005-11-18 11:31:10 -0800292 inode->i_mode |= S_IFCHR;
Steve French4523cc32007-04-30 20:13:06 +0000293 if (bytes_read == 24) {
Steve French86c96b42005-11-18 20:25:31 -0800294 /* we have enough to decode dev num */
295 __u64 mjr; /* major */
296 __u64 mnr; /* minor */
297 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
298 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
299 inode->i_rdev = MKDEV(mjr, mnr);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000300 }
Steve French4523cc32007-04-30 20:13:06 +0000301 } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000302 cFYI(1, ("Symlink"));
Steve French3020a1f2005-11-18 11:31:10 -0800303 inode->i_mode |= S_IFLNK;
Steve French86c96b42005-11-18 20:25:31 -0800304 } else {
305 inode->i_mode |= S_IFREG; /* file? */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000306 rc = -EOPNOTSUPP;
Steve French86c96b42005-11-18 20:25:31 -0800307 }
Steve French3020a1f2005-11-18 11:31:10 -0800308 } else {
309 inode->i_mode |= S_IFREG; /* then it is a file */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000310 rc = -EOPNOTSUPP; /* or some unknown SFU type */
311 }
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800312 CIFSSMBClose(xid, pTcon, netfid);
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800313 }
314 return rc;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800315}
316
Steve French9e294f12005-11-17 16:59:21 -0800317#define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
318
Steve French953f8682007-10-31 04:54:42 +0000319static int get_sfu_mode(struct inode *inode,
Steve French9e294f12005-11-17 16:59:21 -0800320 const unsigned char *path,
321 struct cifs_sb_info *cifs_sb, int xid)
322{
Steve French3020a1f2005-11-18 11:31:10 -0800323#ifdef CONFIG_CIFS_XATTR
Steve French9e294f12005-11-17 16:59:21 -0800324 ssize_t rc;
325 char ea_value[4];
326 __u32 mode;
327
328 rc = CIFSSMBQueryEA(xid, cifs_sb->tcon, path, "SETFILEBITS",
329 ea_value, 4 /* size of buf */, cifs_sb->local_nls,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000330 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve French4523cc32007-04-30 20:13:06 +0000331 if (rc < 0)
Steve French9e294f12005-11-17 16:59:21 -0800332 return (int)rc;
333 else if (rc > 3) {
334 mode = le32_to_cpu(*((__le32 *)ea_value));
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000335 inode->i_mode &= ~SFBITS_MASK;
336 cFYI(1, ("special bits 0%o org mode 0%o", mode, inode->i_mode));
Steve French9e294f12005-11-17 16:59:21 -0800337 inode->i_mode = (mode & SFBITS_MASK) | inode->i_mode;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000338 cFYI(1, ("special mode bits 0%o", mode));
Steve French9e294f12005-11-17 16:59:21 -0800339 return 0;
340 } else {
341 return 0;
342 }
Steve French3020a1f2005-11-18 11:31:10 -0800343#else
344 return -EOPNOTSUPP;
345#endif
Steve French9e294f12005-11-17 16:59:21 -0800346}
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348int cifs_get_inode_info(struct inode **pinode,
349 const unsigned char *search_path, FILE_ALL_INFO *pfindData,
350 struct super_block *sb, int xid)
351{
352 int rc = 0;
353 struct cifsTconInfo *pTcon;
354 struct inode *inode;
355 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
356 char *tmp_path;
357 char *buf = NULL;
Steve French8d6286f2006-11-16 22:48:25 +0000358 int adjustTZ = FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 pTcon = cifs_sb->tcon;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000361 cFYI(1, ("Getting info on %s", search_path));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700363 if ((pfindData == NULL) && (*pinode != NULL)) {
364 if (CIFS_I(*pinode)->clientCanCacheRead) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000365 cFYI(1, ("No need to revalidate cached inode sizes"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return rc;
367 }
368 }
369
370 /* if file info not passed in then get it from server */
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700371 if (pfindData == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700373 if (buf == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return -ENOMEM;
375 pfindData = (FILE_ALL_INFO *)buf;
376 /* could do find first instead but this returns more info */
377 rc = CIFSSMBQPathInfo(xid, pTcon, search_path, pfindData,
Steve Frenchacf1a1b2006-10-12 03:28:28 +0000378 0 /* not legacy */,
Steve French6b8edfe2005-08-23 20:26:03 -0700379 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
Steve French737b7582005-04-28 22:41:06 -0700380 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve French6b8edfe2005-08-23 20:26:03 -0700381 /* BB optimize code so we do not make the above call
382 when server claims no NT SMB support and the above call
383 failed at least once - set flag in tcon or mount */
Steve French4523cc32007-04-30 20:13:06 +0000384 if ((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
Steve French6b8edfe2005-08-23 20:26:03 -0700385 rc = SMBQueryInformation(xid, pTcon, search_path,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000386 pfindData, cifs_sb->local_nls,
Steve French6b8edfe2005-08-23 20:26:03 -0700387 cifs_sb->mnt_cifs_flags &
388 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve French8d6286f2006-11-16 22:48:25 +0000389 adjustTZ = TRUE;
Steve French6b8edfe2005-08-23 20:26:03 -0700390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392 /* dump_mem("\nQPathInfo return data",&findData, sizeof(findData)); */
393 if (rc) {
394 if (rc == -EREMOTE) {
395 tmp_path =
396 kmalloc(strnlen
397 (pTcon->treeName,
398 MAX_TREE_SIZE + 1) +
399 strnlen(search_path, MAX_PATHCONF) + 1,
400 GFP_KERNEL);
401 if (tmp_path == NULL) {
402 kfree(buf);
403 return -ENOMEM;
404 }
405
406 strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE);
407 strncat(tmp_path, search_path, MAX_PATHCONF);
408 rc = connect_to_dfs_path(xid, pTcon->ses,
409 /* treename + */ tmp_path,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000410 cifs_sb->local_nls,
411 cifs_sb->mnt_cifs_flags &
Steve French737b7582005-04-28 22:41:06 -0700412 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 kfree(tmp_path);
414 /* BB fix up inode etc. */
415 } else if (rc) {
416 kfree(buf);
417 return rc;
418 }
419 } else {
420 struct cifsInodeInfo *cifsInfo;
421 __u32 attr = le32_to_cpu(pfindData->Attributes);
422
423 /* get new inode */
424 if (*pinode == NULL) {
425 *pinode = new_inode(sb);
Steve Frenchacf1a1b2006-10-12 03:28:28 +0000426 if (*pinode == NULL) {
427 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return -ENOMEM;
Steve Frenchacf1a1b2006-10-12 03:28:28 +0000429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /* Is an i_ino of zero legal? Can we use that to check
431 if the server supports returning inode numbers? Are
432 there other sanity checks we can use to ensure that
433 the server is really filling in that field? */
434
435 /* We can not use the IndexNumber field by default from
436 Windows or Samba (in ALL_INFO buf) but we can request
437 it explicitly. It may not be unique presumably if
438 the server has multiple devices mounted under one
439 share */
440
441 /* There may be higher info levels that work but are
442 there Windows server or network appliances for which
443 IndexNumber field is not guaranteed unique? */
444
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000445 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 int rc1 = 0;
447 __u64 inode_num;
448
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000449 rc1 = CIFSGetSrvInodeNumber(xid, pTcon,
450 search_path, &inode_num,
Steve French737b7582005-04-28 22:41:06 -0700451 cifs_sb->local_nls,
452 cifs_sb->mnt_cifs_flags &
453 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700454 if (rc1) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000455 cFYI(1, ("GetSrvInodeNum rc %d", rc1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 /* BB EOPNOSUPP disable SERVER_INUM? */
457 } else /* do we need cast or hash to ino? */
458 (*pinode)->i_ino = inode_num;
459 } /* else ino incremented to unique num in new_inode*/
Steve French4523cc32007-04-30 20:13:06 +0000460 if (sb->s_flags & MS_NOATIME)
Steve French1b2b2122007-02-17 04:30:54 +0000461 (*pinode)->i_flags |= S_NOATIME | S_NOCMTIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 insert_inode_hash(*pinode);
463 }
464 inode = *pinode;
465 cifsInfo = CIFS_I(inode);
466 cifsInfo->cifsAttrs = attr;
Steve French26a21b92006-05-31 18:05:34 +0000467 cFYI(1, ("Old time %ld", cifsInfo->time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 cifsInfo->time = jiffies;
Steve French26a21b92006-05-31 18:05:34 +0000469 cFYI(1, ("New time %ld", cifsInfo->time));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 /* blksize needs to be multiple of two. So safer to default to
472 blksize and blkbits set in superblock so 2**blkbits and blksize
473 will match rather than setting to:
474 (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
475
Steve French26a21b92006-05-31 18:05:34 +0000476 /* Linux can not store file creation time so ignore it */
Steve French4523cc32007-04-30 20:13:06 +0000477 if (pfindData->LastAccessTime)
Steve French1bd5bbc2006-09-28 03:35:57 +0000478 inode->i_atime = cifs_NTtimeToUnix
479 (le64_to_cpu(pfindData->LastAccessTime));
480 else /* do not need to use current_fs_time - time not stored */
481 inode->i_atime = CURRENT_TIME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 inode->i_mtime =
483 cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime));
484 inode->i_ctime =
485 cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime));
Steve French26a21b92006-05-31 18:05:34 +0000486 cFYI(0, ("Attributes came in as 0x%x", attr));
Steve French4523cc32007-04-30 20:13:06 +0000487 if (adjustTZ && (pTcon->ses) && (pTcon->ses->server)) {
Steve French8d6286f2006-11-16 22:48:25 +0000488 inode->i_ctime.tv_sec += pTcon->ses->server->timeAdj;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000489 inode->i_mtime.tv_sec += pTcon->ses->server->timeAdj;
Steve French8d6286f2006-11-16 22:48:25 +0000490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 /* set default mode. will override for dirs below */
493 if (atomic_read(&cifsInfo->inUse) == 0)
494 /* new inode, can safely set these fields */
495 inode->i_mode = cifs_sb->mnt_file_mode;
Steve French3020a1f2005-11-18 11:31:10 -0800496 else /* since we set the inode type below we need to mask off
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000497 to avoid strange results if type changes and both
498 get orred in */
499 inode->i_mode &= ~S_IFMT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500/* if (attr & ATTR_REPARSE) */
501 /* We no longer handle these as symlinks because we could not
502 follow them due to the absolute path with drive letter */
503 if (attr & ATTR_DIRECTORY) {
504 /* override default perms since we do not do byte range locking
505 on dirs */
506 inode->i_mode = cifs_sb->mnt_dir_mode;
507 inode->i_mode |= S_IFDIR;
Steve Frencheda3c0292005-07-21 15:20:28 -0700508 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
509 (cifsInfo->cifsAttrs & ATTR_SYSTEM) &&
510 /* No need to le64 convert size of zero */
511 (pfindData->EndOfFile == 0)) {
512 inode->i_mode = cifs_sb->mnt_file_mode;
513 inode->i_mode |= S_IFIFO;
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800514/* BB Finish for SFU style symlinks and devices */
515 } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) &&
516 (cifsInfo->cifsAttrs & ATTR_SYSTEM)) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000517 if (decode_sfu_inode(inode,
Steve Frenchd6e2f2a42005-11-15 16:43:39 -0800518 le64_to_cpu(pfindData->EndOfFile),
519 search_path,
Steve Frenchad7a2922008-02-07 23:25:02 +0000520 cifs_sb, xid))
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000521 cFYI(1, ("Unrecognized sfu inode type"));
Steve Frenchad7a2922008-02-07 23:25:02 +0000522
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000523 cFYI(1, ("sfu mode 0%o", inode->i_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 } else {
525 inode->i_mode |= S_IFREG;
526 /* treat the dos attribute of read-only as read-only
527 mode e.g. 555 */
528 if (cifsInfo->cifsAttrs & ATTR_READONLY)
529 inode->i_mode &= ~(S_IWUGO);
Alan Tysonf5c1e2e2007-03-10 06:05:14 +0000530 else if ((inode->i_mode & S_IWUGO) == 0)
531 /* the ATTR_READONLY flag may have been */
532 /* changed on server -- set any w bits */
533 /* allowed by mnt_file_mode */
534 inode->i_mode |= (S_IWUGO &
535 cifs_sb->mnt_file_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /* BB add code here -
537 validate if device or weird share or device type? */
538 }
Steve French50c2f752007-07-13 00:33:32 +0000539
Steve French3677db12007-02-26 16:46:11 +0000540 spin_lock(&inode->i_lock);
Steve Frenchf6d09982008-01-08 23:18:22 +0000541 if (is_size_safe_to_change(cifsInfo,
542 le64_to_cpu(pfindData->EndOfFile))) {
Steve French7ba52632007-02-08 18:14:13 +0000543 /* can not safely shrink the file size here if the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 client is writing to it due to potential races */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000545 i_size_write(inode, le64_to_cpu(pfindData->EndOfFile));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 /* 512 bytes (2**9) is the fake blocksize that must be
548 used for this calculation */
549 inode->i_blocks = (512 - 1 + le64_to_cpu(
550 pfindData->AllocationSize)) >> 9;
551 }
Steve French3677db12007-02-26 16:46:11 +0000552 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 inode->i_nlink = le32_to_cpu(pfindData->NumberOfLinks);
555
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000556 /* BB fill in uid and gid here? with help from winbind?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 or retrieve from NTFS stream extended attribute */
Steve French4879b442007-10-19 21:57:39 +0000558#ifdef CONFIG_CIFS_EXPERIMENTAL
Steve French953f8682007-10-31 04:54:42 +0000559 /* fill in 0777 bits from ACL */
Steve French4879b442007-10-19 21:57:39 +0000560 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
561 cFYI(1, ("Getting mode bits from ACL"));
Steve French630f3f0c2007-10-25 21:17:17 +0000562 acl_to_uid_mode(inode, search_path);
Steve French4879b442007-10-19 21:57:39 +0000563 }
564#endif
Steve French4523cc32007-04-30 20:13:06 +0000565 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
Steve French953f8682007-10-31 04:54:42 +0000566 /* fill in remaining high mode bits e.g. SUID, VTX */
567 get_sfu_mode(inode, search_path, cifs_sb, xid);
Steve French9e294f12005-11-17 16:59:21 -0800568 } else if (atomic_read(&cifsInfo->inUse) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 inode->i_uid = cifs_sb->mnt_uid;
570 inode->i_gid = cifs_sb->mnt_gid;
571 /* set so we do not keep refreshing these fields with
572 bad data after user has changed them in memory */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000573 atomic_set(&cifsInfo->inUse, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575
Christoph Hellwig70eff552008-02-15 20:55:05 +0000576 cifs_set_ops(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578 kfree(buf);
579 return rc;
580}
581
Steve French7f8ed422007-09-28 22:28:55 +0000582static const struct inode_operations cifs_ipc_inode_ops = {
583 .lookup = cifs_lookup,
584};
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/* gets root inode */
David Howellsce634ab2008-02-07 00:15:33 -0800587struct inode *cifs_iget(struct super_block *sb, unsigned long ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
David Howellsce634ab2008-02-07 00:15:33 -0800589 int xid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 struct cifs_sb_info *cifs_sb;
David Howellsce634ab2008-02-07 00:15:33 -0800591 struct inode *inode;
592 long rc;
593
594 inode = iget_locked(sb, ino);
595 if (!inode)
596 return ERR_PTR(-ENOMEM);
597 if (!(inode->i_state & I_NEW))
598 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 cifs_sb = CIFS_SB(inode->i_sb);
601 xid = GetXid();
Steve Frenchc18c8422007-07-18 23:21:09 +0000602
603 if (cifs_sb->tcon->unix_ext)
Steve French7f8ed422007-09-28 22:28:55 +0000604 rc = cifs_get_inode_info_unix(&inode, "", inode->i_sb, xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 else
Steve French7f8ed422007-09-28 22:28:55 +0000606 rc = cifs_get_inode_info(&inode, "", NULL, inode->i_sb, xid);
607 if (rc && cifs_sb->tcon->ipc) {
608 cFYI(1, ("ipc connection - fake read inode"));
609 inode->i_mode |= S_IFDIR;
610 inode->i_nlink = 2;
611 inode->i_op = &cifs_ipc_inode_ops;
612 inode->i_fop = &simple_dir_operations;
613 inode->i_uid = cifs_sb->mnt_uid;
614 inode->i_gid = cifs_sb->mnt_gid;
David Howellsce634ab2008-02-07 00:15:33 -0800615 _FreeXid(xid);
616 iget_failed(inode);
617 return ERR_PTR(rc);
Steve French7f8ed422007-09-28 22:28:55 +0000618 }
619
David Howellsce634ab2008-02-07 00:15:33 -0800620 unlock_new_inode(inode);
621
622 /* can not call macro FreeXid here since in a void func
623 * TODO: This is no longer true
624 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 _FreeXid(xid);
David Howellsce634ab2008-02-07 00:15:33 -0800626 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
629int cifs_unlink(struct inode *inode, struct dentry *direntry)
630{
631 int rc = 0;
632 int xid;
633 struct cifs_sb_info *cifs_sb;
634 struct cifsTconInfo *pTcon;
635 char *full_path = NULL;
636 struct cifsInodeInfo *cifsInode;
637 FILE_BASIC_INFO *pinfo_buf;
638
Steve French06bcfed2006-03-31 22:43:50 +0000639 cFYI(1, ("cifs_unlink, inode = 0x%p", inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 xid = GetXid();
642
Steve French4523cc32007-04-30 20:13:06 +0000643 if (inode)
Steve French6910ab32006-03-31 03:37:08 +0000644 cifs_sb = CIFS_SB(inode->i_sb);
645 else
Steve French06bcfed2006-03-31 22:43:50 +0000646 cifs_sb = CIFS_SB(direntry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 pTcon = cifs_sb->tcon;
648
649 /* Unlink can be called from rename so we can not grab the sem here
650 since we deadlock otherwise */
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800651/* mutex_lock(&direntry->d_sb->s_vfs_rename_mutex);*/
Steve French7f573562005-08-30 11:32:14 -0700652 full_path = build_path_from_dentry(direntry);
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800653/* mutex_unlock(&direntry->d_sb->s_vfs_rename_mutex);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (full_path == NULL) {
655 FreeXid(xid);
656 return -ENOMEM;
657 }
Steve French2d785a52007-07-15 01:48:57 +0000658
659 if ((pTcon->ses->capabilities & CAP_UNIX) &&
660 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
661 le64_to_cpu(pTcon->fsUnixInfo.Capability))) {
662 rc = CIFSPOSIXDelFile(xid, pTcon, full_path,
663 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
664 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
665 cFYI(1, ("posix del rc %d", rc));
666 if ((rc == 0) || (rc == -ENOENT))
667 goto psx_del_no_retry;
668 }
669
Steve French737b7582005-04-28 22:41:06 -0700670 rc = CIFSSMBDelFile(xid, pTcon, full_path, cifs_sb->local_nls,
671 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve French2d785a52007-07-15 01:48:57 +0000672psx_del_no_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (!rc) {
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700674 if (direntry->d_inode)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700675 drop_nlink(direntry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 } else if (rc == -ENOENT) {
677 d_drop(direntry);
678 } else if (rc == -ETXTBSY) {
679 int oplock = FALSE;
680 __u16 netfid;
681
682 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, DELETE,
683 CREATE_NOT_DIR | CREATE_DELETE_ON_CLOSE,
Steve French737b7582005-04-28 22:41:06 -0700684 &netfid, &oplock, NULL, cifs_sb->local_nls,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000685 cifs_sb->mnt_cifs_flags &
Steve French737b7582005-04-28 22:41:06 -0700686 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000687 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 CIFSSMBRenameOpenFile(xid, pTcon, netfid, NULL,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000689 cifs_sb->local_nls,
690 cifs_sb->mnt_cifs_flags &
Steve French737b7582005-04-28 22:41:06 -0700691 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 CIFSSMBClose(xid, pTcon, netfid);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700693 if (direntry->d_inode)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700694 drop_nlink(direntry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696 } else if (rc == -EACCES) {
697 /* try only if r/o attribute set in local lookup data? */
Eric Sesterhenna048d7a2006-02-21 22:33:09 +0000698 pinfo_buf = kzalloc(sizeof(FILE_BASIC_INFO), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (pinfo_buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /* ATTRS set to normal clears r/o bit */
701 pinfo_buf->Attributes = cpu_to_le32(ATTR_NORMAL);
702 if (!(pTcon->ses->flags & CIFS_SES_NT4))
703 rc = CIFSSMBSetTimes(xid, pTcon, full_path,
704 pinfo_buf,
Steve French737b7582005-04-28 22:41:06 -0700705 cifs_sb->local_nls,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000706 cifs_sb->mnt_cifs_flags &
Steve French737b7582005-04-28 22:41:06 -0700707 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 else
709 rc = -EOPNOTSUPP;
710
711 if (rc == -EOPNOTSUPP) {
712 int oplock = FALSE;
713 __u16 netfid;
714 /* rc = CIFSSMBSetAttrLegacy(xid, pTcon,
715 full_path,
716 (__u16)ATTR_NORMAL,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000717 cifs_sb->local_nls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 For some strange reason it seems that NT4 eats the
719 old setattr call without actually setting the
720 attributes so on to the third attempted workaround
721 */
722
723 /* BB could scan to see if we already have it open
724 and pass in pid of opener to function */
725 rc = CIFSSMBOpen(xid, pTcon, full_path,
726 FILE_OPEN, SYNCHRONIZE |
727 FILE_WRITE_ATTRIBUTES, 0,
728 &netfid, &oplock, NULL,
Steve French737b7582005-04-28 22:41:06 -0700729 cifs_sb->local_nls,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000730 cifs_sb->mnt_cifs_flags &
Steve French737b7582005-04-28 22:41:06 -0700731 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000732 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 rc = CIFSSMBSetFileTimes(xid, pTcon,
734 pinfo_buf,
735 netfid);
736 CIFSSMBClose(xid, pTcon, netfid);
737 }
738 }
739 kfree(pinfo_buf);
740 }
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000741 if (rc == 0) {
742 rc = CIFSSMBDelFile(xid, pTcon, full_path,
743 cifs_sb->local_nls,
744 cifs_sb->mnt_cifs_flags &
Steve French737b7582005-04-28 22:41:06 -0700745 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (!rc) {
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700747 if (direntry->d_inode)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700748 drop_nlink(direntry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 } else if (rc == -ETXTBSY) {
750 int oplock = FALSE;
751 __u16 netfid;
752
753 rc = CIFSSMBOpen(xid, pTcon, full_path,
754 FILE_OPEN, DELETE,
755 CREATE_NOT_DIR |
756 CREATE_DELETE_ON_CLOSE,
757 &netfid, &oplock, NULL,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000758 cifs_sb->local_nls,
759 cifs_sb->mnt_cifs_flags &
Steve French737b7582005-04-28 22:41:06 -0700760 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000761 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 CIFSSMBRenameOpenFile(xid, pTcon,
763 netfid, NULL,
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 CIFSSMBClose(xid, pTcon, netfid);
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700768 if (direntry->d_inode)
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700769 drop_nlink(direntry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771 /* BB if rc = -ETXTBUSY goto the rename logic BB */
772 }
773 }
774 }
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700775 if (direntry->d_inode) {
Steve Frenchb2aeb9d2005-05-17 13:16:18 -0500776 cifsInode = CIFS_I(direntry->d_inode);
777 cifsInode->time = 0; /* will force revalidate to get info
778 when needed */
779 direntry->d_inode->i_ctime = current_fs_time(inode->i_sb);
780 }
Steve French4523cc32007-04-30 20:13:06 +0000781 if (inode) {
Steve French06bcfed2006-03-31 22:43:50 +0000782 inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb);
783 cifsInode = CIFS_I(inode);
784 cifsInode->time = 0; /* force revalidate of dir as well */
785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 kfree(full_path);
788 FreeXid(xid);
789 return rc;
790}
791
Steve French2dd29d32007-04-23 22:07:35 +0000792static void posix_fill_in_inode(struct inode *tmp_inode,
793 FILE_UNIX_BASIC_INFO *pData, int *pobject_type, int isNewInode)
794{
Christoph Hellwig75f12982008-02-25 20:25:21 +0000795 struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode);
Steve French2dd29d32007-04-23 22:07:35 +0000796 loff_t local_size;
797 struct timespec local_mtime;
798
Steve French2dd29d32007-04-23 22:07:35 +0000799 cifsInfo->time = jiffies;
800 atomic_inc(&cifsInfo->inUse);
801
802 /* save mtime and size */
803 local_mtime = tmp_inode->i_mtime;
804 local_size = tmp_inode->i_size;
805
Christoph Hellwig75f12982008-02-25 20:25:21 +0000806 cifs_unix_info_to_inode(tmp_inode, pData, 1);
807 cifs_set_ops(tmp_inode);
Steve French2dd29d32007-04-23 22:07:35 +0000808
Christoph Hellwig75f12982008-02-25 20:25:21 +0000809 if (!S_ISREG(tmp_inode->i_mode))
810 return;
811
812 /*
813 * No sense invalidating pages for new inode
814 * since we we have not started caching
815 * readahead file data yet.
816 */
817 if (isNewInode)
818 return;
819
820 if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) &&
821 (local_size == tmp_inode->i_size)) {
822 cFYI(1, ("inode exists but unchanged"));
Steve French2dd29d32007-04-23 22:07:35 +0000823 } else {
Christoph Hellwig75f12982008-02-25 20:25:21 +0000824 /* file may have changed on server */
825 cFYI(1, ("invalidate inode, readdir detected change"));
826 invalidate_remote_inode(tmp_inode);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000827 }
Steve French2dd29d32007-04-23 22:07:35 +0000828}
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode)
831{
832 int rc = 0;
833 int xid;
834 struct cifs_sb_info *cifs_sb;
835 struct cifsTconInfo *pTcon;
836 char *full_path = NULL;
837 struct inode *newinode = NULL;
838
Steve French6473a552005-11-29 20:20:10 -0800839 cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p", mode, inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 xid = GetXid();
842
843 cifs_sb = CIFS_SB(inode->i_sb);
844 pTcon = cifs_sb->tcon;
845
Steve French7f573562005-08-30 11:32:14 -0700846 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (full_path == NULL) {
848 FreeXid(xid);
849 return -ENOMEM;
850 }
Steve French50c2f752007-07-13 00:33:32 +0000851
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000852 if ((pTcon->ses->capabilities & CAP_UNIX) &&
853 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
Steve French2dd29d32007-04-23 22:07:35 +0000854 le64_to_cpu(pTcon->fsUnixInfo.Capability))) {
855 u32 oplock = 0;
Steve Frenchf6d09982008-01-08 23:18:22 +0000856 FILE_UNIX_BASIC_INFO *pInfo =
Steve French2dd29d32007-04-23 22:07:35 +0000857 kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000858 if (pInfo == NULL) {
Steve French2dd29d32007-04-23 22:07:35 +0000859 rc = -ENOMEM;
860 goto mkdir_out;
861 }
Steve French50c2f752007-07-13 00:33:32 +0000862
Jeffa8cd9252007-09-13 18:38:50 +0000863 mode &= ~current->fs->umask;
Steve French2dd29d32007-04-23 22:07:35 +0000864 rc = CIFSPOSIXCreate(xid, pTcon, SMB_O_DIRECTORY | SMB_O_CREAT,
865 mode, NULL /* netfid */, pInfo, &oplock,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000866 full_path, cifs_sb->local_nls,
867 cifs_sb->mnt_cifs_flags &
Steve French2dd29d32007-04-23 22:07:35 +0000868 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve Frenchc45d7072007-09-17 02:04:21 +0000869 if (rc == -EOPNOTSUPP) {
870 kfree(pInfo);
871 goto mkdir_retry_old;
872 } else if (rc) {
Steve French2dd29d32007-04-23 22:07:35 +0000873 cFYI(1, ("posix mkdir returned 0x%x", rc));
874 d_drop(direntry);
875 } else {
Steve Frenchcbac3cb2007-04-25 11:46:06 +0000876 int obj_type;
Cyril Gorcunov8f2376a2007-10-14 17:58:43 +0000877 if (pInfo->Type == cpu_to_le32(-1)) {
878 /* no return info, go query for it */
Steve French5a07cdf2007-09-16 23:12:47 +0000879 kfree(pInfo);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000880 goto mkdir_get_info;
Steve French5a07cdf2007-09-16 23:12:47 +0000881 }
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000882/*BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if need
883 to set uid/gid */
Steve French2dd29d32007-04-23 22:07:35 +0000884 inc_nlink(inode);
885 if (pTcon->nocase)
886 direntry->d_op = &cifs_ci_dentry_ops;
887 else
888 direntry->d_op = &cifs_dentry_ops;
Steve Frenchcbac3cb2007-04-25 11:46:06 +0000889
890 newinode = new_inode(inode->i_sb);
Steve French5a07cdf2007-09-16 23:12:47 +0000891 if (newinode == NULL) {
892 kfree(pInfo);
Steve Frenchcbac3cb2007-04-25 11:46:06 +0000893 goto mkdir_get_info;
Steve French5a07cdf2007-09-16 23:12:47 +0000894 }
Steve Frenchcbac3cb2007-04-25 11:46:06 +0000895 /* Is an i_ino of zero legal? */
896 /* Are there sanity checks we can use to ensure that
897 the server is really filling in that field? */
898 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
899 newinode->i_ino =
900 (unsigned long)pInfo->UniqueId;
901 } /* note ino incremented to unique num in new_inode */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000902 if (inode->i_sb->s_flags & MS_NOATIME)
Steve Frenchcbac3cb2007-04-25 11:46:06 +0000903 newinode->i_flags |= S_NOATIME | S_NOCMTIME;
904 newinode->i_nlink = 2;
905
906 insert_inode_hash(newinode);
Steve French2dd29d32007-04-23 22:07:35 +0000907 d_instantiate(direntry, newinode);
Steve Frenchcbac3cb2007-04-25 11:46:06 +0000908
909 /* we already checked in POSIXCreate whether
910 frame was long enough */
911 posix_fill_in_inode(direntry->d_inode,
Steve French2dd29d32007-04-23 22:07:35 +0000912 pInfo, &obj_type, 1 /* NewInode */);
Steve Frenchcbac3cb2007-04-25 11:46:06 +0000913#ifdef CONFIG_CIFS_DEBUG2
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000914 cFYI(1, ("instantiated dentry %p %s to inode %p",
Steve Frenchcbac3cb2007-04-25 11:46:06 +0000915 direntry, direntry->d_name.name, newinode));
916
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000917 if (newinode->i_nlink != 2)
918 cFYI(1, ("unexpected number of links %d",
Steve Frenchcbac3cb2007-04-25 11:46:06 +0000919 newinode->i_nlink));
920#endif
Steve French2dd29d32007-04-23 22:07:35 +0000921 }
922 kfree(pInfo);
923 goto mkdir_out;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000924 }
Steve Frenchc45d7072007-09-17 02:04:21 +0000925mkdir_retry_old:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 /* BB add setting the equivalent of mode via CreateX w/ACLs */
Steve French737b7582005-04-28 22:41:06 -0700927 rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls,
928 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (rc) {
Steve French26a21b92006-05-31 18:05:34 +0000930 cFYI(1, ("cifs_mkdir returned 0x%x", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 d_drop(direntry);
932 } else {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000933mkdir_get_info:
Dave Hansend8c76e62006-09-30 23:29:04 -0700934 inc_nlink(inode);
Steve Frenchc18c8422007-07-18 23:21:09 +0000935 if (pTcon->unix_ext)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 rc = cifs_get_inode_info_unix(&newinode, full_path,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000937 inode->i_sb, xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 else
939 rc = cifs_get_inode_info(&newinode, full_path, NULL,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000940 inode->i_sb, xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Steve Frenchb92327f2005-08-22 20:09:43 -0700942 if (pTcon->nocase)
943 direntry->d_op = &cifs_ci_dentry_ops;
944 else
945 direntry->d_op = &cifs_dentry_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 d_instantiate(direntry, newinode);
Steve French2dd29d32007-04-23 22:07:35 +0000947 /* setting nlink not necessary except in cases where we
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000948 * failed to get it from the server or was set bogus */
Steve French2dd29d32007-04-23 22:07:35 +0000949 if ((direntry->d_inode) && (direntry->d_inode->i_nlink < 2))
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000950 direntry->d_inode->i_nlink = 2;
Steve Frenchc18c8422007-07-18 23:21:09 +0000951 if (pTcon->unix_ext) {
Steve French3ce53fc2007-06-08 14:55:14 +0000952 mode &= ~current->fs->umask;
Steve Frenchd0d2f2d2005-06-02 15:12:36 -0700953 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
955 mode,
Steve French83451872005-12-01 17:12:59 -0800956 (__u64)current->fsuid,
957 (__u64)current->fsgid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 0 /* dev_t */,
Steve French737b7582005-04-28 22:41:06 -0700959 cifs_sb->local_nls,
960 cifs_sb->mnt_cifs_flags &
961 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 } else {
963 CIFSSMBUnixSetPerms(xid, pTcon, full_path,
964 mode, (__u64)-1,
965 (__u64)-1, 0 /* dev_t */,
Steve French737b7582005-04-28 22:41:06 -0700966 cifs_sb->local_nls,
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000967 cifs_sb->mnt_cifs_flags &
Steve French737b7582005-04-28 22:41:06 -0700968 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
Steve French3ce53fc2007-06-08 14:55:14 +0000970 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 /* BB to be implemented via Windows secrty descriptors
972 eg CIFSSMBWinSetPerms(xid, pTcon, full_path, mode,
973 -1, -1, local_nls); */
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000974 if (direntry->d_inode) {
Steve French6473a552005-11-29 20:20:10 -0800975 direntry->d_inode->i_mode = mode;
Steve French25741b32005-11-29 22:38:43 -0800976 direntry->d_inode->i_mode |= S_IFDIR;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000977 if (cifs_sb->mnt_cifs_flags &
Steve French6473a552005-11-29 20:20:10 -0800978 CIFS_MOUNT_SET_UID) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000979 direntry->d_inode->i_uid =
Steve French6473a552005-11-29 20:20:10 -0800980 current->fsuid;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000981 direntry->d_inode->i_gid =
Steve French6473a552005-11-29 20:20:10 -0800982 current->fsgid;
983 }
984 }
Steve French2a138ebb2005-11-29 21:22:19 -0800985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000987mkdir_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 kfree(full_path);
989 FreeXid(xid);
990 return rc;
991}
992
993int cifs_rmdir(struct inode *inode, struct dentry *direntry)
994{
995 int rc = 0;
996 int xid;
997 struct cifs_sb_info *cifs_sb;
998 struct cifsTconInfo *pTcon;
999 char *full_path = NULL;
1000 struct cifsInodeInfo *cifsInode;
1001
Steve French26a21b92006-05-31 18:05:34 +00001002 cFYI(1, ("cifs_rmdir, inode = 0x%p", inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 xid = GetXid();
1005
1006 cifs_sb = CIFS_SB(inode->i_sb);
1007 pTcon = cifs_sb->tcon;
1008
Steve French7f573562005-08-30 11:32:14 -07001009 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (full_path == NULL) {
1011 FreeXid(xid);
1012 return -ENOMEM;
1013 }
1014
Steve French737b7582005-04-28 22:41:06 -07001015 rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls,
1016 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 if (!rc) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07001019 drop_nlink(inode);
Steve French3677db12007-02-26 16:46:11 +00001020 spin_lock(&direntry->d_inode->i_lock);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001021 i_size_write(direntry->d_inode, 0);
Dave Hansence71ec32006-09-30 23:29:06 -07001022 clear_nlink(direntry->d_inode);
Steve French3677db12007-02-26 16:46:11 +00001023 spin_unlock(&direntry->d_inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025
1026 cifsInode = CIFS_I(direntry->d_inode);
1027 cifsInode->time = 0; /* force revalidate to go get info when
1028 needed */
1029 direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
1030 current_fs_time(inode->i_sb);
1031
1032 kfree(full_path);
1033 FreeXid(xid);
1034 return rc;
1035}
1036
1037int cifs_rename(struct inode *source_inode, struct dentry *source_direntry,
1038 struct inode *target_inode, struct dentry *target_direntry)
1039{
1040 char *fromName;
1041 char *toName;
1042 struct cifs_sb_info *cifs_sb_source;
1043 struct cifs_sb_info *cifs_sb_target;
1044 struct cifsTconInfo *pTcon;
1045 int xid;
1046 int rc = 0;
1047
1048 xid = GetXid();
1049
1050 cifs_sb_target = CIFS_SB(target_inode->i_sb);
1051 cifs_sb_source = CIFS_SB(source_inode->i_sb);
1052 pTcon = cifs_sb_source->tcon;
1053
1054 if (pTcon != cifs_sb_target->tcon) {
1055 FreeXid(xid);
1056 return -EXDEV; /* BB actually could be allowed if same server,
1057 but different share.
1058 Might eventually add support for this */
1059 }
1060
1061 /* we already have the rename sem so we do not need to grab it again
1062 here to protect the path integrity */
Steve French7f573562005-08-30 11:32:14 -07001063 fromName = build_path_from_dentry(source_direntry);
1064 toName = build_path_from_dentry(target_direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if ((fromName == NULL) || (toName == NULL)) {
1066 rc = -ENOMEM;
1067 goto cifs_rename_exit;
1068 }
1069
1070 rc = CIFSSMBRename(xid, pTcon, fromName, toName,
Steve French737b7582005-04-28 22:41:06 -07001071 cifs_sb_source->local_nls,
1072 cifs_sb_source->mnt_cifs_flags &
1073 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (rc == -EEXIST) {
1075 /* check if they are the same file because rename of hardlinked
1076 files is a noop */
1077 FILE_UNIX_BASIC_INFO *info_buf_source;
1078 FILE_UNIX_BASIC_INFO *info_buf_target;
1079
1080 info_buf_source =
1081 kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
1082 if (info_buf_source != NULL) {
1083 info_buf_target = info_buf_source + 1;
Steve Frenchc18c8422007-07-18 23:21:09 +00001084 if (pTcon->unix_ext)
Steve French8e87d4d2006-11-02 03:45:24 +00001085 rc = CIFSSMBUnixQPathInfo(xid, pTcon, fromName,
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001086 info_buf_source,
Steve French8e87d4d2006-11-02 03:45:24 +00001087 cifs_sb_source->local_nls,
1088 cifs_sb_source->mnt_cifs_flags &
1089 CIFS_MOUNT_MAP_SPECIAL_CHR);
1090 /* else rc is still EEXIST so will fall through to
1091 unlink the target and retry rename */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (rc == 0) {
1093 rc = CIFSSMBUnixQPathInfo(xid, pTcon, toName,
1094 info_buf_target,
Steve French737b7582005-04-28 22:41:06 -07001095 cifs_sb_target->local_nls,
1096 /* remap based on source sb */
1097 cifs_sb_source->mnt_cifs_flags &
1098 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100 if ((rc == 0) &&
1101 (info_buf_source->UniqueId ==
1102 info_buf_target->UniqueId)) {
1103 /* do not rename since the files are hardlinked which
1104 is a noop */
1105 } else {
1106 /* we either can not tell the files are hardlinked
1107 (as with Windows servers) or files are not
1108 hardlinked so delete the target manually before
1109 renaming to follow POSIX rather than Windows
1110 semantics */
1111 cifs_unlink(target_inode, target_direntry);
1112 rc = CIFSSMBRename(xid, pTcon, fromName,
1113 toName,
Steve French737b7582005-04-28 22:41:06 -07001114 cifs_sb_source->local_nls,
1115 cifs_sb_source->mnt_cifs_flags
1116 & CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118 kfree(info_buf_source);
1119 } /* if we can not get memory just leave rc as EEXIST */
1120 }
1121
Steve Frenchad7a2922008-02-07 23:25:02 +00001122 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 cFYI(1, ("rename rc %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 if ((rc == -EIO) || (rc == -EEXIST)) {
1126 int oplock = FALSE;
1127 __u16 netfid;
1128
1129 /* BB FIXME Is Generic Read correct for rename? */
1130 /* if renaming directory - we should not say CREATE_NOT_DIR,
1131 need to test renaming open directory, also GENERIC_READ
1132 might not right be right access to request */
1133 rc = CIFSSMBOpen(xid, pTcon, fromName, FILE_OPEN, GENERIC_READ,
1134 CREATE_NOT_DIR, &netfid, &oplock, NULL,
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001135 cifs_sb_source->local_nls,
1136 cifs_sb_source->mnt_cifs_flags &
Steve French737b7582005-04-28 22:41:06 -07001137 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001138 if (rc == 0) {
Steve French8e87d4d2006-11-02 03:45:24 +00001139 rc = CIFSSMBRenameOpenFile(xid, pTcon, netfid, toName,
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001140 cifs_sb_source->local_nls,
Steve French737b7582005-04-28 22:41:06 -07001141 cifs_sb_source->mnt_cifs_flags &
1142 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 CIFSSMBClose(xid, pTcon, netfid);
1144 }
1145 }
1146
1147cifs_rename_exit:
1148 kfree(fromName);
1149 kfree(toName);
1150 FreeXid(xid);
1151 return rc;
1152}
1153
1154int cifs_revalidate(struct dentry *direntry)
1155{
1156 int xid;
Jeff Laytoncea21802007-11-20 23:19:03 +00001157 int rc = 0, wbrc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 char *full_path;
1159 struct cifs_sb_info *cifs_sb;
1160 struct cifsInodeInfo *cifsInode;
1161 loff_t local_size;
1162 struct timespec local_mtime;
1163 int invalidate_inode = FALSE;
1164
1165 if (direntry->d_inode == NULL)
1166 return -ENOENT;
1167
1168 cifsInode = CIFS_I(direntry->d_inode);
1169
1170 if (cifsInode == NULL)
1171 return -ENOENT;
1172
1173 /* no sense revalidating inode info on file that no one can write */
1174 if (CIFS_I(direntry->d_inode)->clientCanCacheRead)
1175 return rc;
1176
1177 xid = GetXid();
1178
1179 cifs_sb = CIFS_SB(direntry->d_sb);
1180
1181 /* can not safely grab the rename sem here if rename calls revalidate
1182 since that would deadlock */
Steve French7f573562005-08-30 11:32:14 -07001183 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (full_path == NULL) {
1185 FreeXid(xid);
1186 return -ENOMEM;
1187 }
1188 cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld "
1189 "jiffies %ld", full_path, direntry->d_inode,
1190 direntry->d_inode->i_count.counter, direntry,
1191 direntry->d_time, jiffies));
1192
1193 if (cifsInode->time == 0) {
1194 /* was set to zero previously to force revalidate */
1195 } else if (time_before(jiffies, cifsInode->time + HZ) &&
1196 lookupCacheEnabled) {
1197 if ((S_ISREG(direntry->d_inode->i_mode) == 0) ||
1198 (direntry->d_inode->i_nlink == 1)) {
1199 kfree(full_path);
1200 FreeXid(xid);
1201 return rc;
1202 } else {
1203 cFYI(1, ("Have to revalidate file due to hardlinks"));
1204 }
1205 }
1206
1207 /* save mtime and size */
1208 local_mtime = direntry->d_inode->i_mtime;
1209 local_size = direntry->d_inode->i_size;
1210
Steve Frenchc18c8422007-07-18 23:21:09 +00001211 if (cifs_sb->tcon->unix_ext) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path,
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001213 direntry->d_sb, xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (rc) {
1215 cFYI(1, ("error on getting revalidate info %d", rc));
1216/* if (rc != -ENOENT)
1217 rc = 0; */ /* BB should we cache info on
1218 certain errors? */
1219 }
1220 } else {
1221 rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL,
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001222 direntry->d_sb, xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (rc) {
1224 cFYI(1, ("error on getting revalidate info %d", rc));
1225/* if (rc != -ENOENT)
1226 rc = 0; */ /* BB should we cache info on
1227 certain errors? */
1228 }
1229 }
1230 /* should we remap certain errors, access denied?, to zero */
1231
1232 /* if not oplocked, we invalidate inode pages if mtime or file size
1233 had changed on server */
1234
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001235 if (timespec_equal(&local_mtime, &direntry->d_inode->i_mtime) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 (local_size == direntry->d_inode->i_size)) {
1237 cFYI(1, ("cifs_revalidate - inode unchanged"));
1238 } else {
1239 /* file may have changed on server */
1240 if (cifsInode->clientCanCacheRead) {
1241 /* no need to invalidate inode pages since we were the
1242 only ones who could have modified the file and the
1243 server copy is staler than ours */
1244 } else {
1245 invalidate_inode = TRUE;
1246 }
1247 }
1248
1249 /* can not grab this sem since kernel filesys locking documentation
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001250 indicates i_mutex may be taken by the kernel on lookup and rename
1251 which could deadlock if we grab the i_mutex here as well */
1252/* mutex_lock(&direntry->d_inode->i_mutex);*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 /* need to write out dirty pages here */
1254 if (direntry->d_inode->i_mapping) {
1255 /* do we need to lock inode until after invalidate completes
1256 below? */
Jeff Laytoncea21802007-11-20 23:19:03 +00001257 wbrc = filemap_fdatawrite(direntry->d_inode->i_mapping);
1258 if (wbrc)
1259 CIFS_I(direntry->d_inode)->write_behind_rc = wbrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
1261 if (invalidate_inode) {
Steve French3abb9272005-11-28 08:16:13 -08001262 /* shrink_dcache not necessary now that cifs dentry ops
1263 are exported for negative dentries */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001264/* if (S_ISDIR(direntry->d_inode->i_mode))
Steve French3abb9272005-11-28 08:16:13 -08001265 shrink_dcache_parent(direntry); */
1266 if (S_ISREG(direntry->d_inode->i_mode)) {
1267 if (direntry->d_inode->i_mapping)
Jeff Laytoncea21802007-11-20 23:19:03 +00001268 wbrc = filemap_fdatawait(direntry->d_inode->i_mapping);
1269 if (wbrc)
1270 CIFS_I(direntry->d_inode)->write_behind_rc = wbrc;
Steve French3abb9272005-11-28 08:16:13 -08001271 /* may eventually have to do this for open files too */
1272 if (list_empty(&(cifsInode->openFileList))) {
1273 /* changed on server - flush read ahead pages */
1274 cFYI(1, ("Invalidating read ahead data on "
1275 "closed file"));
1276 invalidate_remote_inode(direntry->d_inode);
1277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
1279 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001280/* mutex_unlock(&direntry->d_inode->i_mutex); */
Steve French50c2f752007-07-13 00:33:32 +00001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 kfree(full_path);
1283 FreeXid(xid);
1284 return rc;
1285}
1286
1287int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1288 struct kstat *stat)
1289{
1290 int err = cifs_revalidate(dentry);
Steve French5fe14c82006-11-07 19:26:33 +00001291 if (!err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 generic_fillattr(dentry->d_inode, stat);
Steve French5fe14c82006-11-07 19:26:33 +00001293 stat->blksize = CIFS_MAX_MSGSIZE;
1294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return err;
1296}
1297
1298static int cifs_truncate_page(struct address_space *mapping, loff_t from)
1299{
1300 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1301 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
1302 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 int rc = 0;
1304
1305 page = grab_cache_page(mapping, index);
1306 if (!page)
1307 return -ENOMEM;
1308
Christoph Lametereebd2aa2008-02-04 22:28:29 -08001309 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 unlock_page(page);
1311 page_cache_release(page);
1312 return rc;
1313}
1314
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001315static int cifs_vmtruncate(struct inode *inode, loff_t offset)
Steve French3677db12007-02-26 16:46:11 +00001316{
1317 struct address_space *mapping = inode->i_mapping;
1318 unsigned long limit;
1319
Steve Frenchba6a46a2007-02-26 20:06:29 +00001320 spin_lock(&inode->i_lock);
Steve French3677db12007-02-26 16:46:11 +00001321 if (inode->i_size < offset)
1322 goto do_expand;
1323 /*
1324 * truncation of in-use swapfiles is disallowed - it would cause
1325 * subsequent swapout to scribble on the now-freed blocks.
1326 */
Steve Frenchba6a46a2007-02-26 20:06:29 +00001327 if (IS_SWAPFILE(inode)) {
1328 spin_unlock(&inode->i_lock);
Steve French3677db12007-02-26 16:46:11 +00001329 goto out_busy;
Steve Frenchba6a46a2007-02-26 20:06:29 +00001330 }
Steve French3677db12007-02-26 16:46:11 +00001331 i_size_write(inode, offset);
1332 spin_unlock(&inode->i_lock);
Steve French8064ab42007-08-22 22:12:07 +00001333 /*
1334 * unmap_mapping_range is called twice, first simply for efficiency
1335 * so that truncate_inode_pages does fewer single-page unmaps. However
1336 * after this first call, and before truncate_inode_pages finishes,
1337 * it is possible for private pages to be COWed, which remain after
1338 * truncate_inode_pages finishes, hence the second unmap_mapping_range
1339 * call must be made for correctness.
1340 */
Steve French3677db12007-02-26 16:46:11 +00001341 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
1342 truncate_inode_pages(mapping, offset);
Steve French8064ab42007-08-22 22:12:07 +00001343 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
Steve French3677db12007-02-26 16:46:11 +00001344 goto out_truncate;
1345
1346do_expand:
1347 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
Steve Frenchba6a46a2007-02-26 20:06:29 +00001348 if (limit != RLIM_INFINITY && offset > limit) {
1349 spin_unlock(&inode->i_lock);
Steve French3677db12007-02-26 16:46:11 +00001350 goto out_sig;
Steve Frenchba6a46a2007-02-26 20:06:29 +00001351 }
1352 if (offset > inode->i_sb->s_maxbytes) {
1353 spin_unlock(&inode->i_lock);
Steve French3677db12007-02-26 16:46:11 +00001354 goto out_big;
Steve Frenchba6a46a2007-02-26 20:06:29 +00001355 }
Steve French3677db12007-02-26 16:46:11 +00001356 i_size_write(inode, offset);
Steve Frenchba6a46a2007-02-26 20:06:29 +00001357 spin_unlock(&inode->i_lock);
Steve French3677db12007-02-26 16:46:11 +00001358out_truncate:
1359 if (inode->i_op && inode->i_op->truncate)
1360 inode->i_op->truncate(inode);
1361 return 0;
1362out_sig:
1363 send_sig(SIGXFSZ, current, 0);
1364out_big:
1365 return -EFBIG;
1366out_busy:
1367 return -ETXTBSY;
1368}
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370int cifs_setattr(struct dentry *direntry, struct iattr *attrs)
1371{
1372 int xid;
1373 struct cifs_sb_info *cifs_sb;
1374 struct cifsTconInfo *pTcon;
1375 char *full_path = NULL;
1376 int rc = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 struct cifsFileInfo *open_file = NULL;
1378 FILE_BASIC_INFO time_buf;
1379 int set_time = FALSE;
Steve French066fcb02007-03-23 00:45:08 +00001380 int set_dosattr = FALSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 __u64 mode = 0xFFFFFFFFFFFFFFFFULL;
1382 __u64 uid = 0xFFFFFFFFFFFFFFFFULL;
1383 __u64 gid = 0xFFFFFFFFFFFFFFFFULL;
1384 struct cifsInodeInfo *cifsInode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 xid = GetXid();
1387
Steve French39798772006-05-31 22:40:51 +00001388 cFYI(1, ("setattr on file %s attrs->iavalid 0x%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 direntry->d_name.name, attrs->ia_valid));
Steve French6473a552005-11-29 20:20:10 -08001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 cifs_sb = CIFS_SB(direntry->d_inode->i_sb);
1392 pTcon = cifs_sb->tcon;
1393
Steve French2a138ebb2005-11-29 21:22:19 -08001394 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) {
Steve French6473a552005-11-29 20:20:10 -08001395 /* check if we have permission to change attrs */
1396 rc = inode_change_ok(direntry->d_inode, attrs);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001397 if (rc < 0) {
Steve French6473a552005-11-29 20:20:10 -08001398 FreeXid(xid);
1399 return rc;
1400 } else
1401 rc = 0;
1402 }
Steve French50c2f752007-07-13 00:33:32 +00001403
Steve French7f573562005-08-30 11:32:14 -07001404 full_path = build_path_from_dentry(direntry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 if (full_path == NULL) {
1406 FreeXid(xid);
1407 return -ENOMEM;
1408 }
1409 cifsInode = CIFS_I(direntry->d_inode);
1410
1411 /* BB check if we need to refresh inode from server now ? BB */
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 if (attrs->ia_valid & ATTR_SIZE) {
Jeff Laytoncea21802007-11-20 23:19:03 +00001414 /*
1415 Flush data before changing file size on server. If the
1416 flush returns error, store it to report later and continue.
1417 BB: This should be smarter. Why bother flushing pages that
1418 will be truncated anyway? Also, should we error out here if
1419 the flush returns error?
1420 */
1421 rc = filemap_write_and_wait(direntry->d_inode->i_mapping);
1422 if (rc != 0) {
1423 CIFS_I(direntry->d_inode)->write_behind_rc = rc;
1424 rc = 0;
1425 }
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 /* To avoid spurious oplock breaks from server, in the case of
1428 inodes that we already have open, avoid doing path based
1429 setting of file size if we can do it by handle.
1430 This keeps our caching token (oplock) and avoids timeouts
1431 when the local oplock break takes longer to flush
1432 writebehind data than the SMB timeout for the SetPathInfo
1433 request would allow */
Steve French39798772006-05-31 22:40:51 +00001434
Steve French6148a742005-10-05 12:23:19 -07001435 open_file = find_writable_file(cifsInode);
1436 if (open_file) {
1437 __u16 nfid = open_file->netfid;
1438 __u32 npid = open_file->pid;
1439 rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size,
1440 nfid, npid, FALSE);
Steve French23e7dd72005-10-20 13:44:56 -07001441 atomic_dec(&open_file->wrtPending);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001442 cFYI(1, ("SetFSize for attrs rc = %d", rc));
1443 if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
Steve French77159b42007-08-31 01:10:17 +00001444 unsigned int bytes_written;
Steve French6148a742005-10-05 12:23:19 -07001445 rc = CIFSSMBWrite(xid, pTcon,
1446 nfid, 0, attrs->ia_size,
1447 &bytes_written, NULL, NULL,
1448 1 /* 45 seconds */);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001449 cFYI(1, ("Wrt seteof rc %d", rc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001451 } else
Steve French6473a552005-11-29 20:20:10 -08001452 rc = -EINVAL;
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (rc != 0) {
1455 /* Set file size by pathname rather than by handle
1456 either because no valid, writeable file handle for
1457 it was found or because there was an error setting
1458 it by handle */
1459 rc = CIFSSMBSetEOF(xid, pTcon, full_path,
1460 attrs->ia_size, FALSE,
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001461 cifs_sb->local_nls,
Steve French737b7582005-04-28 22:41:06 -07001462 cifs_sb->mnt_cifs_flags &
1463 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve Frenche30dcf32005-09-20 20:49:16 -07001464 cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc));
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001465 if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
Steve Frenche30dcf32005-09-20 20:49:16 -07001466 __u16 netfid;
1467 int oplock = FALSE;
1468
1469 rc = SMBLegacyOpen(xid, pTcon, full_path,
1470 FILE_OPEN,
1471 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1472 CREATE_NOT_DIR, &netfid, &oplock,
1473 NULL, cifs_sb->local_nls,
1474 cifs_sb->mnt_cifs_flags &
1475 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001476 if (rc == 0) {
Steve French77159b42007-08-31 01:10:17 +00001477 unsigned int bytes_written;
Steve Frenche30dcf32005-09-20 20:49:16 -07001478 rc = CIFSSMBWrite(xid, pTcon,
1479 netfid, 0,
1480 attrs->ia_size,
1481 &bytes_written, NULL,
1482 NULL, 1 /* 45 sec */);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001483 cFYI(1, ("wrt seteof rc %d", rc));
Steve Frenche30dcf32005-09-20 20:49:16 -07001484 CIFSSMBClose(xid, pTcon, netfid);
1485 }
1486
1487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 }
1489
1490 /* Server is ok setting allocation size implicitly - no need
1491 to call:
1492 CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, TRUE,
1493 cifs_sb->local_nls);
1494 */
1495
1496 if (rc == 0) {
Steve French3677db12007-02-26 16:46:11 +00001497 rc = cifs_vmtruncate(direntry->d_inode, attrs->ia_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 cifs_truncate_page(direntry->d_inode->i_mapping,
1499 direntry->d_inode->i_size);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001500 } else
Steve Frenche30dcf32005-09-20 20:49:16 -07001501 goto cifs_setattr_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
1503 if (attrs->ia_valid & ATTR_UID) {
Steve Frenche30dcf32005-09-20 20:49:16 -07001504 cFYI(1, ("UID changed to %d", attrs->ia_uid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 uid = attrs->ia_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
1507 if (attrs->ia_valid & ATTR_GID) {
Steve Frenche30dcf32005-09-20 20:49:16 -07001508 cFYI(1, ("GID changed to %d", attrs->ia_gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 gid = attrs->ia_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
1511
1512 time_buf.Attributes = 0;
Jeff Laytond32c4f22007-10-18 03:05:22 -07001513
1514 /* skip mode change if it's just for clearing setuid/setgid */
1515 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
1516 attrs->ia_valid &= ~ATTR_MODE;
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 if (attrs->ia_valid & ATTR_MODE) {
Steve Frenche30dcf32005-09-20 20:49:16 -07001519 cFYI(1, ("Mode changed to 0x%x", attrs->ia_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 mode = attrs->ia_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
1522
Steve Frenchc18c8422007-07-18 23:21:09 +00001523 if ((pTcon->unix_ext)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 && (attrs->ia_valid & (ATTR_MODE | ATTR_GID | ATTR_UID)))
1525 rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode, uid, gid,
Steve French737b7582005-04-28 22:41:06 -07001526 0 /* dev_t */, cifs_sb->local_nls,
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001527 cifs_sb->mnt_cifs_flags &
Steve French737b7582005-04-28 22:41:06 -07001528 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 else if (attrs->ia_valid & ATTR_MODE) {
Steve Frenchcdbce9c2005-11-19 21:04:52 -08001530 rc = 0;
Steve French97837582007-12-31 07:47:21 +00001531#ifdef CONFIG_CIFS_EXPERIMENTAL
1532 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
1533 rc = mode_to_acl(direntry->d_inode, full_path, mode);
Steve Frenchf6d09982008-01-08 23:18:22 +00001534 else if ((mode & S_IWUGO) == 0) {
Steve French97837582007-12-31 07:47:21 +00001535#else
Steve Frenchf6d09982008-01-08 23:18:22 +00001536 if ((mode & S_IWUGO) == 0) {
Steve French97837582007-12-31 07:47:21 +00001537#endif
Steve Frenchf6d09982008-01-08 23:18:22 +00001538 /* not writeable */
Steve French066fcb02007-03-23 00:45:08 +00001539 if ((cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
1540 set_dosattr = TRUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 time_buf.Attributes =
1542 cpu_to_le32(cifsInode->cifsAttrs |
1543 ATTR_READONLY);
Steve French066fcb02007-03-23 00:45:08 +00001544 }
Steve French5268df22007-04-06 19:28:16 +00001545 } else if (cifsInode->cifsAttrs & ATTR_READONLY) {
1546 /* If file is readonly on server, we would
1547 not be able to write to it - so if any write
1548 bit is enabled for user or group or other we
1549 need to at least try to remove r/o dos attr */
1550 set_dosattr = TRUE;
1551 time_buf.Attributes = cpu_to_le32(cifsInode->cifsAttrs &
1552 (~ATTR_READONLY));
1553 /* Windows ignores set to zero */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001554 if (time_buf.Attributes == 0)
Steve French5268df22007-04-06 19:28:16 +00001555 time_buf.Attributes |= cpu_to_le32(ATTR_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
Steve French97837582007-12-31 07:47:21 +00001557#ifdef CONFIG_CIFS_EXPERIMENTAL
1558 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
1559 mode_to_acl(direntry->d_inode, full_path, mode);
1560#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 }
1562
1563 if (attrs->ia_valid & ATTR_ATIME) {
1564 set_time = TRUE;
1565 time_buf.LastAccessTime =
1566 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
1567 } else
1568 time_buf.LastAccessTime = 0;
1569
1570 if (attrs->ia_valid & ATTR_MTIME) {
1571 set_time = TRUE;
1572 time_buf.LastWriteTime =
1573 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
1574 } else
1575 time_buf.LastWriteTime = 0;
Steve Frenche30dcf32005-09-20 20:49:16 -07001576 /* Do not set ctime explicitly unless other time
1577 stamps are changed explicitly (i.e. by utime()
1578 since we would then have a mix of client and
1579 server times */
Steve French50c2f752007-07-13 00:33:32 +00001580
Steve Frenche30dcf32005-09-20 20:49:16 -07001581 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 set_time = TRUE;
Steve Frenche30dcf32005-09-20 20:49:16 -07001583 /* Although Samba throws this field away
1584 it may be useful to Windows - but we do
1585 not want to set ctime unless some other
1586 timestamp is changing */
Steve French26a21b92006-05-31 18:05:34 +00001587 cFYI(1, ("CIFS - CTIME changed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 time_buf.ChangeTime =
1589 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
1590 } else
1591 time_buf.ChangeTime = 0;
1592
Steve French066fcb02007-03-23 00:45:08 +00001593 if (set_time || set_dosattr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 time_buf.CreationTime = 0; /* do not change */
1595 /* In the future we should experiment - try setting timestamps
1596 via Handle (SetFileInfo) instead of by path */
1597 if (!(pTcon->ses->flags & CIFS_SES_NT4))
1598 rc = CIFSSMBSetTimes(xid, pTcon, full_path, &time_buf,
Steve French737b7582005-04-28 22:41:06 -07001599 cifs_sb->local_nls,
1600 cifs_sb->mnt_cifs_flags &
1601 CIFS_MOUNT_MAP_SPECIAL_CHR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 else
1603 rc = -EOPNOTSUPP;
1604
1605 if (rc == -EOPNOTSUPP) {
1606 int oplock = FALSE;
1607 __u16 netfid;
1608
1609 cFYI(1, ("calling SetFileInfo since SetPathInfo for "
1610 "times not supported by this server"));
1611 /* BB we could scan to see if we already have it open
1612 and pass in pid of opener to function */
1613 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN,
1614 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
1615 CREATE_NOT_DIR, &netfid, &oplock,
Steve French737b7582005-04-28 22:41:06 -07001616 NULL, cifs_sb->local_nls,
1617 cifs_sb->mnt_cifs_flags &
1618 CIFS_MOUNT_MAP_SPECIAL_CHR);
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001619 if (rc == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 rc = CIFSSMBSetFileTimes(xid, pTcon, &time_buf,
1621 netfid);
1622 CIFSSMBClose(xid, pTcon, netfid);
1623 } else {
1624 /* BB For even older servers we could convert time_buf
1625 into old DOS style which uses two second
1626 granularity */
1627
1628 /* rc = CIFSSMBSetTimesLegacy(xid, pTcon, full_path,
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001629 &time_buf, cifs_sb->local_nls); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
1631 }
Steve Frenche30dcf32005-09-20 20:49:16 -07001632 /* Even if error on time set, no sense failing the call if
1633 the server would set the time to a reasonable value anyway,
1634 and this check ensures that we are not being called from
1635 sys_utimes in which case we ought to fail the call back to
1636 the user when the server rejects the call */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001637 if ((rc) && (attrs->ia_valid &
Steve Frenche30dcf32005-09-20 20:49:16 -07001638 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
1639 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 }
1641
1642 /* do not need local check to inode_check_ok since the server does
1643 that */
1644 if (!rc)
1645 rc = inode_setattr(direntry->d_inode, attrs);
Steve Frenche30dcf32005-09-20 20:49:16 -07001646cifs_setattr_exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 kfree(full_path);
1648 FreeXid(xid);
1649 return rc;
1650}
1651
Steve French99ee4db2007-02-27 05:35:17 +00001652#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653void cifs_delete_inode(struct inode *inode)
1654{
Steve French26a21b92006-05-31 18:05:34 +00001655 cFYI(1, ("In cifs_delete_inode, inode = 0x%p", inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 /* may have to add back in if and when safe distributed caching of
1657 directories added e.g. via FindNotify */
1658}
Steve French99ee4db2007-02-27 05:35:17 +00001659#endif