]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * fs/cifs/inode.c | |
3 | * | |
2dd29d31 | 4 | * Copyright (C) International Business Machines Corp., 2002,2007 |
1da177e4 LT |
5 | * Author(s): Steve French ([email protected]) |
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> | |
1da177e4 LT |
22 | #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 | ||
70eff55d CH |
32 | |
33 | static 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 | ||
75f12983 CH |
72 | static 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 | ||
1da177e4 LT |
156 | int 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; | |
26a21b98 | 167 | cFYI(1, ("Getting info on %s", search_path)); |
1da177e4 LT |
168 | /* could have done a find first instead but this returns more info */ |
169 | rc = CIFSSMBUnixQPathInfo(xid, pTcon, search_path, &findData, | |
737b758c SF |
170 | cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & |
171 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
172 | /* 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); | |
f6d09982 | 181 | if (tmp_path == NULL) |
1da177e4 | 182 | return -ENOMEM; |
f6d09982 | 183 | |
fb8c4b14 | 184 | /* have to skip first of the double backslash of |
1da177e4 LT |
185 | 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, | |
fb8c4b14 SF |
190 | cifs_sb->local_nls, |
191 | cifs_sb->mnt_cifs_flags & | |
737b758c | 192 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
1da177e4 LT |
193 | kfree(tmp_path); |
194 | ||
195 | /* BB fix up inode etc. */ | |
196 | } else if (rc) { | |
197 | return rc; | |
198 | } | |
199 | } else { | |
200 | struct cifsInodeInfo *cifsInfo; | |
1da177e4 LT |
201 | __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); | |
fb8c4b14 | 207 | if (*pinode == NULL) |
1da177e4 LT |
208 | 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? */ | |
d0d2f2df | 212 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) { |
1da177e4 LT |
213 | (*pinode)->i_ino = |
214 | (unsigned long)findData.UniqueId; | |
215 | } /* note ino incremented to unique num in new_inode */ | |
4523cc30 | 216 | if (sb->s_flags & MS_NOATIME) |
1b2b2126 | 217 | (*pinode)->i_flags |= S_NOATIME | S_NOCMTIME; |
50c2f753 | 218 | |
1da177e4 LT |
219 | insert_inode_hash(*pinode); |
220 | } | |
221 | ||
222 | inode = *pinode; | |
223 | cifsInfo = CIFS_I(inode); | |
224 | ||
26a21b98 | 225 | cFYI(1, ("Old time %ld", cifsInfo->time)); |
1da177e4 | 226 | cifsInfo->time = jiffies; |
26a21b98 | 227 | cFYI(1, ("New time %ld", cifsInfo->time)); |
1da177e4 | 228 | /* this is ok to set on every inode revalidate */ |
fb8c4b14 | 229 | atomic_set(&cifsInfo->inUse, 1); |
1da177e4 | 230 | |
75f12983 | 231 | cifs_unix_info_to_inode(inode, &findData, 0); |
1da177e4 | 232 | |
1da177e4 LT |
233 | |
234 | if (num_of_bytes < end_of_file) | |
8b94bcb9 | 235 | cFYI(1, ("allocation size less than end of file")); |
5515eff8 AM |
236 | cFYI(1, ("Size %ld and blocks %llu", |
237 | (unsigned long) inode->i_size, | |
238 | (unsigned long long)inode->i_blocks)); | |
70eff55d CH |
239 | |
240 | cifs_set_ops(inode); | |
1da177e4 LT |
241 | } |
242 | return rc; | |
243 | } | |
244 | ||
fb8c4b14 | 245 | static int decode_sfu_inode(struct inode *inode, __u64 size, |
d6e2f2a4 SF |
246 | 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; | |
86c96b4b | 253 | char buf[24]; |
d6e2f2a4 | 254 | unsigned int bytes_read; |
fb8c4b14 | 255 | char *pbuf; |
d6e2f2a4 SF |
256 | |
257 | pbuf = buf; | |
258 | ||
4523cc30 | 259 | if (size == 0) { |
d6e2f2a4 SF |
260 | inode->i_mode |= S_IFIFO; |
261 | return 0; | |
262 | } else if (size < 8) { | |
263 | return -EINVAL; /* EOPNOTSUPP? */ | |
264 | } | |
50c2f753 | 265 | |
d6e2f2a4 SF |
266 | 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); | |
fb8c4b14 | 271 | if (rc == 0) { |
ec637e3f | 272 | int buf_type = CIFS_NO_BUFFER; |
d6e2f2a4 SF |
273 | /* Read header */ |
274 | rc = CIFSSMBRead(xid, pTcon, | |
fb8c4b14 | 275 | netfid, |
86c96b4b | 276 | 24 /* length */, 0 /* offset */, |
ec637e3f | 277 | &bytes_read, &pbuf, &buf_type); |
4523cc30 SF |
278 | if ((rc == 0) && (bytes_read >= 8)) { |
279 | if (memcmp("IntxBLK", pbuf, 8) == 0) { | |
fb8c4b14 | 280 | cFYI(1, ("Block device")); |
3020a1f5 | 281 | inode->i_mode |= S_IFBLK; |
4523cc30 | 282 | if (bytes_read == 24) { |
86c96b4b SF |
283 | /* 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 | } | |
4523cc30 | 290 | } else if (memcmp("IntxCHR", pbuf, 8) == 0) { |
fb8c4b14 | 291 | cFYI(1, ("Char device")); |
3020a1f5 | 292 | inode->i_mode |= S_IFCHR; |
4523cc30 | 293 | if (bytes_read == 24) { |
86c96b4b SF |
294 | /* 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); | |
fb8c4b14 | 300 | } |
4523cc30 | 301 | } else if (memcmp("IntxLNK", pbuf, 7) == 0) { |
fb8c4b14 | 302 | cFYI(1, ("Symlink")); |
3020a1f5 | 303 | inode->i_mode |= S_IFLNK; |
86c96b4b SF |
304 | } else { |
305 | inode->i_mode |= S_IFREG; /* file? */ | |
fb8c4b14 | 306 | rc = -EOPNOTSUPP; |
86c96b4b | 307 | } |
3020a1f5 SF |
308 | } else { |
309 | inode->i_mode |= S_IFREG; /* then it is a file */ | |
fb8c4b14 SF |
310 | rc = -EOPNOTSUPP; /* or some unknown SFU type */ |
311 | } | |
d6e2f2a4 | 312 | CIFSSMBClose(xid, pTcon, netfid); |
d6e2f2a4 SF |
313 | } |
314 | return rc; | |
d6e2f2a4 SF |
315 | } |
316 | ||
9e294f1c SF |
317 | #define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */ |
318 | ||
953f8681 | 319 | static int get_sfu_mode(struct inode *inode, |
9e294f1c SF |
320 | const unsigned char *path, |
321 | struct cifs_sb_info *cifs_sb, int xid) | |
322 | { | |
3020a1f5 | 323 | #ifdef CONFIG_CIFS_XATTR |
9e294f1c SF |
324 | 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, | |
fb8c4b14 | 330 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); |
4523cc30 | 331 | if (rc < 0) |
9e294f1c SF |
332 | return (int)rc; |
333 | else if (rc > 3) { | |
334 | mode = le32_to_cpu(*((__le32 *)ea_value)); | |
fb8c4b14 SF |
335 | inode->i_mode &= ~SFBITS_MASK; |
336 | cFYI(1, ("special bits 0%o org mode 0%o", mode, inode->i_mode)); | |
9e294f1c | 337 | inode->i_mode = (mode & SFBITS_MASK) | inode->i_mode; |
fb8c4b14 | 338 | cFYI(1, ("special mode bits 0%o", mode)); |
9e294f1c SF |
339 | return 0; |
340 | } else { | |
341 | return 0; | |
342 | } | |
3020a1f5 SF |
343 | #else |
344 | return -EOPNOTSUPP; | |
345 | #endif | |
9e294f1c SF |
346 | } |
347 | ||
1da177e4 LT |
348 | int 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; | |
8d6286fd | 358 | int adjustTZ = FALSE; |
1da177e4 LT |
359 | |
360 | pTcon = cifs_sb->tcon; | |
fb8c4b14 | 361 | cFYI(1, ("Getting info on %s", search_path)); |
1da177e4 | 362 | |
d0d2f2df SF |
363 | if ((pfindData == NULL) && (*pinode != NULL)) { |
364 | if (CIFS_I(*pinode)->clientCanCacheRead) { | |
fb8c4b14 | 365 | cFYI(1, ("No need to revalidate cached inode sizes")); |
1da177e4 LT |
366 | return rc; |
367 | } | |
368 | } | |
369 | ||
370 | /* if file info not passed in then get it from server */ | |
d0d2f2df | 371 | if (pfindData == NULL) { |
1da177e4 | 372 | buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); |
d0d2f2df | 373 | if (buf == NULL) |
1da177e4 LT |
374 | 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, | |
acf1a1b1 | 378 | 0 /* not legacy */, |
6b8edfe0 | 379 | cifs_sb->local_nls, cifs_sb->mnt_cifs_flags & |
737b758c | 380 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
6b8edfe0 SF |
381 | /* 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 */ | |
4523cc30 | 384 | if ((rc == -EOPNOTSUPP) || (rc == -EINVAL)) { |
6b8edfe0 | 385 | rc = SMBQueryInformation(xid, pTcon, search_path, |
fb8c4b14 | 386 | pfindData, cifs_sb->local_nls, |
6b8edfe0 SF |
387 | cifs_sb->mnt_cifs_flags & |
388 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
8d6286fd | 389 | adjustTZ = TRUE; |
6b8edfe0 | 390 | } |
1da177e4 LT |
391 | } |
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, | |
fb8c4b14 SF |
410 | cifs_sb->local_nls, |
411 | cifs_sb->mnt_cifs_flags & | |
737b758c | 412 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
1da177e4 LT |
413 | 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); | |
acf1a1b1 SF |
426 | if (*pinode == NULL) { |
427 | kfree(buf); | |
1da177e4 | 428 | return -ENOMEM; |
acf1a1b1 | 429 | } |
1da177e4 LT |
430 | /* 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 | ||
fb8c4b14 | 445 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) { |
1da177e4 LT |
446 | int rc1 = 0; |
447 | __u64 inode_num; | |
448 | ||
fb8c4b14 SF |
449 | rc1 = CIFSGetSrvInodeNumber(xid, pTcon, |
450 | search_path, &inode_num, | |
737b758c SF |
451 | cifs_sb->local_nls, |
452 | cifs_sb->mnt_cifs_flags & | |
453 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
d0d2f2df | 454 | if (rc1) { |
fb8c4b14 | 455 | cFYI(1, ("GetSrvInodeNum rc %d", rc1)); |
1da177e4 LT |
456 | /* 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*/ | |
4523cc30 | 460 | if (sb->s_flags & MS_NOATIME) |
1b2b2126 | 461 | (*pinode)->i_flags |= S_NOATIME | S_NOCMTIME; |
1da177e4 LT |
462 | insert_inode_hash(*pinode); |
463 | } | |
464 | inode = *pinode; | |
465 | cifsInfo = CIFS_I(inode); | |
466 | cifsInfo->cifsAttrs = attr; | |
26a21b98 | 467 | cFYI(1, ("Old time %ld", cifsInfo->time)); |
1da177e4 | 468 | cifsInfo->time = jiffies; |
26a21b98 | 469 | cFYI(1, ("New time %ld", cifsInfo->time)); |
1da177e4 LT |
470 | |
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 | ||
26a21b98 | 476 | /* Linux can not store file creation time so ignore it */ |
4523cc30 | 477 | if (pfindData->LastAccessTime) |
1bd5bbcb SF |
478 | 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; | |
1da177e4 LT |
482 | inode->i_mtime = |
483 | cifs_NTtimeToUnix(le64_to_cpu(pfindData->LastWriteTime)); | |
484 | inode->i_ctime = | |
485 | cifs_NTtimeToUnix(le64_to_cpu(pfindData->ChangeTime)); | |
26a21b98 | 486 | cFYI(0, ("Attributes came in as 0x%x", attr)); |
4523cc30 | 487 | if (adjustTZ && (pTcon->ses) && (pTcon->ses->server)) { |
8d6286fd | 488 | inode->i_ctime.tv_sec += pTcon->ses->server->timeAdj; |
fb8c4b14 | 489 | inode->i_mtime.tv_sec += pTcon->ses->server->timeAdj; |
8d6286fd | 490 | } |
1da177e4 LT |
491 | |
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; | |
3020a1f5 | 496 | else /* since we set the inode type below we need to mask off |
fb8c4b14 SF |
497 | to avoid strange results if type changes and both |
498 | get orred in */ | |
499 | inode->i_mode &= ~S_IFMT; | |
1da177e4 LT |
500 | /* 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; | |
eda3c029 SF |
508 | } 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; | |
d6e2f2a4 SF |
514 | /* 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)) { | |
fb8c4b14 | 517 | if (decode_sfu_inode(inode, |
d6e2f2a4 SF |
518 | le64_to_cpu(pfindData->EndOfFile), |
519 | search_path, | |
ad7a2926 | 520 | cifs_sb, xid)) |
fb8c4b14 | 521 | cFYI(1, ("Unrecognized sfu inode type")); |
ad7a2926 | 522 | |
fb8c4b14 | 523 | cFYI(1, ("sfu mode 0%o", inode->i_mode)); |
1da177e4 LT |
524 | } 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); | |
f5c1e2ea AT |
530 | 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); | |
1da177e4 LT |
536 | /* BB add code here - |
537 | validate if device or weird share or device type? */ | |
538 | } | |
50c2f753 | 539 | |
3677db10 | 540 | spin_lock(&inode->i_lock); |
f6d09982 SF |
541 | if (is_size_safe_to_change(cifsInfo, |
542 | le64_to_cpu(pfindData->EndOfFile))) { | |
7ba52631 | 543 | /* can not safely shrink the file size here if the |
1da177e4 | 544 | client is writing to it due to potential races */ |
fb8c4b14 | 545 | i_size_write(inode, le64_to_cpu(pfindData->EndOfFile)); |
1da177e4 LT |
546 | |
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 | } | |
3677db10 | 552 | spin_unlock(&inode->i_lock); |
1da177e4 LT |
553 | |
554 | inode->i_nlink = le32_to_cpu(pfindData->NumberOfLinks); | |
555 | ||
fb8c4b14 | 556 | /* BB fill in uid and gid here? with help from winbind? |
1da177e4 | 557 | or retrieve from NTFS stream extended attribute */ |
4879b448 | 558 | #ifdef CONFIG_CIFS_EXPERIMENTAL |
953f8681 | 559 | /* fill in 0777 bits from ACL */ |
4879b448 SF |
560 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) { |
561 | cFYI(1, ("Getting mode bits from ACL")); | |
630f3f0c | 562 | acl_to_uid_mode(inode, search_path); |
4879b448 SF |
563 | } |
564 | #endif | |
4523cc30 | 565 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) { |
953f8681 SF |
566 | /* fill in remaining high mode bits e.g. SUID, VTX */ |
567 | get_sfu_mode(inode, search_path, cifs_sb, xid); | |
9e294f1c | 568 | } else if (atomic_read(&cifsInfo->inUse) == 0) { |
1da177e4 LT |
569 | 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 */ | |
fb8c4b14 | 573 | atomic_set(&cifsInfo->inUse, 1); |
1da177e4 LT |
574 | } |
575 | ||
70eff55d | 576 | cifs_set_ops(inode); |
1da177e4 LT |
577 | } |
578 | kfree(buf); | |
579 | return rc; | |
580 | } | |
581 | ||
7f8ed420 SF |
582 | static const struct inode_operations cifs_ipc_inode_ops = { |
583 | .lookup = cifs_lookup, | |
584 | }; | |
585 | ||
1da177e4 | 586 | /* gets root inode */ |
ce634ab2 | 587 | struct inode *cifs_iget(struct super_block *sb, unsigned long ino) |
1da177e4 | 588 | { |
ce634ab2 | 589 | int xid; |
1da177e4 | 590 | struct cifs_sb_info *cifs_sb; |
ce634ab2 DH |
591 | 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; | |
1da177e4 LT |
599 | |
600 | cifs_sb = CIFS_SB(inode->i_sb); | |
601 | xid = GetXid(); | |
c18c842b SF |
602 | |
603 | if (cifs_sb->tcon->unix_ext) | |
7f8ed420 | 604 | rc = cifs_get_inode_info_unix(&inode, "", inode->i_sb, xid); |
1da177e4 | 605 | else |
7f8ed420 SF |
606 | 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; | |
ce634ab2 DH |
615 | _FreeXid(xid); |
616 | iget_failed(inode); | |
617 | return ERR_PTR(rc); | |
7f8ed420 SF |
618 | } |
619 | ||
ce634ab2 DH |
620 | 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 | */ | |
1da177e4 | 625 | _FreeXid(xid); |
ce634ab2 | 626 | return inode; |
1da177e4 LT |
627 | } |
628 | ||
629 | int 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 | ||
06bcfedd | 639 | cFYI(1, ("cifs_unlink, inode = 0x%p", inode)); |
1da177e4 LT |
640 | |
641 | xid = GetXid(); | |
642 | ||
4523cc30 | 643 | if (inode) |
6910ab30 SF |
644 | cifs_sb = CIFS_SB(inode->i_sb); |
645 | else | |
06bcfedd | 646 | cifs_sb = CIFS_SB(direntry->d_sb); |
1da177e4 LT |
647 | 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 */ | |
a11f3a05 | 651 | /* mutex_lock(&direntry->d_sb->s_vfs_rename_mutex);*/ |
7f57356b | 652 | full_path = build_path_from_dentry(direntry); |
a11f3a05 | 653 | /* mutex_unlock(&direntry->d_sb->s_vfs_rename_mutex);*/ |
1da177e4 LT |
654 | if (full_path == NULL) { |
655 | FreeXid(xid); | |
656 | return -ENOMEM; | |
657 | } | |
2d785a50 SF |
658 | |
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, | |
737b758c | 664 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); |
2d785a50 SF |
665 | cFYI(1, ("posix del rc %d", rc)); |
666 | if ((rc == 0) || (rc == -ENOENT)) | |
667 | goto psx_del_no_retry; | |
668 | } | |
1da177e4 | 669 | |
2d785a50 SF |
670 | rc = CIFSSMBDelFile(xid, pTcon, full_path, cifs_sb->local_nls, |
671 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); | |
672 | psx_del_no_retry: | |
1da177e4 | 673 | if (!rc) { |
d0d2f2df | 674 | if (direntry->d_inode) |
9a53c3a7 | 675 | drop_nlink(direntry->d_inode); |
1da177e4 LT |
676 | } 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, | |
737b758c | 684 | &netfid, &oplock, NULL, cifs_sb->local_nls, |
fb8c4b14 | 685 | cifs_sb->mnt_cifs_flags & |
737b758c | 686 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
fb8c4b14 | 687 | if (rc == 0) { |
1da177e4 | 688 | CIFSSMBRenameOpenFile(xid, pTcon, netfid, NULL, |
fb8c4b14 SF |
689 | cifs_sb->local_nls, |
690 | cifs_sb->mnt_cifs_flags & | |
737b758c | 691 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
1da177e4 | 692 | CIFSSMBClose(xid, pTcon, netfid); |
d0d2f2df | 693 | if (direntry->d_inode) |
9a53c3a7 | 694 | drop_nlink(direntry->d_inode); |
1da177e4 LT |
695 | } |
696 | } else if (rc == -EACCES) { | |
697 | /* try only if r/o attribute set in local lookup data? */ | |
a048d7a8 | 698 | pinfo_buf = kzalloc(sizeof(FILE_BASIC_INFO), GFP_KERNEL); |
1da177e4 | 699 | if (pinfo_buf) { |
1da177e4 LT |
700 | /* 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, | |
737b758c | 705 | cifs_sb->local_nls, |
fb8c4b14 | 706 | cifs_sb->mnt_cifs_flags & |
737b758c | 707 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
1da177e4 LT |
708 | 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, | |
fb8c4b14 | 717 | cifs_sb->local_nls); |
1da177e4 LT |
718 | 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, | |
737b758c | 729 | cifs_sb->local_nls, |
fb8c4b14 | 730 | cifs_sb->mnt_cifs_flags & |
737b758c | 731 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
fb8c4b14 | 732 | if (rc == 0) { |
1da177e4 LT |
733 | rc = CIFSSMBSetFileTimes(xid, pTcon, |
734 | pinfo_buf, | |
735 | netfid); | |
736 | CIFSSMBClose(xid, pTcon, netfid); | |
737 | } | |
738 | } | |
739 | kfree(pinfo_buf); | |
740 | } | |
fb8c4b14 SF |
741 | if (rc == 0) { |
742 | rc = CIFSSMBDelFile(xid, pTcon, full_path, | |
743 | cifs_sb->local_nls, | |
744 | cifs_sb->mnt_cifs_flags & | |
737b758c | 745 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
1da177e4 | 746 | if (!rc) { |
d0d2f2df | 747 | if (direntry->d_inode) |
9a53c3a7 | 748 | drop_nlink(direntry->d_inode); |
1da177e4 LT |
749 | } 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, | |
fb8c4b14 SF |
758 | cifs_sb->local_nls, |
759 | cifs_sb->mnt_cifs_flags & | |
737b758c | 760 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
fb8c4b14 | 761 | if (rc == 0) { |
1da177e4 LT |
762 | CIFSSMBRenameOpenFile(xid, pTcon, |
763 | netfid, NULL, | |
737b758c SF |
764 | cifs_sb->local_nls, |
765 | cifs_sb->mnt_cifs_flags & | |
766 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 | 767 | CIFSSMBClose(xid, pTcon, netfid); |
d0d2f2df | 768 | if (direntry->d_inode) |
9a53c3a7 | 769 | drop_nlink(direntry->d_inode); |
1da177e4 LT |
770 | } |
771 | /* BB if rc = -ETXTBUSY goto the rename logic BB */ | |
772 | } | |
773 | } | |
774 | } | |
d0d2f2df | 775 | if (direntry->d_inode) { |
b2aeb9d5 SF |
776 | 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 | } | |
4523cc30 | 781 | if (inode) { |
06bcfedd SF |
782 | 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 | } | |
1da177e4 LT |
786 | |
787 | kfree(full_path); | |
788 | FreeXid(xid); | |
789 | return rc; | |
790 | } | |
791 | ||
2dd29d31 | 792 | static void posix_fill_in_inode(struct inode *tmp_inode, |
0b442d2c | 793 | FILE_UNIX_BASIC_INFO *pData, int isNewInode) |
2dd29d31 | 794 | { |
75f12983 | 795 | struct cifsInodeInfo *cifsInfo = CIFS_I(tmp_inode); |
2dd29d31 SF |
796 | loff_t local_size; |
797 | struct timespec local_mtime; | |
798 | ||
2dd29d31 SF |
799 | 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 | ||
75f12983 CH |
806 | cifs_unix_info_to_inode(tmp_inode, pData, 1); |
807 | cifs_set_ops(tmp_inode); | |
50c2f753 | 808 | |
75f12983 CH |
809 | if (!S_ISREG(tmp_inode->i_mode)) |
810 | return; | |
2dd29d31 | 811 | |
75f12983 CH |
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; | |
2dd29d31 | 819 | |
75f12983 CH |
820 | if (timespec_equal(&tmp_inode->i_mtime, &local_mtime) && |
821 | (local_size == tmp_inode->i_size)) { | |
822 | cFYI(1, ("inode exists but unchanged")); | |
2dd29d31 | 823 | } else { |
75f12983 CH |
824 | /* file may have changed on server */ |
825 | cFYI(1, ("invalidate inode, readdir detected change")); | |
826 | invalidate_remote_inode(tmp_inode); | |
fb8c4b14 | 827 | } |
2dd29d31 SF |
828 | } |
829 | ||
1da177e4 LT |
830 | int 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 | ||
6473a559 | 839 | cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p", mode, inode)); |
1da177e4 LT |
840 | |
841 | xid = GetXid(); | |
842 | ||
843 | cifs_sb = CIFS_SB(inode->i_sb); | |
844 | pTcon = cifs_sb->tcon; | |
845 | ||
7f57356b | 846 | full_path = build_path_from_dentry(direntry); |
1da177e4 LT |
847 | if (full_path == NULL) { |
848 | FreeXid(xid); | |
849 | return -ENOMEM; | |
850 | } | |
50c2f753 | 851 | |
fb8c4b14 SF |
852 | if ((pTcon->ses->capabilities & CAP_UNIX) && |
853 | (CIFS_UNIX_POSIX_PATH_OPS_CAP & | |
2dd29d31 SF |
854 | le64_to_cpu(pTcon->fsUnixInfo.Capability))) { |
855 | u32 oplock = 0; | |
f6d09982 | 856 | FILE_UNIX_BASIC_INFO *pInfo = |
2dd29d31 | 857 | kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL); |
fb8c4b14 | 858 | if (pInfo == NULL) { |
2dd29d31 SF |
859 | rc = -ENOMEM; |
860 | goto mkdir_out; | |
861 | } | |
50c2f753 | 862 | |
a8cd925f | 863 | mode &= ~current->fs->umask; |
2dd29d31 SF |
864 | rc = CIFSPOSIXCreate(xid, pTcon, SMB_O_DIRECTORY | SMB_O_CREAT, |
865 | mode, NULL /* netfid */, pInfo, &oplock, | |
fb8c4b14 SF |
866 | full_path, cifs_sb->local_nls, |
867 | cifs_sb->mnt_cifs_flags & | |
2dd29d31 | 868 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
c45d707f SF |
869 | if (rc == -EOPNOTSUPP) { |
870 | kfree(pInfo); | |
871 | goto mkdir_retry_old; | |
872 | } else if (rc) { | |
2dd29d31 SF |
873 | cFYI(1, ("posix mkdir returned 0x%x", rc)); |
874 | d_drop(direntry); | |
875 | } else { | |
8f2376ad CG |
876 | if (pInfo->Type == cpu_to_le32(-1)) { |
877 | /* no return info, go query for it */ | |
5a07cdf8 | 878 | kfree(pInfo); |
fb8c4b14 | 879 | goto mkdir_get_info; |
5a07cdf8 | 880 | } |
fb8c4b14 SF |
881 | /*BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if need |
882 | to set uid/gid */ | |
2dd29d31 SF |
883 | inc_nlink(inode); |
884 | if (pTcon->nocase) | |
885 | direntry->d_op = &cifs_ci_dentry_ops; | |
886 | else | |
887 | direntry->d_op = &cifs_dentry_ops; | |
cbac3cba SF |
888 | |
889 | newinode = new_inode(inode->i_sb); | |
5a07cdf8 SF |
890 | if (newinode == NULL) { |
891 | kfree(pInfo); | |
cbac3cba | 892 | goto mkdir_get_info; |
5a07cdf8 | 893 | } |
cbac3cba SF |
894 | /* Is an i_ino of zero legal? */ |
895 | /* Are there sanity checks we can use to ensure that | |
896 | the server is really filling in that field? */ | |
897 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) { | |
898 | newinode->i_ino = | |
899 | (unsigned long)pInfo->UniqueId; | |
900 | } /* note ino incremented to unique num in new_inode */ | |
fb8c4b14 | 901 | if (inode->i_sb->s_flags & MS_NOATIME) |
cbac3cba SF |
902 | newinode->i_flags |= S_NOATIME | S_NOCMTIME; |
903 | newinode->i_nlink = 2; | |
904 | ||
905 | insert_inode_hash(newinode); | |
2dd29d31 | 906 | d_instantiate(direntry, newinode); |
cbac3cba SF |
907 | |
908 | /* we already checked in POSIXCreate whether | |
909 | frame was long enough */ | |
910 | posix_fill_in_inode(direntry->d_inode, | |
0b442d2c | 911 | pInfo, 1 /* NewInode */); |
cbac3cba | 912 | #ifdef CONFIG_CIFS_DEBUG2 |
fb8c4b14 | 913 | cFYI(1, ("instantiated dentry %p %s to inode %p", |
cbac3cba SF |
914 | direntry, direntry->d_name.name, newinode)); |
915 | ||
fb8c4b14 SF |
916 | if (newinode->i_nlink != 2) |
917 | cFYI(1, ("unexpected number of links %d", | |
cbac3cba SF |
918 | newinode->i_nlink)); |
919 | #endif | |
2dd29d31 SF |
920 | } |
921 | kfree(pInfo); | |
922 | goto mkdir_out; | |
fb8c4b14 | 923 | } |
c45d707f | 924 | mkdir_retry_old: |
1da177e4 | 925 | /* BB add setting the equivalent of mode via CreateX w/ACLs */ |
737b758c SF |
926 | rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls, |
927 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 | 928 | if (rc) { |
26a21b98 | 929 | cFYI(1, ("cifs_mkdir returned 0x%x", rc)); |
1da177e4 LT |
930 | d_drop(direntry); |
931 | } else { | |
fb8c4b14 | 932 | mkdir_get_info: |
d8c76e6f | 933 | inc_nlink(inode); |
c18c842b | 934 | if (pTcon->unix_ext) |
1da177e4 | 935 | rc = cifs_get_inode_info_unix(&newinode, full_path, |
fb8c4b14 | 936 | inode->i_sb, xid); |
1da177e4 LT |
937 | else |
938 | rc = cifs_get_inode_info(&newinode, full_path, NULL, | |
fb8c4b14 | 939 | inode->i_sb, xid); |
1da177e4 | 940 | |
b92327fe SF |
941 | if (pTcon->nocase) |
942 | direntry->d_op = &cifs_ci_dentry_ops; | |
943 | else | |
944 | direntry->d_op = &cifs_dentry_ops; | |
1da177e4 | 945 | d_instantiate(direntry, newinode); |
2dd29d31 | 946 | /* setting nlink not necessary except in cases where we |
fb8c4b14 | 947 | * failed to get it from the server or was set bogus */ |
2dd29d31 | 948 | if ((direntry->d_inode) && (direntry->d_inode->i_nlink < 2)) |
fb8c4b14 | 949 | direntry->d_inode->i_nlink = 2; |
c18c842b | 950 | if (pTcon->unix_ext) { |
3ce53fc4 | 951 | mode &= ~current->fs->umask; |
d0d2f2df | 952 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) { |
1da177e4 LT |
953 | CIFSSMBUnixSetPerms(xid, pTcon, full_path, |
954 | mode, | |
83451879 SF |
955 | (__u64)current->fsuid, |
956 | (__u64)current->fsgid, | |
1da177e4 | 957 | 0 /* dev_t */, |
737b758c SF |
958 | cifs_sb->local_nls, |
959 | cifs_sb->mnt_cifs_flags & | |
960 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
961 | } else { |
962 | CIFSSMBUnixSetPerms(xid, pTcon, full_path, | |
963 | mode, (__u64)-1, | |
964 | (__u64)-1, 0 /* dev_t */, | |
737b758c | 965 | cifs_sb->local_nls, |
fb8c4b14 | 966 | cifs_sb->mnt_cifs_flags & |
737b758c | 967 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
1da177e4 | 968 | } |
3ce53fc4 | 969 | } else { |
1da177e4 LT |
970 | /* BB to be implemented via Windows secrty descriptors |
971 | eg CIFSSMBWinSetPerms(xid, pTcon, full_path, mode, | |
972 | -1, -1, local_nls); */ | |
fb8c4b14 | 973 | if (direntry->d_inode) { |
6473a559 | 974 | direntry->d_inode->i_mode = mode; |
25741b3e | 975 | direntry->d_inode->i_mode |= S_IFDIR; |
fb8c4b14 | 976 | if (cifs_sb->mnt_cifs_flags & |
6473a559 | 977 | CIFS_MOUNT_SET_UID) { |
fb8c4b14 | 978 | direntry->d_inode->i_uid = |
6473a559 | 979 | current->fsuid; |
fb8c4b14 | 980 | direntry->d_inode->i_gid = |
6473a559 SF |
981 | current->fsgid; |
982 | } | |
983 | } | |
2a138ebb | 984 | } |
1da177e4 | 985 | } |
fb8c4b14 | 986 | mkdir_out: |
1da177e4 LT |
987 | kfree(full_path); |
988 | FreeXid(xid); | |
989 | return rc; | |
990 | } | |
991 | ||
992 | int cifs_rmdir(struct inode *inode, struct dentry *direntry) | |
993 | { | |
994 | int rc = 0; | |
995 | int xid; | |
996 | struct cifs_sb_info *cifs_sb; | |
997 | struct cifsTconInfo *pTcon; | |
998 | char *full_path = NULL; | |
999 | struct cifsInodeInfo *cifsInode; | |
1000 | ||
26a21b98 | 1001 | cFYI(1, ("cifs_rmdir, inode = 0x%p", inode)); |
1da177e4 LT |
1002 | |
1003 | xid = GetXid(); | |
1004 | ||
1005 | cifs_sb = CIFS_SB(inode->i_sb); | |
1006 | pTcon = cifs_sb->tcon; | |
1007 | ||
7f57356b | 1008 | full_path = build_path_from_dentry(direntry); |
1da177e4 LT |
1009 | if (full_path == NULL) { |
1010 | FreeXid(xid); | |
1011 | return -ENOMEM; | |
1012 | } | |
1013 | ||
737b758c SF |
1014 | rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls, |
1015 | cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
1016 | |
1017 | if (!rc) { | |
9a53c3a7 | 1018 | drop_nlink(inode); |
3677db10 | 1019 | spin_lock(&direntry->d_inode->i_lock); |
fb8c4b14 | 1020 | i_size_write(direntry->d_inode, 0); |
ce71ec36 | 1021 | clear_nlink(direntry->d_inode); |
3677db10 | 1022 | spin_unlock(&direntry->d_inode->i_lock); |
1da177e4 LT |
1023 | } |
1024 | ||
1025 | cifsInode = CIFS_I(direntry->d_inode); | |
1026 | cifsInode->time = 0; /* force revalidate to go get info when | |
1027 | needed */ | |
1028 | direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime = | |
1029 | current_fs_time(inode->i_sb); | |
1030 | ||
1031 | kfree(full_path); | |
1032 | FreeXid(xid); | |
1033 | return rc; | |
1034 | } | |
1035 | ||
1036 | int cifs_rename(struct inode *source_inode, struct dentry *source_direntry, | |
1037 | struct inode *target_inode, struct dentry *target_direntry) | |
1038 | { | |
1039 | char *fromName; | |
1040 | char *toName; | |
1041 | struct cifs_sb_info *cifs_sb_source; | |
1042 | struct cifs_sb_info *cifs_sb_target; | |
1043 | struct cifsTconInfo *pTcon; | |
1044 | int xid; | |
1045 | int rc = 0; | |
1046 | ||
1047 | xid = GetXid(); | |
1048 | ||
1049 | cifs_sb_target = CIFS_SB(target_inode->i_sb); | |
1050 | cifs_sb_source = CIFS_SB(source_inode->i_sb); | |
1051 | pTcon = cifs_sb_source->tcon; | |
1052 | ||
1053 | if (pTcon != cifs_sb_target->tcon) { | |
1054 | FreeXid(xid); | |
1055 | return -EXDEV; /* BB actually could be allowed if same server, | |
1056 | but different share. | |
1057 | Might eventually add support for this */ | |
1058 | } | |
1059 | ||
1060 | /* we already have the rename sem so we do not need to grab it again | |
1061 | here to protect the path integrity */ | |
7f57356b SF |
1062 | fromName = build_path_from_dentry(source_direntry); |
1063 | toName = build_path_from_dentry(target_direntry); | |
1da177e4 LT |
1064 | if ((fromName == NULL) || (toName == NULL)) { |
1065 | rc = -ENOMEM; | |
1066 | goto cifs_rename_exit; | |
1067 | } | |
1068 | ||
1069 | rc = CIFSSMBRename(xid, pTcon, fromName, toName, | |
737b758c SF |
1070 | cifs_sb_source->local_nls, |
1071 | cifs_sb_source->mnt_cifs_flags & | |
1072 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
1073 | if (rc == -EEXIST) { |
1074 | /* check if they are the same file because rename of hardlinked | |
1075 | files is a noop */ | |
1076 | FILE_UNIX_BASIC_INFO *info_buf_source; | |
1077 | FILE_UNIX_BASIC_INFO *info_buf_target; | |
1078 | ||
1079 | info_buf_source = | |
1080 | kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL); | |
1081 | if (info_buf_source != NULL) { | |
1082 | info_buf_target = info_buf_source + 1; | |
c18c842b | 1083 | if (pTcon->unix_ext) |
8e87d4dc | 1084 | rc = CIFSSMBUnixQPathInfo(xid, pTcon, fromName, |
fb8c4b14 | 1085 | info_buf_source, |
8e87d4dc SF |
1086 | cifs_sb_source->local_nls, |
1087 | cifs_sb_source->mnt_cifs_flags & | |
1088 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1089 | /* else rc is still EEXIST so will fall through to | |
1090 | unlink the target and retry rename */ | |
1da177e4 LT |
1091 | if (rc == 0) { |
1092 | rc = CIFSSMBUnixQPathInfo(xid, pTcon, toName, | |
1093 | info_buf_target, | |
737b758c SF |
1094 | cifs_sb_target->local_nls, |
1095 | /* remap based on source sb */ | |
1096 | cifs_sb_source->mnt_cifs_flags & | |
1097 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
1098 | } |
1099 | if ((rc == 0) && | |
1100 | (info_buf_source->UniqueId == | |
1101 | info_buf_target->UniqueId)) { | |
1102 | /* do not rename since the files are hardlinked which | |
1103 | is a noop */ | |
1104 | } else { | |
1105 | /* we either can not tell the files are hardlinked | |
1106 | (as with Windows servers) or files are not | |
1107 | hardlinked so delete the target manually before | |
1108 | renaming to follow POSIX rather than Windows | |
1109 | semantics */ | |
1110 | cifs_unlink(target_inode, target_direntry); | |
1111 | rc = CIFSSMBRename(xid, pTcon, fromName, | |
1112 | toName, | |
737b758c SF |
1113 | cifs_sb_source->local_nls, |
1114 | cifs_sb_source->mnt_cifs_flags | |
1115 | & CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
1116 | } |
1117 | kfree(info_buf_source); | |
1118 | } /* if we can not get memory just leave rc as EEXIST */ | |
1119 | } | |
1120 | ||
ad7a2926 | 1121 | if (rc) |
1da177e4 | 1122 | cFYI(1, ("rename rc %d", rc)); |
1da177e4 LT |
1123 | |
1124 | if ((rc == -EIO) || (rc == -EEXIST)) { | |
1125 | int oplock = FALSE; | |
1126 | __u16 netfid; | |
1127 | ||
1128 | /* BB FIXME Is Generic Read correct for rename? */ | |
1129 | /* if renaming directory - we should not say CREATE_NOT_DIR, | |
1130 | need to test renaming open directory, also GENERIC_READ | |
1131 | might not right be right access to request */ | |
1132 | rc = CIFSSMBOpen(xid, pTcon, fromName, FILE_OPEN, GENERIC_READ, | |
1133 | CREATE_NOT_DIR, &netfid, &oplock, NULL, | |
fb8c4b14 SF |
1134 | cifs_sb_source->local_nls, |
1135 | cifs_sb_source->mnt_cifs_flags & | |
737b758c | 1136 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
fb8c4b14 | 1137 | if (rc == 0) { |
8e87d4dc | 1138 | rc = CIFSSMBRenameOpenFile(xid, pTcon, netfid, toName, |
fb8c4b14 | 1139 | cifs_sb_source->local_nls, |
737b758c SF |
1140 | cifs_sb_source->mnt_cifs_flags & |
1141 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
1142 | CIFSSMBClose(xid, pTcon, netfid); |
1143 | } | |
1144 | } | |
1145 | ||
1146 | cifs_rename_exit: | |
1147 | kfree(fromName); | |
1148 | kfree(toName); | |
1149 | FreeXid(xid); | |
1150 | return rc; | |
1151 | } | |
1152 | ||
1153 | int cifs_revalidate(struct dentry *direntry) | |
1154 | { | |
1155 | int xid; | |
cea21805 | 1156 | int rc = 0, wbrc = 0; |
1da177e4 LT |
1157 | char *full_path; |
1158 | struct cifs_sb_info *cifs_sb; | |
1159 | struct cifsInodeInfo *cifsInode; | |
1160 | loff_t local_size; | |
1161 | struct timespec local_mtime; | |
1162 | int invalidate_inode = FALSE; | |
1163 | ||
1164 | if (direntry->d_inode == NULL) | |
1165 | return -ENOENT; | |
1166 | ||
1167 | cifsInode = CIFS_I(direntry->d_inode); | |
1168 | ||
1169 | if (cifsInode == NULL) | |
1170 | return -ENOENT; | |
1171 | ||
1172 | /* no sense revalidating inode info on file that no one can write */ | |
1173 | if (CIFS_I(direntry->d_inode)->clientCanCacheRead) | |
1174 | return rc; | |
1175 | ||
1176 | xid = GetXid(); | |
1177 | ||
1178 | cifs_sb = CIFS_SB(direntry->d_sb); | |
1179 | ||
1180 | /* can not safely grab the rename sem here if rename calls revalidate | |
1181 | since that would deadlock */ | |
7f57356b | 1182 | full_path = build_path_from_dentry(direntry); |
1da177e4 LT |
1183 | if (full_path == NULL) { |
1184 | FreeXid(xid); | |
1185 | return -ENOMEM; | |
1186 | } | |
1187 | cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld " | |
1188 | "jiffies %ld", full_path, direntry->d_inode, | |
1189 | direntry->d_inode->i_count.counter, direntry, | |
1190 | direntry->d_time, jiffies)); | |
1191 | ||
1192 | if (cifsInode->time == 0) { | |
1193 | /* was set to zero previously to force revalidate */ | |
1194 | } else if (time_before(jiffies, cifsInode->time + HZ) && | |
1195 | lookupCacheEnabled) { | |
1196 | if ((S_ISREG(direntry->d_inode->i_mode) == 0) || | |
1197 | (direntry->d_inode->i_nlink == 1)) { | |
1198 | kfree(full_path); | |
1199 | FreeXid(xid); | |
1200 | return rc; | |
1201 | } else { | |
1202 | cFYI(1, ("Have to revalidate file due to hardlinks")); | |
1203 | } | |
1204 | } | |
1205 | ||
1206 | /* save mtime and size */ | |
1207 | local_mtime = direntry->d_inode->i_mtime; | |
1208 | local_size = direntry->d_inode->i_size; | |
1209 | ||
c18c842b | 1210 | if (cifs_sb->tcon->unix_ext) { |
1da177e4 | 1211 | rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path, |
fb8c4b14 | 1212 | direntry->d_sb, xid); |
1da177e4 LT |
1213 | if (rc) { |
1214 | cFYI(1, ("error on getting revalidate info %d", rc)); | |
1215 | /* if (rc != -ENOENT) | |
1216 | rc = 0; */ /* BB should we cache info on | |
1217 | certain errors? */ | |
1218 | } | |
1219 | } else { | |
1220 | rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL, | |
fb8c4b14 | 1221 | direntry->d_sb, xid); |
1da177e4 LT |
1222 | if (rc) { |
1223 | cFYI(1, ("error on getting revalidate info %d", rc)); | |
1224 | /* if (rc != -ENOENT) | |
1225 | rc = 0; */ /* BB should we cache info on | |
1226 | certain errors? */ | |
1227 | } | |
1228 | } | |
1229 | /* should we remap certain errors, access denied?, to zero */ | |
1230 | ||
1231 | /* if not oplocked, we invalidate inode pages if mtime or file size | |
1232 | had changed on server */ | |
1233 | ||
fb8c4b14 | 1234 | if (timespec_equal(&local_mtime, &direntry->d_inode->i_mtime) && |
1da177e4 LT |
1235 | (local_size == direntry->d_inode->i_size)) { |
1236 | cFYI(1, ("cifs_revalidate - inode unchanged")); | |
1237 | } else { | |
1238 | /* file may have changed on server */ | |
1239 | if (cifsInode->clientCanCacheRead) { | |
1240 | /* no need to invalidate inode pages since we were the | |
1241 | only ones who could have modified the file and the | |
1242 | server copy is staler than ours */ | |
1243 | } else { | |
1244 | invalidate_inode = TRUE; | |
1245 | } | |
1246 | } | |
1247 | ||
1248 | /* can not grab this sem since kernel filesys locking documentation | |
1b1dcc1b JS |
1249 | indicates i_mutex may be taken by the kernel on lookup and rename |
1250 | which could deadlock if we grab the i_mutex here as well */ | |
1251 | /* mutex_lock(&direntry->d_inode->i_mutex);*/ | |
1da177e4 LT |
1252 | /* need to write out dirty pages here */ |
1253 | if (direntry->d_inode->i_mapping) { | |
1254 | /* do we need to lock inode until after invalidate completes | |
1255 | below? */ | |
cea21805 JL |
1256 | wbrc = filemap_fdatawrite(direntry->d_inode->i_mapping); |
1257 | if (wbrc) | |
1258 | CIFS_I(direntry->d_inode)->write_behind_rc = wbrc; | |
1da177e4 LT |
1259 | } |
1260 | if (invalidate_inode) { | |
3abb9272 SF |
1261 | /* shrink_dcache not necessary now that cifs dentry ops |
1262 | are exported for negative dentries */ | |
fb8c4b14 | 1263 | /* if (S_ISDIR(direntry->d_inode->i_mode)) |
3abb9272 SF |
1264 | shrink_dcache_parent(direntry); */ |
1265 | if (S_ISREG(direntry->d_inode->i_mode)) { | |
1266 | if (direntry->d_inode->i_mapping) | |
cea21805 JL |
1267 | wbrc = filemap_fdatawait(direntry->d_inode->i_mapping); |
1268 | if (wbrc) | |
1269 | CIFS_I(direntry->d_inode)->write_behind_rc = wbrc; | |
3abb9272 SF |
1270 | /* may eventually have to do this for open files too */ |
1271 | if (list_empty(&(cifsInode->openFileList))) { | |
1272 | /* changed on server - flush read ahead pages */ | |
1273 | cFYI(1, ("Invalidating read ahead data on " | |
1274 | "closed file")); | |
1275 | invalidate_remote_inode(direntry->d_inode); | |
1276 | } | |
1da177e4 LT |
1277 | } |
1278 | } | |
1b1dcc1b | 1279 | /* mutex_unlock(&direntry->d_inode->i_mutex); */ |
50c2f753 | 1280 | |
1da177e4 LT |
1281 | kfree(full_path); |
1282 | FreeXid(xid); | |
1283 | return rc; | |
1284 | } | |
1285 | ||
1286 | int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry, | |
1287 | struct kstat *stat) | |
1288 | { | |
1289 | int err = cifs_revalidate(dentry); | |
5fe14c85 | 1290 | if (!err) { |
1da177e4 | 1291 | generic_fillattr(dentry->d_inode, stat); |
5fe14c85 SF |
1292 | stat->blksize = CIFS_MAX_MSGSIZE; |
1293 | } | |
1da177e4 LT |
1294 | return err; |
1295 | } | |
1296 | ||
1297 | static int cifs_truncate_page(struct address_space *mapping, loff_t from) | |
1298 | { | |
1299 | pgoff_t index = from >> PAGE_CACHE_SHIFT; | |
1300 | unsigned offset = from & (PAGE_CACHE_SIZE - 1); | |
1301 | struct page *page; | |
1da177e4 LT |
1302 | int rc = 0; |
1303 | ||
1304 | page = grab_cache_page(mapping, index); | |
1305 | if (!page) | |
1306 | return -ENOMEM; | |
1307 | ||
eebd2aa3 | 1308 | zero_user_segment(page, offset, PAGE_CACHE_SIZE); |
1da177e4 LT |
1309 | unlock_page(page); |
1310 | page_cache_release(page); | |
1311 | return rc; | |
1312 | } | |
1313 | ||
fb8c4b14 | 1314 | static int cifs_vmtruncate(struct inode *inode, loff_t offset) |
3677db10 SF |
1315 | { |
1316 | struct address_space *mapping = inode->i_mapping; | |
1317 | unsigned long limit; | |
1318 | ||
ba6a46a0 | 1319 | spin_lock(&inode->i_lock); |
3677db10 SF |
1320 | if (inode->i_size < offset) |
1321 | goto do_expand; | |
1322 | /* | |
1323 | * truncation of in-use swapfiles is disallowed - it would cause | |
1324 | * subsequent swapout to scribble on the now-freed blocks. | |
1325 | */ | |
ba6a46a0 SF |
1326 | if (IS_SWAPFILE(inode)) { |
1327 | spin_unlock(&inode->i_lock); | |
3677db10 | 1328 | goto out_busy; |
ba6a46a0 | 1329 | } |
3677db10 SF |
1330 | i_size_write(inode, offset); |
1331 | spin_unlock(&inode->i_lock); | |
8064ab4d SF |
1332 | /* |
1333 | * unmap_mapping_range is called twice, first simply for efficiency | |
1334 | * so that truncate_inode_pages does fewer single-page unmaps. However | |
1335 | * after this first call, and before truncate_inode_pages finishes, | |
1336 | * it is possible for private pages to be COWed, which remain after | |
1337 | * truncate_inode_pages finishes, hence the second unmap_mapping_range | |
1338 | * call must be made for correctness. | |
1339 | */ | |
3677db10 SF |
1340 | unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1); |
1341 | truncate_inode_pages(mapping, offset); | |
8064ab4d | 1342 | unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1); |
3677db10 SF |
1343 | goto out_truncate; |
1344 | ||
1345 | do_expand: | |
1346 | limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; | |
ba6a46a0 SF |
1347 | if (limit != RLIM_INFINITY && offset > limit) { |
1348 | spin_unlock(&inode->i_lock); | |
3677db10 | 1349 | goto out_sig; |
ba6a46a0 SF |
1350 | } |
1351 | if (offset > inode->i_sb->s_maxbytes) { | |
1352 | spin_unlock(&inode->i_lock); | |
3677db10 | 1353 | goto out_big; |
ba6a46a0 | 1354 | } |
3677db10 | 1355 | i_size_write(inode, offset); |
ba6a46a0 | 1356 | spin_unlock(&inode->i_lock); |
3677db10 SF |
1357 | out_truncate: |
1358 | if (inode->i_op && inode->i_op->truncate) | |
1359 | inode->i_op->truncate(inode); | |
1360 | return 0; | |
1361 | out_sig: | |
1362 | send_sig(SIGXFSZ, current, 0); | |
1363 | out_big: | |
1364 | return -EFBIG; | |
1365 | out_busy: | |
1366 | return -ETXTBSY; | |
1367 | } | |
1368 | ||
1da177e4 LT |
1369 | int cifs_setattr(struct dentry *direntry, struct iattr *attrs) |
1370 | { | |
1371 | int xid; | |
1372 | struct cifs_sb_info *cifs_sb; | |
1373 | struct cifsTconInfo *pTcon; | |
1374 | char *full_path = NULL; | |
1375 | int rc = -EACCES; | |
1da177e4 LT |
1376 | struct cifsFileInfo *open_file = NULL; |
1377 | FILE_BASIC_INFO time_buf; | |
1378 | int set_time = FALSE; | |
066fcb06 | 1379 | int set_dosattr = FALSE; |
1da177e4 LT |
1380 | __u64 mode = 0xFFFFFFFFFFFFFFFFULL; |
1381 | __u64 uid = 0xFFFFFFFFFFFFFFFFULL; | |
1382 | __u64 gid = 0xFFFFFFFFFFFFFFFFULL; | |
1383 | struct cifsInodeInfo *cifsInode; | |
1da177e4 LT |
1384 | |
1385 | xid = GetXid(); | |
1386 | ||
3979877e | 1387 | cFYI(1, ("setattr on file %s attrs->iavalid 0x%x", |
1da177e4 | 1388 | direntry->d_name.name, attrs->ia_valid)); |
6473a559 | 1389 | |
1da177e4 LT |
1390 | cifs_sb = CIFS_SB(direntry->d_inode->i_sb); |
1391 | pTcon = cifs_sb->tcon; | |
1392 | ||
2a138ebb | 1393 | if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) { |
6473a559 SF |
1394 | /* check if we have permission to change attrs */ |
1395 | rc = inode_change_ok(direntry->d_inode, attrs); | |
fb8c4b14 | 1396 | if (rc < 0) { |
6473a559 SF |
1397 | FreeXid(xid); |
1398 | return rc; | |
1399 | } else | |
1400 | rc = 0; | |
1401 | } | |
50c2f753 | 1402 | |
7f57356b | 1403 | full_path = build_path_from_dentry(direntry); |
1da177e4 LT |
1404 | if (full_path == NULL) { |
1405 | FreeXid(xid); | |
1406 | return -ENOMEM; | |
1407 | } | |
1408 | cifsInode = CIFS_I(direntry->d_inode); | |
1409 | ||
1410 | /* BB check if we need to refresh inode from server now ? BB */ | |
1411 | ||
1da177e4 | 1412 | if (attrs->ia_valid & ATTR_SIZE) { |
cea21805 JL |
1413 | /* |
1414 | Flush data before changing file size on server. If the | |
1415 | flush returns error, store it to report later and continue. | |
1416 | BB: This should be smarter. Why bother flushing pages that | |
1417 | will be truncated anyway? Also, should we error out here if | |
1418 | the flush returns error? | |
1419 | */ | |
1420 | rc = filemap_write_and_wait(direntry->d_inode->i_mapping); | |
1421 | if (rc != 0) { | |
1422 | CIFS_I(direntry->d_inode)->write_behind_rc = rc; | |
1423 | rc = 0; | |
1424 | } | |
1425 | ||
1da177e4 LT |
1426 | /* To avoid spurious oplock breaks from server, in the case of |
1427 | inodes that we already have open, avoid doing path based | |
1428 | setting of file size if we can do it by handle. | |
1429 | This keeps our caching token (oplock) and avoids timeouts | |
1430 | when the local oplock break takes longer to flush | |
1431 | writebehind data than the SMB timeout for the SetPathInfo | |
1432 | request would allow */ | |
3979877e | 1433 | |
6148a742 SF |
1434 | open_file = find_writable_file(cifsInode); |
1435 | if (open_file) { | |
1436 | __u16 nfid = open_file->netfid; | |
1437 | __u32 npid = open_file->pid; | |
1438 | rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size, | |
1439 | nfid, npid, FALSE); | |
23e7dd7d | 1440 | atomic_dec(&open_file->wrtPending); |
fb8c4b14 SF |
1441 | cFYI(1, ("SetFSize for attrs rc = %d", rc)); |
1442 | if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) { | |
77159b4d | 1443 | unsigned int bytes_written; |
6148a742 SF |
1444 | rc = CIFSSMBWrite(xid, pTcon, |
1445 | nfid, 0, attrs->ia_size, | |
1446 | &bytes_written, NULL, NULL, | |
1447 | 1 /* 45 seconds */); | |
fb8c4b14 | 1448 | cFYI(1, ("Wrt seteof rc %d", rc)); |
1da177e4 | 1449 | } |
fb8c4b14 | 1450 | } else |
6473a559 SF |
1451 | rc = -EINVAL; |
1452 | ||
1da177e4 LT |
1453 | if (rc != 0) { |
1454 | /* Set file size by pathname rather than by handle | |
1455 | either because no valid, writeable file handle for | |
1456 | it was found or because there was an error setting | |
1457 | it by handle */ | |
1458 | rc = CIFSSMBSetEOF(xid, pTcon, full_path, | |
1459 | attrs->ia_size, FALSE, | |
fb8c4b14 | 1460 | cifs_sb->local_nls, |
737b758c SF |
1461 | cifs_sb->mnt_cifs_flags & |
1462 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
e30dcf3a | 1463 | cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc)); |
fb8c4b14 | 1464 | if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) { |
e30dcf3a SF |
1465 | __u16 netfid; |
1466 | int oplock = FALSE; | |
1467 | ||
1468 | rc = SMBLegacyOpen(xid, pTcon, full_path, | |
1469 | FILE_OPEN, | |
1470 | SYNCHRONIZE | FILE_WRITE_ATTRIBUTES, | |
1471 | CREATE_NOT_DIR, &netfid, &oplock, | |
1472 | NULL, cifs_sb->local_nls, | |
1473 | cifs_sb->mnt_cifs_flags & | |
1474 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
fb8c4b14 | 1475 | if (rc == 0) { |
77159b4d | 1476 | unsigned int bytes_written; |
e30dcf3a SF |
1477 | rc = CIFSSMBWrite(xid, pTcon, |
1478 | netfid, 0, | |
1479 | attrs->ia_size, | |
1480 | &bytes_written, NULL, | |
1481 | NULL, 1 /* 45 sec */); | |
fb8c4b14 | 1482 | cFYI(1, ("wrt seteof rc %d", rc)); |
e30dcf3a SF |
1483 | CIFSSMBClose(xid, pTcon, netfid); |
1484 | } | |
1485 | ||
1486 | } | |
1da177e4 LT |
1487 | } |
1488 | ||
1489 | /* Server is ok setting allocation size implicitly - no need | |
1490 | to call: | |
1491 | CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size, TRUE, | |
1492 | cifs_sb->local_nls); | |
1493 | */ | |
1494 | ||
1495 | if (rc == 0) { | |
3677db10 | 1496 | rc = cifs_vmtruncate(direntry->d_inode, attrs->ia_size); |
1da177e4 LT |
1497 | cifs_truncate_page(direntry->d_inode->i_mapping, |
1498 | direntry->d_inode->i_size); | |
fb8c4b14 | 1499 | } else |
e30dcf3a | 1500 | goto cifs_setattr_exit; |
1da177e4 LT |
1501 | } |
1502 | if (attrs->ia_valid & ATTR_UID) { | |
e30dcf3a | 1503 | cFYI(1, ("UID changed to %d", attrs->ia_uid)); |
1da177e4 | 1504 | uid = attrs->ia_uid; |
1da177e4 LT |
1505 | } |
1506 | if (attrs->ia_valid & ATTR_GID) { | |
e30dcf3a | 1507 | cFYI(1, ("GID changed to %d", attrs->ia_gid)); |
1da177e4 | 1508 | gid = attrs->ia_gid; |
1da177e4 LT |
1509 | } |
1510 | ||
1511 | time_buf.Attributes = 0; | |
d32c4f26 JL |
1512 | |
1513 | /* skip mode change if it's just for clearing setuid/setgid */ | |
1514 | if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) | |
1515 | attrs->ia_valid &= ~ATTR_MODE; | |
1516 | ||
1da177e4 | 1517 | if (attrs->ia_valid & ATTR_MODE) { |
e30dcf3a | 1518 | cFYI(1, ("Mode changed to 0x%x", attrs->ia_mode)); |
1da177e4 | 1519 | mode = attrs->ia_mode; |
1da177e4 LT |
1520 | } |
1521 | ||
c18c842b | 1522 | if ((pTcon->unix_ext) |
1da177e4 LT |
1523 | && (attrs->ia_valid & (ATTR_MODE | ATTR_GID | ATTR_UID))) |
1524 | rc = CIFSSMBUnixSetPerms(xid, pTcon, full_path, mode, uid, gid, | |
737b758c | 1525 | 0 /* dev_t */, cifs_sb->local_nls, |
fb8c4b14 | 1526 | cifs_sb->mnt_cifs_flags & |
737b758c | 1527 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
1da177e4 | 1528 | else if (attrs->ia_valid & ATTR_MODE) { |
cdbce9c8 | 1529 | rc = 0; |
97837582 SF |
1530 | #ifdef CONFIG_CIFS_EXPERIMENTAL |
1531 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) | |
1532 | rc = mode_to_acl(direntry->d_inode, full_path, mode); | |
f6d09982 | 1533 | else if ((mode & S_IWUGO) == 0) { |
97837582 | 1534 | #else |
f6d09982 | 1535 | if ((mode & S_IWUGO) == 0) { |
97837582 | 1536 | #endif |
f6d09982 | 1537 | /* not writeable */ |
066fcb06 SF |
1538 | if ((cifsInode->cifsAttrs & ATTR_READONLY) == 0) { |
1539 | set_dosattr = TRUE; | |
1da177e4 LT |
1540 | time_buf.Attributes = |
1541 | cpu_to_le32(cifsInode->cifsAttrs | | |
1542 | ATTR_READONLY); | |
066fcb06 | 1543 | } |
5268df2e SF |
1544 | } else if (cifsInode->cifsAttrs & ATTR_READONLY) { |
1545 | /* If file is readonly on server, we would | |
1546 | not be able to write to it - so if any write | |
1547 | bit is enabled for user or group or other we | |
1548 | need to at least try to remove r/o dos attr */ | |
1549 | set_dosattr = TRUE; | |
1550 | time_buf.Attributes = cpu_to_le32(cifsInode->cifsAttrs & | |
1551 | (~ATTR_READONLY)); | |
1552 | /* Windows ignores set to zero */ | |
fb8c4b14 | 1553 | if (time_buf.Attributes == 0) |
5268df2e | 1554 | time_buf.Attributes |= cpu_to_le32(ATTR_NORMAL); |
1da177e4 | 1555 | } |
97837582 SF |
1556 | #ifdef CONFIG_CIFS_EXPERIMENTAL |
1557 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) | |
1558 | mode_to_acl(direntry->d_inode, full_path, mode); | |
1559 | #endif | |
1da177e4 LT |
1560 | } |
1561 | ||
1562 | if (attrs->ia_valid & ATTR_ATIME) { | |
1563 | set_time = TRUE; | |
1564 | time_buf.LastAccessTime = | |
1565 | cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime)); | |
1566 | } else | |
1567 | time_buf.LastAccessTime = 0; | |
1568 | ||
1569 | if (attrs->ia_valid & ATTR_MTIME) { | |
1570 | set_time = TRUE; | |
1571 | time_buf.LastWriteTime = | |
1572 | cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime)); | |
1573 | } else | |
1574 | time_buf.LastWriteTime = 0; | |
e30dcf3a SF |
1575 | /* Do not set ctime explicitly unless other time |
1576 | stamps are changed explicitly (i.e. by utime() | |
1577 | since we would then have a mix of client and | |
1578 | server times */ | |
50c2f753 | 1579 | |
e30dcf3a | 1580 | if (set_time && (attrs->ia_valid & ATTR_CTIME)) { |
1da177e4 | 1581 | set_time = TRUE; |
e30dcf3a SF |
1582 | /* Although Samba throws this field away |
1583 | it may be useful to Windows - but we do | |
1584 | not want to set ctime unless some other | |
1585 | timestamp is changing */ | |
26a21b98 | 1586 | cFYI(1, ("CIFS - CTIME changed")); |
1da177e4 LT |
1587 | time_buf.ChangeTime = |
1588 | cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime)); | |
1589 | } else | |
1590 | time_buf.ChangeTime = 0; | |
1591 | ||
066fcb06 | 1592 | if (set_time || set_dosattr) { |
1da177e4 LT |
1593 | time_buf.CreationTime = 0; /* do not change */ |
1594 | /* In the future we should experiment - try setting timestamps | |
1595 | via Handle (SetFileInfo) instead of by path */ | |
1596 | if (!(pTcon->ses->flags & CIFS_SES_NT4)) | |
1597 | rc = CIFSSMBSetTimes(xid, pTcon, full_path, &time_buf, | |
737b758c SF |
1598 | cifs_sb->local_nls, |
1599 | cifs_sb->mnt_cifs_flags & | |
1600 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
1da177e4 LT |
1601 | else |
1602 | rc = -EOPNOTSUPP; | |
1603 | ||
1604 | if (rc == -EOPNOTSUPP) { | |
1605 | int oplock = FALSE; | |
1606 | __u16 netfid; | |
1607 | ||
1608 | cFYI(1, ("calling SetFileInfo since SetPathInfo for " | |
1609 | "times not supported by this server")); | |
1610 | /* BB we could scan to see if we already have it open | |
1611 | and pass in pid of opener to function */ | |
1612 | rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN, | |
1613 | SYNCHRONIZE | FILE_WRITE_ATTRIBUTES, | |
1614 | CREATE_NOT_DIR, &netfid, &oplock, | |
737b758c SF |
1615 | NULL, cifs_sb->local_nls, |
1616 | cifs_sb->mnt_cifs_flags & | |
1617 | CIFS_MOUNT_MAP_SPECIAL_CHR); | |
fb8c4b14 | 1618 | if (rc == 0) { |
1da177e4 LT |
1619 | rc = CIFSSMBSetFileTimes(xid, pTcon, &time_buf, |
1620 | netfid); | |
1621 | CIFSSMBClose(xid, pTcon, netfid); | |
1622 | } else { | |
1623 | /* BB For even older servers we could convert time_buf | |
1624 | into old DOS style which uses two second | |
1625 | granularity */ | |
1626 | ||
1627 | /* rc = CIFSSMBSetTimesLegacy(xid, pTcon, full_path, | |
fb8c4b14 | 1628 | &time_buf, cifs_sb->local_nls); */ |
1da177e4 LT |
1629 | } |
1630 | } | |
e30dcf3a SF |
1631 | /* Even if error on time set, no sense failing the call if |
1632 | the server would set the time to a reasonable value anyway, | |
1633 | and this check ensures that we are not being called from | |
1634 | sys_utimes in which case we ought to fail the call back to | |
1635 | the user when the server rejects the call */ | |
fb8c4b14 | 1636 | if ((rc) && (attrs->ia_valid & |
e30dcf3a SF |
1637 | (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE))) |
1638 | rc = 0; | |
1da177e4 LT |
1639 | } |
1640 | ||
1641 | /* do not need local check to inode_check_ok since the server does | |
1642 | that */ | |
1643 | if (!rc) | |
1644 | rc = inode_setattr(direntry->d_inode, attrs); | |
e30dcf3a | 1645 | cifs_setattr_exit: |
1da177e4 LT |
1646 | kfree(full_path); |
1647 | FreeXid(xid); | |
1648 | return rc; | |
1649 | } | |
1650 | ||
99ee4dbd | 1651 | #if 0 |
1da177e4 LT |
1652 | void cifs_delete_inode(struct inode *inode) |
1653 | { | |
26a21b98 | 1654 | cFYI(1, ("In cifs_delete_inode, inode = 0x%p", inode)); |
1da177e4 LT |
1655 | /* may have to add back in if and when safe distributed caching of |
1656 | directories added e.g. via FindNotify */ | |
1657 | } | |
99ee4dbd | 1658 | #endif |