Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | ||
33 | int 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; | |
44 | cFYI(1, (" Getting info on %s ", search_path)); | |
45 | /* could have done a find first instead but this returns more info */ | |
46 | rc = CIFSSMBUnixQPathInfo(xid, pTcon, search_path, &findData, | |
737b758c SF |
47 | cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & |
48 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
49 | /* 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, | |
737b758c SF |
67 | cifs_sb->local_nls, |
68 | cifs_sb->mnt_cifs_flags & | |
69 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
70 | 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); | |
d0d2f2df | 85 | if (*pinode == NULL) |
1da177e4 LT |
86 | 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? */ | |
d0d2f2df | 90 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) { |
1da177e4 LT |
91 | (*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 | ||
100 | cFYI(1, (" Old time %ld ", cifsInfo->time)); | |
101 | cifsInfo->time = jiffies; | |
102 | cFYI(1, (" New time %ld ", cifsInfo->time)); | |
103 | /* 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); | |
114 | if (type == UNIX_FILE) { | |
115 | inode->i_mode |= S_IFREG; | |
116 | } else if (type == UNIX_SYMLINK) { | |
117 | inode->i_mode |= S_IFLNK; | |
118 | } else if (type == UNIX_DIR) { | |
119 | inode->i_mode |= S_IFDIR; | |
120 | } else if (type == UNIX_CHARDEV) { | |
121 | inode->i_mode |= S_IFCHR; | |
122 | inode->i_rdev = MKDEV(le64_to_cpu(findData.DevMajor), | |
123 | le64_to_cpu(findData.DevMinor) & MINORMASK); | |
124 | } else if (type == UNIX_BLOCKDEV) { | |
125 | inode->i_mode |= S_IFBLK; | |
126 | inode->i_rdev = MKDEV(le64_to_cpu(findData.DevMajor), | |
127 | le64_to_cpu(findData.DevMinor) & MINORMASK); | |
128 | } else if (type == UNIX_FIFO) { | |
129 | inode->i_mode |= S_IFIFO; | |
130 | } else if (type == UNIX_SOCKET) { | |
131 | inode->i_mode |= S_IFSOCK; | |
132 | } | |
133 | inode->i_uid = le64_to_cpu(findData.Uid); | |
134 | inode->i_gid = le64_to_cpu(findData.Gid); | |
135 | inode->i_nlink = le64_to_cpu(findData.Nlinks); | |
136 | ||
d0d2f2df | 137 | if (is_size_safe_to_change(cifsInfo)) { |
1da177e4 LT |
138 | /* can not safely change the file size here if the |
139 | client is writing to it due to potential races */ | |
140 | ||
141 | i_size_write(inode, end_of_file); | |
142 | ||
143 | /* blksize needs to be multiple of two. So safer to default to | |
144 | blksize and blkbits set in superblock so 2**blkbits and blksize | |
145 | will match rather than setting to: | |
146 | (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/ | |
147 | ||
148 | /* This seems incredibly stupid but it turns out that i_blocks | |
149 | is not related to (i_size / i_blksize), instead 512 byte size | |
150 | is required for calculating num blocks */ | |
151 | ||
152 | /* 512 bytes (2**9) is the fake blocksize that must be used */ | |
153 | /* for this calculation */ | |
154 | inode->i_blocks = (512 - 1 + num_of_bytes) >> 9; | |
155 | } | |
156 | ||
157 | if (num_of_bytes < end_of_file) | |
158 | cFYI(1, ("allocation size less than end of file ")); | |
159 | cFYI(1, | |
160 | ("Size %ld and blocks %ld", | |
161 | (unsigned long) inode->i_size, inode->i_blocks)); | |
162 | if (S_ISREG(inode->i_mode)) { | |
163 | cFYI(1, (" File inode ")); | |
164 | inode->i_op = &cifs_file_inode_ops; | |
d0d2f2df | 165 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) |
1da177e4 LT |
166 | inode->i_fop = &cifs_file_direct_ops; |
167 | else | |
168 | inode->i_fop = &cifs_file_ops; | |
c46fa8ac SF |
169 | if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) |
170 | inode->i_fop->lock = NULL; | |
1da177e4 | 171 | inode->i_data.a_ops = &cifs_addr_ops; |
bfa0d75a SF |
172 | /* check if server can support readpages */ |
173 | if(pTcon->ses->server->maxBuf < | |
174 | 4096 + MAX_CIFS_HDR_SIZE) | |
175 | inode->i_data.a_ops->readpages = NULL; | |
1da177e4 LT |
176 | } else if (S_ISDIR(inode->i_mode)) { |
177 | cFYI(1, (" Directory inode")); | |
178 | inode->i_op = &cifs_dir_inode_ops; | |
179 | inode->i_fop = &cifs_dir_ops; | |
180 | } else if (S_ISLNK(inode->i_mode)) { | |
181 | cFYI(1, (" Symbolic Link inode ")); | |
182 | inode->i_op = &cifs_symlink_inode_ops; | |
183 | /* tmp_inode->i_fop = */ /* do not need to set to anything */ | |
184 | } else { | |
185 | cFYI(1, (" Init special inode ")); | |
186 | init_special_inode(inode, inode->i_mode, | |
187 | inode->i_rdev); | |
188 | } | |
189 | } | |
190 | return rc; | |
191 | } | |
192 | ||
193 | int cifs_get_inode_info(struct inode **pinode, | |
194 | const unsigned char *search_path, FILE_ALL_INFO *pfindData, | |
195 | struct super_block *sb, int xid) | |
196 | { | |
197 | int rc = 0; | |
198 | struct cifsTconInfo *pTcon; | |
199 | struct inode *inode; | |
200 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); | |
201 | char *tmp_path; | |
202 | char *buf = NULL; | |
203 | ||
204 | pTcon = cifs_sb->tcon; | |
205 | cFYI(1,("Getting info on %s ", search_path)); | |
206 | ||
d0d2f2df SF |
207 | if ((pfindData == NULL) && (*pinode != NULL)) { |
208 | if (CIFS_I(*pinode)->clientCanCacheRead) { | |
1da177e4 LT |
209 | cFYI(1,("No need to revalidate cached inode sizes")); |
210 | return rc; | |
211 | } | |
212 | } | |
213 | ||
214 | /* if file info not passed in then get it from server */ | |
d0d2f2df | 215 | if (pfindData == NULL) { |
1da177e4 | 216 | buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); |
d0d2f2df | 217 | if (buf == NULL) |
1da177e4 LT |
218 | return -ENOMEM; |
219 | pfindData = (FILE_ALL_INFO *)buf; | |
220 | /* could do find first instead but this returns more info */ | |
221 | rc = CIFSSMBQPathInfo(xid, pTcon, search_path, pfindData, | |
6b8edfe0 | 222 | cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & |
737b758c | 223 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
6b8edfe0 SF |
224 | /* BB optimize code so we do not make the above call |
225 | when server claims no NT SMB support and the above call | |
226 | failed at least once - set flag in tcon or mount */ | |
227 | if((rc == -EOPNOTSUPP) || (rc == -EINVAL)) { | |
228 | rc = SMBQueryInformation(xid, pTcon, search_path, | |
229 | pfindData, cifs_sb->local_nls, | |
230 | cifs_sb->mnt_cifs_flags & | |
231 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
232 | } | |
233 | ||
1da177e4 LT |
234 | } |
235 | /* dump_mem("\nQPathInfo return data",&findData, sizeof(findData)); */ | |
236 | if (rc) { | |
237 | if (rc == -EREMOTE) { | |
238 | tmp_path = | |
239 | kmalloc(strnlen | |
240 | (pTcon->treeName, | |
241 | MAX_TREE_SIZE + 1) + | |
242 | strnlen(search_path, MAX_PATHCONF) + 1, | |
243 | GFP_KERNEL); | |
244 | if (tmp_path == NULL) { | |
245 | kfree(buf); | |
246 | return -ENOMEM; | |
247 | } | |
248 | ||
249 | strncpy(tmp_path, pTcon->treeName, MAX_TREE_SIZE); | |
250 | strncat(tmp_path, search_path, MAX_PATHCONF); | |
251 | rc = connect_to_dfs_path(xid, pTcon->ses, | |
252 | /* treename + */ tmp_path, | |
737b758c SF |
253 | cifs_sb->local_nls, |
254 | cifs_sb->mnt_cifs_flags & | |
255 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
256 | kfree(tmp_path); |
257 | /* BB fix up inode etc. */ | |
258 | } else if (rc) { | |
259 | kfree(buf); | |
260 | return rc; | |
261 | } | |
262 | } else { | |
263 | struct cifsInodeInfo *cifsInfo; | |
264 | __u32 attr = le32_to_cpu(pfindData->Attributes); | |
265 | ||
266 | /* get new inode */ | |
267 | if (*pinode == NULL) { | |
268 | *pinode = new_inode(sb); | |
269 | if (*pinode == NULL) | |
270 | return -ENOMEM; | |
271 | /* Is an i_ino of zero legal? Can we use that to check | |
272 | if the server supports returning inode numbers? Are | |
273 | there other sanity checks we can use to ensure that | |
274 | the server is really filling in that field? */ | |
275 | ||
276 | /* We can not use the IndexNumber field by default from | |
277 | Windows or Samba (in ALL_INFO buf) but we can request | |
278 | it explicitly. It may not be unique presumably if | |
279 | the server has multiple devices mounted under one | |
280 | share */ | |
281 | ||
282 | /* There may be higher info levels that work but are | |
283 | there Windows server or network appliances for which | |
284 | IndexNumber field is not guaranteed unique? */ | |
285 | ||
286 | #ifdef CONFIG_CIFS_EXPERIMENTAL | |
d0d2f2df | 287 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM){ |
1da177e4 LT |
288 | int rc1 = 0; |
289 | __u64 inode_num; | |
290 | ||
291 | rc1 = CIFSGetSrvInodeNumber(xid, pTcon, | |
292 | search_path, &inode_num, | |
737b758c SF |
293 | cifs_sb->local_nls, |
294 | cifs_sb->mnt_cifs_flags & | |
295 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
d0d2f2df | 296 | if (rc1) { |
1da177e4 LT |
297 | cFYI(1,("GetSrvInodeNum rc %d", rc1)); |
298 | /* BB EOPNOSUPP disable SERVER_INUM? */ | |
299 | } else /* do we need cast or hash to ino? */ | |
300 | (*pinode)->i_ino = inode_num; | |
301 | } /* else ino incremented to unique num in new_inode*/ | |
302 | #endif /* CIFS_EXPERIMENTAL */ | |
303 | insert_inode_hash(*pinode); | |
304 | } | |
305 | inode = *pinode; | |
306 | cifsInfo = CIFS_I(inode); | |
307 | cifsInfo->cifsAttrs = attr; | |
308 | cFYI(1, (" Old time %ld ", cifsInfo->time)); | |
309 | cifsInfo->time = jiffies; | |
310 | cFYI(1, (" New time %ld ", cifsInfo->time)); | |
311 | ||
312 | /* blksize needs to be multiple of two. So safer to default to | |
313 | blksize and blkbits set in superblock so 2**blkbits and blksize | |
314 | will match rather than setting to: | |
315 | (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/ | |
316 | ||
317 | /* Linux can not store file creation time unfortunately so we ignore it */ | |
318 | inode->i_atime = | |
319 | cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastAccessTime)); | |
320 | inode->i_mtime = | |
321 | cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime)); | |
322 | inode->i_ctime = | |
323 | cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime)); | |
324 | cFYI(0, (" Attributes came in as 0x%x ", attr)); | |
325 | ||
326 | /* set default mode. will override for dirs below */ | |
327 | if (atomic_read(&cifsInfo->inUse) == 0) | |
328 | /* new inode, can safely set these fields */ | |
329 | inode->i_mode = cifs_sb->mnt_file_mode; | |
330 | ||
331 | /* if (attr & ATTR_REPARSE) */ | |
332 | /* We no longer handle these as symlinks because we could not | |
333 | follow them due to the absolute path with drive letter */ | |
334 | if (attr & ATTR_DIRECTORY) { | |
335 | /* override default perms since we do not do byte range locking | |
336 | on dirs */ | |
337 | inode->i_mode = cifs_sb->mnt_dir_mode; | |
338 | inode->i_mode |= S_IFDIR; | |
eda3c029 SF |
339 | } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) && |
340 | (cifsInfo->cifsAttrs & ATTR_SYSTEM) && | |
341 | /* No need to le64 convert size of zero */ | |
342 | (pfindData->EndOfFile == 0)) { | |
343 | inode->i_mode = cifs_sb->mnt_file_mode; | |
344 | inode->i_mode |= S_IFIFO; | |
345 | /* BB Finish for SFU style symlinks and devies */ | |
346 | /* } else if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) && | |
347 | (cifsInfo->cifsAttrs & ATTR_SYSTEM) && ) */ | |
348 | ||
1da177e4 LT |
349 | } else { |
350 | inode->i_mode |= S_IFREG; | |
351 | /* treat the dos attribute of read-only as read-only | |
352 | mode e.g. 555 */ | |
353 | if (cifsInfo->cifsAttrs & ATTR_READONLY) | |
354 | inode->i_mode &= ~(S_IWUGO); | |
355 | /* BB add code here - | |
356 | validate if device or weird share or device type? */ | |
357 | } | |
358 | if (is_size_safe_to_change(cifsInfo)) { | |
359 | /* can not safely change the file size here if the | |
360 | client is writing to it due to potential races */ | |
361 | i_size_write(inode,le64_to_cpu(pfindData->EndOfFile)); | |
362 | ||
363 | /* 512 bytes (2**9) is the fake blocksize that must be | |
364 | used for this calculation */ | |
365 | inode->i_blocks = (512 - 1 + le64_to_cpu( | |
366 | pfindData->AllocationSize)) >> 9; | |
367 | } | |
368 | ||
369 | inode->i_nlink = le32_to_cpu(pfindData->NumberOfLinks); | |
370 | ||
371 | /* BB fill in uid and gid here? with help from winbind? | |
372 | or retrieve from NTFS stream extended attribute */ | |
373 | if (atomic_read(&cifsInfo->inUse) == 0) { | |
374 | inode->i_uid = cifs_sb->mnt_uid; | |
375 | inode->i_gid = cifs_sb->mnt_gid; | |
376 | /* set so we do not keep refreshing these fields with | |
377 | bad data after user has changed them in memory */ | |
378 | atomic_set(&cifsInfo->inUse,1); | |
379 | } | |
380 | ||
381 | if (S_ISREG(inode->i_mode)) { | |
382 | cFYI(1, (" File inode ")); | |
383 | inode->i_op = &cifs_file_inode_ops; | |
d0d2f2df | 384 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DIRECT_IO) |
1da177e4 LT |
385 | inode->i_fop = &cifs_file_direct_ops; |
386 | else | |
387 | inode->i_fop = &cifs_file_ops; | |
c46fa8ac SF |
388 | if(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_BRL) |
389 | inode->i_fop->lock = NULL; | |
1da177e4 | 390 | inode->i_data.a_ops = &cifs_addr_ops; |
bfa0d75a SF |
391 | if(pTcon->ses->server->maxBuf < |
392 | 4096 + MAX_CIFS_HDR_SIZE) | |
393 | inode->i_data.a_ops->readpages = NULL; | |
1da177e4 LT |
394 | } else if (S_ISDIR(inode->i_mode)) { |
395 | cFYI(1, (" Directory inode ")); | |
396 | inode->i_op = &cifs_dir_inode_ops; | |
397 | inode->i_fop = &cifs_dir_ops; | |
398 | } else if (S_ISLNK(inode->i_mode)) { | |
399 | cFYI(1, (" Symbolic Link inode ")); | |
400 | inode->i_op = &cifs_symlink_inode_ops; | |
401 | } else { | |
402 | init_special_inode(inode, inode->i_mode, | |
403 | inode->i_rdev); | |
404 | } | |
405 | } | |
406 | kfree(buf); | |
407 | return rc; | |
408 | } | |
409 | ||
410 | /* gets root inode */ | |
411 | void cifs_read_inode(struct inode *inode) | |
412 | { | |
413 | int xid; | |
414 | struct cifs_sb_info *cifs_sb; | |
415 | ||
416 | cifs_sb = CIFS_SB(inode->i_sb); | |
417 | xid = GetXid(); | |
418 | if (cifs_sb->tcon->ses->capabilities & CAP_UNIX) | |
419 | cifs_get_inode_info_unix(&inode, "", inode->i_sb,xid); | |
420 | else | |
421 | cifs_get_inode_info(&inode, "", NULL, inode->i_sb,xid); | |
422 | /* can not call macro FreeXid here since in a void func */ | |
423 | _FreeXid(xid); | |
424 | } | |
425 | ||
426 | int cifs_unlink(struct inode *inode, struct dentry *direntry) | |
427 | { | |
428 | int rc = 0; | |
429 | int xid; | |
430 | struct cifs_sb_info *cifs_sb; | |
431 | struct cifsTconInfo *pTcon; | |
432 | char *full_path = NULL; | |
433 | struct cifsInodeInfo *cifsInode; | |
434 | FILE_BASIC_INFO *pinfo_buf; | |
435 | ||
436 | cFYI(1, (" cifs_unlink, inode = 0x%p with ", inode)); | |
437 | ||
438 | xid = GetXid(); | |
439 | ||
440 | cifs_sb = CIFS_SB(inode->i_sb); | |
441 | pTcon = cifs_sb->tcon; | |
442 | ||
443 | /* Unlink can be called from rename so we can not grab the sem here | |
444 | since we deadlock otherwise */ | |
445 | /* down(&direntry->d_sb->s_vfs_rename_sem);*/ | |
7f57356b | 446 | full_path = build_path_from_dentry(direntry); |
1da177e4 LT |
447 | /* up(&direntry->d_sb->s_vfs_rename_sem);*/ |
448 | if (full_path == NULL) { | |
449 | FreeXid(xid); | |
450 | return -ENOMEM; | |
451 | } | |
737b758c SF |
452 | rc = CIFSSMBDelFile(xid, pTcon, full_path, cifs_sb->local_nls, |
453 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
454 | |
455 | if (!rc) { | |
d0d2f2df | 456 | if (direntry->d_inode) |
b2aeb9d5 | 457 | direntry->d_inode->i_nlink--; |
1da177e4 LT |
458 | } else if (rc == -ENOENT) { |
459 | d_drop(direntry); | |
460 | } else if (rc == -ETXTBSY) { | |
461 | int oplock = FALSE; | |
462 | __u16 netfid; | |
463 | ||
464 | rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, DELETE, | |
465 | CREATE_NOT_DIR | CREATE_DELETE_ON_CLOSE, | |
737b758c SF |
466 | &netfid, &oplock, NULL, cifs_sb->local_nls, |
467 | cifs_sb->mnt_cifs_flags & | |
468 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
469 | if (rc==0) { |
470 | CIFSSMBRenameOpenFile(xid, pTcon, netfid, NULL, | |
737b758c SF |
471 | cifs_sb->local_nls, |
472 | cifs_sb->mnt_cifs_flags & | |
473 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 | 474 | CIFSSMBClose(xid, pTcon, netfid); |
d0d2f2df | 475 | if (direntry->d_inode) |
b2aeb9d5 | 476 | direntry->d_inode->i_nlink--; |
1da177e4 LT |
477 | } |
478 | } else if (rc == -EACCES) { | |
479 | /* try only if r/o attribute set in local lookup data? */ | |
480 | pinfo_buf = kmalloc(sizeof(FILE_BASIC_INFO), GFP_KERNEL); | |
481 | if (pinfo_buf) { | |
482 | memset(pinfo_buf, 0, sizeof(FILE_BASIC_INFO)); | |
483 | /* ATTRS set to normal clears r/o bit */ | |
484 | pinfo_buf->Attributes = cpu_to_le32(ATTR_NORMAL); | |
485 | if (!(pTcon->ses->flags & CIFS_SES_NT4)) | |
486 | rc = CIFSSMBSetTimes(xid, pTcon, full_path, | |
487 | pinfo_buf, | |
737b758c SF |
488 | cifs_sb->local_nls, |
489 | cifs_sb->mnt_cifs_flags & | |
490 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
491 | else |
492 | rc = -EOPNOTSUPP; | |
493 | ||
494 | if (rc == -EOPNOTSUPP) { | |
495 | int oplock = FALSE; | |
496 | __u16 netfid; | |
497 | /* rc = CIFSSMBSetAttrLegacy(xid, pTcon, | |
498 | full_path, | |
499 | (__u16)ATTR_NORMAL, | |
500 | cifs_sb->local_nls); | |
501 | For some strange reason it seems that NT4 eats the | |
502 | old setattr call without actually setting the | |
503 | attributes so on to the third attempted workaround | |
504 | */ | |
505 | ||
506 | /* BB could scan to see if we already have it open | |
507 | and pass in pid of opener to function */ | |
508 | rc = CIFSSMBOpen(xid, pTcon, full_path, | |
509 | FILE_OPEN, SYNCHRONIZE | | |
510 | FILE_WRITE_ATTRIBUTES, 0, | |
511 | &netfid, &oplock, NULL, | |
737b758c SF |
512 | cifs_sb->local_nls, |
513 | cifs_sb->mnt_cifs_flags & | |
514 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
515 | if (rc==0) { |
516 | rc = CIFSSMBSetFileTimes(xid, pTcon, | |
517 | pinfo_buf, | |
518 | netfid); | |
519 | CIFSSMBClose(xid, pTcon, netfid); | |
520 | } | |
521 | } | |
522 | kfree(pinfo_buf); | |
523 | } | |
524 | if (rc==0) { | |
737b758c SF |
525 | rc = CIFSSMBDelFile(xid, pTcon, full_path, |
526 | cifs_sb->local_nls, | |
527 | cifs_sb->mnt_cifs_flags & | |
528 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 | 529 | if (!rc) { |
d0d2f2df | 530 | if (direntry->d_inode) |
b2aeb9d5 | 531 | direntry->d_inode->i_nlink--; |
1da177e4 LT |
532 | } else if (rc == -ETXTBSY) { |
533 | int oplock = FALSE; | |
534 | __u16 netfid; | |
535 | ||
536 | rc = CIFSSMBOpen(xid, pTcon, full_path, | |
537 | FILE_OPEN, DELETE, | |
538 | CREATE_NOT_DIR | | |
539 | CREATE_DELETE_ON_CLOSE, | |
540 | &netfid, &oplock, NULL, | |
737b758c SF |
541 | cifs_sb->local_nls, |
542 | cifs_sb->mnt_cifs_flags & | |
543 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
544 | if (rc==0) { |
545 | CIFSSMBRenameOpenFile(xid, pTcon, | |
546 | netfid, NULL, | |
737b758c SF |
547 | cifs_sb->local_nls, |
548 | cifs_sb->mnt_cifs_flags & | |
549 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 | 550 | CIFSSMBClose(xid, pTcon, netfid); |
d0d2f2df | 551 | if (direntry->d_inode) |
b2aeb9d5 | 552 | direntry->d_inode->i_nlink--; |
1da177e4 LT |
553 | } |
554 | /* BB if rc = -ETXTBUSY goto the rename logic BB */ | |
555 | } | |
556 | } | |
557 | } | |
d0d2f2df | 558 | if (direntry->d_inode) { |
b2aeb9d5 SF |
559 | cifsInode = CIFS_I(direntry->d_inode); |
560 | cifsInode->time = 0; /* will force revalidate to get info | |
561 | when needed */ | |
562 | direntry->d_inode->i_ctime = current_fs_time(inode->i_sb); | |
563 | } | |
564 | inode->i_ctime = inode->i_mtime = current_fs_time(inode->i_sb); | |
1da177e4 LT |
565 | cifsInode = CIFS_I(inode); |
566 | cifsInode->time = 0; /* force revalidate of dir as well */ | |
567 | ||
568 | kfree(full_path); | |
569 | FreeXid(xid); | |
570 | return rc; | |
571 | } | |
572 | ||
573 | int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode) | |
574 | { | |
575 | int rc = 0; | |
576 | int xid; | |
577 | struct cifs_sb_info *cifs_sb; | |
578 | struct cifsTconInfo *pTcon; | |
579 | char *full_path = NULL; | |
580 | struct inode *newinode = NULL; | |
581 | ||
582 | cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p ", mode, inode)); | |
583 | ||
584 | xid = GetXid(); | |
585 | ||
586 | cifs_sb = CIFS_SB(inode->i_sb); | |
587 | pTcon = cifs_sb->tcon; | |
588 | ||
589 | down(&inode->i_sb->s_vfs_rename_sem); | |
7f57356b | 590 | full_path = build_path_from_dentry(direntry); |
1da177e4 LT |
591 | up(&inode->i_sb->s_vfs_rename_sem); |
592 | if (full_path == NULL) { | |
593 | FreeXid(xid); | |
594 | return -ENOMEM; | |
595 | } | |
596 | /* BB add setting the equivalent of mode via CreateX w/ACLs */ | |
737b758c SF |
597 | rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls, |
598 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
599 | if (rc) { |
600 | cFYI(1, ("cifs_mkdir returned 0x%x ", rc)); | |
601 | d_drop(direntry); | |
602 | } else { | |
603 | inode->i_nlink++; | |
604 | if (pTcon->ses->capabilities & CAP_UNIX) | |
605 | rc = cifs_get_inode_info_unix(&newinode, full_path, | |
606 | inode->i_sb,xid); | |
607 | else | |
608 | rc = cifs_get_inode_info(&newinode, full_path, NULL, | |
609 | inode->i_sb,xid); | |
610 | ||
b92327fe SF |
611 | if (pTcon->nocase) |
612 | direntry->d_op = &cifs_ci_dentry_ops; | |
613 | else | |
614 | direntry->d_op = &cifs_dentry_ops; | |
1da177e4 LT |
615 | d_instantiate(direntry, newinode); |
616 | if (direntry->d_inode) | |
617 | direntry->d_inode->i_nlink = 2; | |
618 | if (cifs_sb->tcon->ses->capabilities & CAP_UNIX) | |
d0d2f2df | 619 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { |
1da177e4 LT |
620 | CIFSSMBUnixSetPerms(xid, pTcon, full_path, |
621 | mode, | |
622 | (__u64)current->euid, | |
623 | (__u64)current->egid, | |
624 | 0 /* dev_t */, | |
737b758c SF |
625 | cifs_sb->local_nls, |
626 | cifs_sb->mnt_cifs_flags & | |
627 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
628 | } else { |
629 | CIFSSMBUnixSetPerms(xid, pTcon, full_path, | |
630 | mode, (__u64)-1, | |
631 | (__u64)-1, 0 /* dev_t */, | |
737b758c SF |
632 | cifs_sb->local_nls, |
633 | cifs_sb->mnt_cifs_flags & | |
634 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
635 | } |
636 | else { | |
637 | /* BB to be implemented via Windows secrty descriptors | |
638 | eg CIFSSMBWinSetPerms(xid, pTcon, full_path, mode, | |
639 | -1, -1, local_nls); */ | |
640 | } | |
641 | } | |
642 | kfree(full_path); | |
643 | FreeXid(xid); | |
644 | return rc; | |
645 | } | |
646 | ||
647 | int cifs_rmdir(struct inode *inode, struct dentry *direntry) | |
648 | { | |
649 | int rc = 0; | |
650 | int xid; | |
651 | struct cifs_sb_info *cifs_sb; | |
652 | struct cifsTconInfo *pTcon; | |
653 | char *full_path = NULL; | |
654 | struct cifsInodeInfo *cifsInode; | |
655 | ||
656 | cFYI(1, (" cifs_rmdir, inode = 0x%p with ", inode)); | |
657 | ||
658 | xid = GetXid(); | |
659 | ||
660 | cifs_sb = CIFS_SB(inode->i_sb); | |
661 | pTcon = cifs_sb->tcon; | |
662 | ||
663 | down(&inode->i_sb->s_vfs_rename_sem); | |
7f57356b | 664 | full_path = build_path_from_dentry(direntry); |
1da177e4 LT |
665 | up(&inode->i_sb->s_vfs_rename_sem); |
666 | if (full_path == NULL) { | |
667 | FreeXid(xid); | |
668 | return -ENOMEM; | |
669 | } | |
670 | ||
737b758c SF |
671 | rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls, |
672 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
673 | |
674 | if (!rc) { | |
675 | inode->i_nlink--; | |
676 | i_size_write(direntry->d_inode,0); | |
677 | direntry->d_inode->i_nlink = 0; | |
678 | } | |
679 | ||
680 | cifsInode = CIFS_I(direntry->d_inode); | |
681 | cifsInode->time = 0; /* force revalidate to go get info when | |
682 | needed */ | |
683 | direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime = | |
684 | current_fs_time(inode->i_sb); | |
685 | ||
686 | kfree(full_path); | |
687 | FreeXid(xid); | |
688 | return rc; | |
689 | } | |
690 | ||
691 | int cifs_rename(struct inode *source_inode, struct dentry *source_direntry, | |
692 | struct inode *target_inode, struct dentry *target_direntry) | |
693 | { | |
694 | char *fromName; | |
695 | char *toName; | |
696 | struct cifs_sb_info *cifs_sb_source; | |
697 | struct cifs_sb_info *cifs_sb_target; | |
698 | struct cifsTconInfo *pTcon; | |
699 | int xid; | |
700 | int rc = 0; | |
701 | ||
702 | xid = GetXid(); | |
703 | ||
704 | cifs_sb_target = CIFS_SB(target_inode->i_sb); | |
705 | cifs_sb_source = CIFS_SB(source_inode->i_sb); | |
706 | pTcon = cifs_sb_source->tcon; | |
707 | ||
708 | if (pTcon != cifs_sb_target->tcon) { | |
709 | FreeXid(xid); | |
710 | return -EXDEV; /* BB actually could be allowed if same server, | |
711 | but different share. | |
712 | Might eventually add support for this */ | |
713 | } | |
714 | ||
715 | /* we already have the rename sem so we do not need to grab it again | |
716 | here to protect the path integrity */ | |
7f57356b SF |
717 | fromName = build_path_from_dentry(source_direntry); |
718 | toName = build_path_from_dentry(target_direntry); | |
1da177e4 LT |
719 | if ((fromName == NULL) || (toName == NULL)) { |
720 | rc = -ENOMEM; | |
721 | goto cifs_rename_exit; | |
722 | } | |
723 | ||
724 | rc = CIFSSMBRename(xid, pTcon, fromName, toName, | |
737b758c SF |
725 | cifs_sb_source->local_nls, |
726 | cifs_sb_source->mnt_cifs_flags & | |
727 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
728 | if (rc == -EEXIST) { |
729 | /* check if they are the same file because rename of hardlinked | |
730 | files is a noop */ | |
731 | FILE_UNIX_BASIC_INFO *info_buf_source; | |
732 | FILE_UNIX_BASIC_INFO *info_buf_target; | |
733 | ||
734 | info_buf_source = | |
735 | kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL); | |
736 | if (info_buf_source != NULL) { | |
737 | info_buf_target = info_buf_source + 1; | |
738 | rc = CIFSSMBUnixQPathInfo(xid, pTcon, fromName, | |
737b758c SF |
739 | info_buf_source, cifs_sb_source->local_nls, |
740 | cifs_sb_source->mnt_cifs_flags & | |
741 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
742 | if (rc == 0) { |
743 | rc = CIFSSMBUnixQPathInfo(xid, pTcon, toName, | |
744 | info_buf_target, | |
737b758c SF |
745 | cifs_sb_target->local_nls, |
746 | /* remap based on source sb */ | |
747 | cifs_sb_source->mnt_cifs_flags & | |
748 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
749 | } |
750 | if ((rc == 0) && | |
751 | (info_buf_source->UniqueId == | |
752 | info_buf_target->UniqueId)) { | |
753 | /* do not rename since the files are hardlinked which | |
754 | is a noop */ | |
755 | } else { | |
756 | /* we either can not tell the files are hardlinked | |
757 | (as with Windows servers) or files are not | |
758 | hardlinked so delete the target manually before | |
759 | renaming to follow POSIX rather than Windows | |
760 | semantics */ | |
761 | cifs_unlink(target_inode, target_direntry); | |
762 | rc = CIFSSMBRename(xid, pTcon, fromName, | |
763 | toName, | |
737b758c SF |
764 | cifs_sb_source->local_nls, |
765 | cifs_sb_source->mnt_cifs_flags | |
766 | & CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
767 | } |
768 | kfree(info_buf_source); | |
769 | } /* if we can not get memory just leave rc as EEXIST */ | |
770 | } | |
771 | ||
772 | if (rc) { | |
773 | cFYI(1, ("rename rc %d", rc)); | |
774 | } | |
775 | ||
776 | if ((rc == -EIO) || (rc == -EEXIST)) { | |
777 | int oplock = FALSE; | |
778 | __u16 netfid; | |
779 | ||
780 | /* BB FIXME Is Generic Read correct for rename? */ | |
781 | /* if renaming directory - we should not say CREATE_NOT_DIR, | |
782 | need to test renaming open directory, also GENERIC_READ | |
783 | might not right be right access to request */ | |
784 | rc = CIFSSMBOpen(xid, pTcon, fromName, FILE_OPEN, GENERIC_READ, | |
785 | CREATE_NOT_DIR, &netfid, &oplock, NULL, | |
737b758c SF |
786 | cifs_sb_source->local_nls, |
787 | cifs_sb_source->mnt_cifs_flags & | |
788 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
789 | if (rc==0) { |
790 | CIFSSMBRenameOpenFile(xid, pTcon, netfid, toName, | |
737b758c SF |
791 | cifs_sb_source->local_nls, |
792 | cifs_sb_source->mnt_cifs_flags & | |
793 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
794 | CIFSSMBClose(xid, pTcon, netfid); |
795 | } | |
796 | } | |
797 | ||
798 | cifs_rename_exit: | |
799 | kfree(fromName); | |
800 | kfree(toName); | |
801 | FreeXid(xid); | |
802 | return rc; | |
803 | } | |
804 | ||
805 | int cifs_revalidate(struct dentry *direntry) | |
806 | { | |
807 | int xid; | |
808 | int rc = 0; | |
809 | char *full_path; | |
810 | struct cifs_sb_info *cifs_sb; | |
811 | struct cifsInodeInfo *cifsInode; | |
812 | loff_t local_size; | |
813 | struct timespec local_mtime; | |
814 | int invalidate_inode = FALSE; | |
815 | ||
816 | if (direntry->d_inode == NULL) | |
817 | return -ENOENT; | |
818 | ||
819 | cifsInode = CIFS_I(direntry->d_inode); | |
820 | ||
821 | if (cifsInode == NULL) | |
822 | return -ENOENT; | |
823 | ||
824 | /* no sense revalidating inode info on file that no one can write */ | |
825 | if (CIFS_I(direntry->d_inode)->clientCanCacheRead) | |
826 | return rc; | |
827 | ||
828 | xid = GetXid(); | |
829 | ||
830 | cifs_sb = CIFS_SB(direntry->d_sb); | |
831 | ||
832 | /* can not safely grab the rename sem here if rename calls revalidate | |
833 | since that would deadlock */ | |
7f57356b | 834 | full_path = build_path_from_dentry(direntry); |
1da177e4 LT |
835 | if (full_path == NULL) { |
836 | FreeXid(xid); | |
837 | return -ENOMEM; | |
838 | } | |
839 | cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld " | |
840 | "jiffies %ld", full_path, direntry->d_inode, | |
841 | direntry->d_inode->i_count.counter, direntry, | |
842 | direntry->d_time, jiffies)); | |
843 | ||
844 | if (cifsInode->time == 0) { | |
845 | /* was set to zero previously to force revalidate */ | |
846 | } else if (time_before(jiffies, cifsInode->time + HZ) && | |
847 | lookupCacheEnabled) { | |
848 | if ((S_ISREG(direntry->d_inode->i_mode) == 0) || | |
849 | (direntry->d_inode->i_nlink == 1)) { | |
850 | kfree(full_path); | |
851 | FreeXid(xid); | |
852 | return rc; | |
853 | } else { | |
854 | cFYI(1, ("Have to revalidate file due to hardlinks")); | |
855 | } | |
856 | } | |
857 | ||
858 | /* save mtime and size */ | |
859 | local_mtime = direntry->d_inode->i_mtime; | |
860 | local_size = direntry->d_inode->i_size; | |
861 | ||
862 | if (cifs_sb->tcon->ses->capabilities & CAP_UNIX) { | |
863 | rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path, | |
864 | direntry->d_sb,xid); | |
865 | if (rc) { | |
866 | cFYI(1, ("error on getting revalidate info %d", rc)); | |
867 | /* if (rc != -ENOENT) | |
868 | rc = 0; */ /* BB should we cache info on | |
869 | certain errors? */ | |
870 | } | |
871 | } else { | |
872 | rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL, | |
873 | direntry->d_sb,xid); | |
874 | if (rc) { | |
875 | cFYI(1, ("error on getting revalidate info %d", rc)); | |
876 | /* if (rc != -ENOENT) | |
877 | rc = 0; */ /* BB should we cache info on | |
878 | certain errors? */ | |
879 | } | |
880 | } | |
881 | /* should we remap certain errors, access denied?, to zero */ | |
882 | ||
883 | /* if not oplocked, we invalidate inode pages if mtime or file size | |
884 | had changed on server */ | |
885 | ||
886 | if (timespec_equal(&local_mtime,&direntry->d_inode->i_mtime) && | |
887 | (local_size == direntry->d_inode->i_size)) { | |
888 | cFYI(1, ("cifs_revalidate - inode unchanged")); | |
889 | } else { | |
890 | /* file may have changed on server */ | |
891 | if (cifsInode->clientCanCacheRead) { | |
892 | /* no need to invalidate inode pages since we were the | |
893 | only ones who could have modified the file and the | |
894 | server copy is staler than ours */ | |
895 | } else { | |
896 | invalidate_inode = TRUE; | |
897 | } | |
898 | } | |
899 | ||
900 | /* can not grab this sem since kernel filesys locking documentation | |
901 | indicates i_sem may be taken by the kernel on lookup and rename | |
902 | which could deadlock if we grab the i_sem here as well */ | |
903 | /* down(&direntry->d_inode->i_sem);*/ | |
904 | /* need to write out dirty pages here */ | |
905 | if (direntry->d_inode->i_mapping) { | |
906 | /* do we need to lock inode until after invalidate completes | |
907 | below? */ | |
908 | filemap_fdatawrite(direntry->d_inode->i_mapping); | |
909 | } | |
910 | if (invalidate_inode) { | |
911 | if (direntry->d_inode->i_mapping) | |
912 | filemap_fdatawait(direntry->d_inode->i_mapping); | |
913 | /* may eventually have to do this for open files too */ | |
914 | if (list_empty(&(cifsInode->openFileList))) { | |
915 | /* Has changed on server - flush read ahead pages */ | |
916 | cFYI(1, ("Invalidating read ahead data on " | |
917 | "closed file")); | |
918 | invalidate_remote_inode(direntry->d_inode); | |
919 | } | |
920 | } | |
921 | /* up(&direntry->d_inode->i_sem); */ | |
922 | ||
923 | kfree(full_path); | |
924 | FreeXid(xid); | |
925 | return rc; | |
926 | } | |
927 | ||
928 | int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry, | |
929 | struct kstat *stat) | |
930 | { | |
931 | int err = cifs_revalidate(dentry); | |
932 | if (!err) | |
933 | generic_fillattr(dentry->d_inode, stat); | |
934 | return err; | |
935 | } | |
936 | ||
937 | static int cifs_truncate_page(struct address_space *mapping, loff_t from) | |
938 | { | |
939 | pgoff_t index = from >> PAGE_CACHE_SHIFT; | |
940 | unsigned offset = from & (PAGE_CACHE_SIZE - 1); | |
941 | struct page *page; | |
942 | char *kaddr; | |
943 | int rc = 0; | |
944 | ||
945 | page = grab_cache_page(mapping, index); | |
946 | if (!page) | |
947 | return -ENOMEM; | |
948 | ||
949 | kaddr = kmap_atomic(page, KM_USER0); | |
950 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); | |
951 | flush_dcache_page(page); | |
952 | kunmap_atomic(kaddr, KM_USER0); | |
953 | unlock_page(page); | |
954 | page_cache_release(page); | |
955 | return rc; | |
956 | } | |
957 | ||
958 | int cifs_setattr(struct dentry *direntry, struct iattr *attrs) | |
959 | { | |
960 | int xid; | |
961 | struct cifs_sb_info *cifs_sb; | |
962 | struct cifsTconInfo *pTcon; | |
963 | char *full_path = NULL; | |
964 | int rc = -EACCES; | |
965 | int found = FALSE; | |
966 | struct cifsFileInfo *open_file = NULL; | |
967 | FILE_BASIC_INFO time_buf; | |
968 | int set_time = FALSE; | |
969 | __u64 mode = 0xFFFFFFFFFFFFFFFFULL; | |
970 | __u64 uid = 0xFFFFFFFFFFFFFFFFULL; | |
971 | __u64 gid = 0xFFFFFFFFFFFFFFFFULL; | |
972 | struct cifsInodeInfo *cifsInode; | |
973 | struct list_head *tmp; | |
974 | ||
975 | xid = GetXid(); | |
976 | ||
977 | cFYI(1, (" In cifs_setattr, name = %s attrs->iavalid 0x%x ", | |
978 | direntry->d_name.name, attrs->ia_valid)); | |
979 | cifs_sb = CIFS_SB(direntry->d_inode->i_sb); | |
980 | pTcon = cifs_sb->tcon; | |
981 | ||
982 | down(&direntry->d_sb->s_vfs_rename_sem); | |
7f57356b | 983 | full_path = build_path_from_dentry(direntry); |
1da177e4 LT |
984 | up(&direntry->d_sb->s_vfs_rename_sem); |
985 | if (full_path == NULL) { | |
986 | FreeXid(xid); | |
987 | return -ENOMEM; | |
988 | } | |
989 | cifsInode = CIFS_I(direntry->d_inode); | |
990 | ||
991 | /* BB check if we need to refresh inode from server now ? BB */ | |
992 | ||
993 | /* need to flush data before changing file size on server */ | |
994 | filemap_fdatawrite(direntry->d_inode->i_mapping); | |
995 | filemap_fdatawait(direntry->d_inode->i_mapping); | |
996 | ||
997 | if (attrs->ia_valid & ATTR_SIZE) { | |
998 | read_lock(&GlobalSMBSeslock); | |
999 | /* To avoid spurious oplock breaks from server, in the case of | |
1000 | inodes that we already have open, avoid doing path based | |
1001 | setting of file size if we can do it by handle. | |
1002 | This keeps our caching token (oplock) and avoids timeouts | |
1003 | when the local oplock break takes longer to flush | |
1004 | writebehind data than the SMB timeout for the SetPathInfo | |
1005 | request would allow */ | |
1006 | list_for_each(tmp, &cifsInode->openFileList) { | |
1007 | open_file = list_entry(tmp, struct cifsFileInfo, | |
1008 | flist); | |
1009 | /* We check if file is open for writing first */ | |
1010 | if ((open_file->pfile) && | |
1011 | ((open_file->pfile->f_flags & O_RDWR) || | |
1012 | (open_file->pfile->f_flags & O_WRONLY))) { | |
1013 | if (open_file->invalidHandle == FALSE) { | |
1014 | /* we found a valid, writeable network | |
1015 | file handle to use to try to set the | |
1016 | file size */ | |
1017 | __u16 nfid = open_file->netfid; | |
1018 | __u32 npid = open_file->pid; | |
1019 | read_unlock(&GlobalSMBSeslock); | |
1020 | found = TRUE; | |
1021 | rc = CIFSSMBSetFileSize(xid, pTcon, | |
1022 | attrs->ia_size, nfid, npid, | |
1023 | FALSE); | |
1024 | cFYI(1, ("SetFileSize by handle " | |
1025 | "(setattrs) rc = %d", rc)); | |
1026 | /* Do not need reopen and retry on | |
1027 | EAGAIN since we will retry by | |
1028 | pathname below */ | |
1029 | ||
1030 | /* now that we found one valid file | |
1031 | handle no sense continuing to loop | |
1032 | trying others, so break here */ | |
3e87d803 SF |
1033 | /* if(rc == -EINVAL) { |
1034 | int bytes_written; | |
1035 | rc = CIFSSMBWrite(xid, pTcon, | |
1036 | nfid, 0, | |
1037 | attrs->ia_size, | |
1038 | &bytes_written, | |
1039 | NULL, NULL, long_op); | |
1040 | } */ | |
1da177e4 LT |
1041 | break; |
1042 | } | |
1043 | } | |
1044 | } | |
1045 | if (found == FALSE) | |
1046 | read_unlock(&GlobalSMBSeslock); | |
1047 | ||
1048 | if (rc != 0) { | |
1049 | /* Set file size by pathname rather than by handle | |
1050 | either because no valid, writeable file handle for | |
1051 | it was found or because there was an error setting | |
1052 | it by handle */ | |
1053 | rc = CIFSSMBSetEOF(xid, pTcon, full_path, | |
1054 | attrs->ia_size, FALSE, | |
737b758c SF |
1055 | cifs_sb->local_nls, |
1056 | cifs_sb->mnt_cifs_flags & | |
1057 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 | 1058 | cFYI(1, (" SetEOF by path (setattrs) rc = %d", rc)); |
3e87d803 SF |
1059 | /* if(rc == -EINVAL) |
1060 | old_style_set_eof_via_write(xid, pTcon, | |
1061 | full_path, | |
1062 | attrs->ia_size, | |
1063 | cifs_sb->local_nls, | |
1064 | cifs_sb->mnt_cifs_flags & | |
1065 | CIFS_MOUNT_MAP_SPECIAL_CHR);*/ | |
1da177e4 LT |
1066 | } |
1067 | ||
1068 | /* Server is ok setting allocation size implicitly - no need | |
1069 | to call: | |
1070 | CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, TRUE, | |
1071 | cifs_sb->local_nls); | |
1072 | */ | |
1073 | ||
1074 | if (rc == 0) { | |
1075 | rc = vmtruncate(direntry->d_inode, attrs->ia_size); | |
1076 | cifs_truncate_page(direntry->d_inode->i_mapping, | |
1077 | direntry->d_inode->i_size); | |
1078 | } | |
1079 | } | |
1080 | if (attrs->ia_valid & ATTR_UID) { | |
1081 | cFYI(1, (" CIFS - UID changed to %d", attrs->ia_uid)); | |
1082 | uid = attrs->ia_uid; | |
1083 | /* entry->uid = cpu_to_le16(attr->ia_uid); */ | |
1084 | } | |
1085 | if (attrs->ia_valid & ATTR_GID) { | |
1086 | cFYI(1, (" CIFS - GID changed to %d", attrs->ia_gid)); | |
1087 | gid = attrs->ia_gid; | |
1088 | /* entry->gid = cpu_to_le16(attr->ia_gid); */ | |
1089 | } | |
1090 | ||
1091 | time_buf.Attributes = 0; | |
1092 | if (attrs->ia_valid & ATTR_MODE) { | |
1093 | cFYI(1, (" CIFS - Mode changed to 0x%x", attrs->ia_mode)); | |
1094 | mode = attrs->ia_mode; | |
1095 | /* entry->mode = cpu_to_le16(attr->ia_mode); */ | |
1096 | } | |
1097 | ||
1098 | if ((cifs_sb->tcon->ses->capabilities & CAP_UNIX) | |
1099 | && (attrs->ia_valid & (ATTR_MODE | ATTR_GID | ATTR_UID))) | |
1100 | rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode, uid, gid, | |
737b758c SF |
1101 | 0 /* dev_t */, cifs_sb->local_nls, |
1102 | cifs_sb->mnt_cifs_flags & | |
1103 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
1104 | else if (attrs->ia_valid & ATTR_MODE) { |
1105 | if ((mode & S_IWUGO) == 0) /* not writeable */ { | |
1106 | if ((cifsInode->cifsAttrs & ATTR_READONLY) == 0) | |
1107 | time_buf.Attributes = | |
1108 | cpu_to_le32(cifsInode->cifsAttrs | | |
1109 | ATTR_READONLY); | |
1110 | } else if ((mode & S_IWUGO) == S_IWUGO) { | |
1111 | if (cifsInode->cifsAttrs & ATTR_READONLY) | |
1112 | time_buf.Attributes = | |
1113 | cpu_to_le32(cifsInode->cifsAttrs & | |
1114 | (~ATTR_READONLY)); | |
1115 | } | |
1116 | /* BB to be implemented - | |
1117 | via Windows security descriptors or streams */ | |
1118 | /* CIFSSMBWinSetPerms(xid, pTcon, full_path, mode, uid, gid, | |
1119 | cifs_sb->local_nls); */ | |
1120 | } | |
1121 | ||
1122 | if (attrs->ia_valid & ATTR_ATIME) { | |
1123 | set_time = TRUE; | |
1124 | time_buf.LastAccessTime = | |
1125 | cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime)); | |
1126 | } else | |
1127 | time_buf.LastAccessTime = 0; | |
1128 | ||
1129 | if (attrs->ia_valid & ATTR_MTIME) { | |
1130 | set_time = TRUE; | |
1131 | time_buf.LastWriteTime = | |
1132 | cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime)); | |
1133 | } else | |
1134 | time_buf.LastWriteTime = 0; | |
1135 | ||
1136 | if (attrs->ia_valid & ATTR_CTIME) { | |
1137 | set_time = TRUE; | |
1138 | cFYI(1, (" CIFS - CTIME changed ")); /* BB probably no need */ | |
1139 | time_buf.ChangeTime = | |
1140 | cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime)); | |
1141 | } else | |
1142 | time_buf.ChangeTime = 0; | |
1143 | ||
1144 | if (set_time || time_buf.Attributes) { | |
1145 | /* BB what if setting one attribute fails (such as size) but | |
1146 | time setting works? */ | |
1147 | time_buf.CreationTime = 0; /* do not change */ | |
1148 | /* In the future we should experiment - try setting timestamps | |
1149 | via Handle (SetFileInfo) instead of by path */ | |
1150 | if (!(pTcon->ses->flags & CIFS_SES_NT4)) | |
1151 | rc = CIFSSMBSetTimes(xid, pTcon, full_path, &time_buf, | |
737b758c SF |
1152 | cifs_sb->local_nls, |
1153 | cifs_sb->mnt_cifs_flags & | |
1154 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
1155 | else |
1156 | rc = -EOPNOTSUPP; | |
1157 | ||
1158 | if (rc == -EOPNOTSUPP) { | |
1159 | int oplock = FALSE; | |
1160 | __u16 netfid; | |
1161 | ||
1162 | cFYI(1, ("calling SetFileInfo since SetPathInfo for " | |
1163 | "times not supported by this server")); | |
1164 | /* BB we could scan to see if we already have it open | |
1165 | and pass in pid of opener to function */ | |
1166 | rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, | |
1167 | SYNCHRONIZE | FILE_WRITE_ATTRIBUTES, | |
1168 | CREATE_NOT_DIR, &netfid, &oplock, | |
737b758c SF |
1169 | NULL, cifs_sb->local_nls, |
1170 | cifs_sb->mnt_cifs_flags & | |
1171 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
1172 | if (rc==0) { |
1173 | rc = CIFSSMBSetFileTimes(xid, pTcon, &time_buf, | |
1174 | netfid); | |
1175 | CIFSSMBClose(xid, pTcon, netfid); | |
1176 | } else { | |
1177 | /* BB For even older servers we could convert time_buf | |
1178 | into old DOS style which uses two second | |
1179 | granularity */ | |
1180 | ||
1181 | /* rc = CIFSSMBSetTimesLegacy(xid, pTcon, full_path, | |
1182 | &time_buf, cifs_sb->local_nls); */ | |
1183 | } | |
1184 | } | |
1185 | } | |
1186 | ||
1187 | /* do not need local check to inode_check_ok since the server does | |
1188 | that */ | |
1189 | if (!rc) | |
1190 | rc = inode_setattr(direntry->d_inode, attrs); | |
1191 | kfree(full_path); | |
1192 | FreeXid(xid); | |
1193 | return rc; | |
1194 | } | |
1195 | ||
1196 | void cifs_delete_inode(struct inode *inode) | |
1197 | { | |
1198 | cFYI(1, ("In cifs_delete_inode, inode = 0x%p ", inode)); | |
1199 | /* may have to add back in if and when safe distributed caching of | |
1200 | directories added e.g. via FindNotify */ | |
1201 | } |