]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/cifs/inode.c
[CIFS] update cifs version number
[mirror_ubuntu-artful-kernel.git] / fs / cifs / inode.c
CommitLineData
1da177e4
LT
1/*
2 * fs/cifs/inode.c
3 *
8be0ed44 4 * Copyright (C) International Business Machines Corp., 2002,2008
1da177e4
LT
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>
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 32
7962670e 33static void cifs_set_ops(struct inode *inode, const bool is_dfs_referral)
70eff55d
CH
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:
bc5b6e24 60#ifdef CONFIG_CIFS_DFS_UPCALL
7962670e
IM
61 if (is_dfs_referral) {
62 inode->i_op = &cifs_dfs_referral_inode_operations;
63 } else {
bc5b6e24
SF
64#else /* NO DFS support, treat as a directory */
65 {
66#endif
7962670e
IM
67 inode->i_op = &cifs_dir_inode_ops;
68 inode->i_fop = &cifs_dir_ops;
69 }
70eff55d
CH
70 break;
71 case S_IFLNK:
72 inode->i_op = &cifs_symlink_inode_ops;
73 break;
74 default:
75 init_special_inode(inode, inode->i_mode, inode->i_rdev);
76 break;
77 }
78}
79
cc0bad75
JL
80/* populate an inode with info from a cifs_fattr struct */
81void
82cifs_fattr_to_inode(struct inode *inode, struct cifs_fattr *fattr)
75f12983 83{
cc0bad75
JL
84 struct cifsInodeInfo *cifs_i = CIFS_I(inode);
85 unsigned long now = jiffies;
86
87 inode->i_atime = fattr->cf_atime;
88 inode->i_mtime = fattr->cf_mtime;
89 inode->i_ctime = fattr->cf_ctime;
90 inode->i_mode = fattr->cf_mode;
91 inode->i_rdev = fattr->cf_rdev;
92 inode->i_nlink = fattr->cf_nlink;
93 inode->i_uid = fattr->cf_uid;
94 inode->i_gid = fattr->cf_gid;
95
96 cifs_i->cifsAttrs = fattr->cf_cifsattrs;
97 cifs_i->uniqueid = fattr->cf_uniqueid;
75f12983 98
cc0bad75
JL
99 cFYI(1, ("inode 0x%p old_time=%ld new_time=%ld", inode,
100 cifs_i->time, now));
101 cifs_i->time = now;
102
103 /*
104 * Can't safely change the file size here if the client is writing to
105 * it due to potential races.
106 */
107 spin_lock(&inode->i_lock);
108 if (is_size_safe_to_change(cifs_i, fattr->cf_eof)) {
109 i_size_write(inode, fattr->cf_eof);
110
111 /*
112 * i_blocks is not related to (i_size / i_blksize),
113 * but instead 512 byte (2**9) size is required for
114 * calculating num blocks.
115 */
116 inode->i_blocks = (512 - 1 + fattr->cf_bytes) >> 9;
117 }
118 spin_unlock(&inode->i_lock);
119
120 cifs_set_ops(inode, fattr->cf_flags & CIFS_FATTR_DFS_REFERRAL);
121}
122
123/* Fill a cifs_fattr struct with info from FILE_UNIX_BASIC_INFO. */
124void
125cifs_unix_basic_to_fattr(struct cifs_fattr *fattr, FILE_UNIX_BASIC_INFO *info,
126 struct cifs_sb_info *cifs_sb)
127{
128 memset(fattr, 0, sizeof(*fattr));
129 fattr->cf_uniqueid = le64_to_cpu(info->UniqueId);
130 fattr->cf_bytes = le64_to_cpu(info->NumOfBytes);
131 fattr->cf_eof = le64_to_cpu(info->EndOfFile);
132
133 fattr->cf_atime = cifs_NTtimeToUnix(info->LastAccessTime);
134 fattr->cf_mtime = cifs_NTtimeToUnix(info->LastModificationTime);
135 fattr->cf_ctime = cifs_NTtimeToUnix(info->LastStatusChange);
136 fattr->cf_mode = le64_to_cpu(info->Permissions);
75f12983
CH
137
138 /*
139 * Since we set the inode type below we need to mask off
140 * to avoid strange results if bits set above.
141 */
cc0bad75 142 fattr->cf_mode &= ~S_IFMT;
75f12983
CH
143 switch (le32_to_cpu(info->Type)) {
144 case UNIX_FILE:
cc0bad75
JL
145 fattr->cf_mode |= S_IFREG;
146 fattr->cf_dtype = DT_REG;
75f12983
CH
147 break;
148 case UNIX_SYMLINK:
cc0bad75
JL
149 fattr->cf_mode |= S_IFLNK;
150 fattr->cf_dtype = DT_LNK;
75f12983
CH
151 break;
152 case UNIX_DIR:
cc0bad75
JL
153 fattr->cf_mode |= S_IFDIR;
154 fattr->cf_dtype = DT_DIR;
75f12983
CH
155 break;
156 case UNIX_CHARDEV:
cc0bad75
JL
157 fattr->cf_mode |= S_IFCHR;
158 fattr->cf_dtype = DT_CHR;
159 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
160 le64_to_cpu(info->DevMinor) & MINORMASK);
75f12983
CH
161 break;
162 case UNIX_BLOCKDEV:
cc0bad75
JL
163 fattr->cf_mode |= S_IFBLK;
164 fattr->cf_dtype = DT_BLK;
165 fattr->cf_rdev = MKDEV(le64_to_cpu(info->DevMajor),
166 le64_to_cpu(info->DevMinor) & MINORMASK);
75f12983
CH
167 break;
168 case UNIX_FIFO:
cc0bad75
JL
169 fattr->cf_mode |= S_IFIFO;
170 fattr->cf_dtype = DT_FIFO;
75f12983
CH
171 break;
172 case UNIX_SOCKET:
cc0bad75
JL
173 fattr->cf_mode |= S_IFSOCK;
174 fattr->cf_dtype = DT_SOCK;
75f12983
CH
175 break;
176 default:
177 /* safest to call it a file if we do not know */
cc0bad75
JL
178 fattr->cf_mode |= S_IFREG;
179 fattr->cf_dtype = DT_REG;
75f12983
CH
180 cFYI(1, ("unknown type %d", le32_to_cpu(info->Type)));
181 break;
182 }
183
cc0bad75
JL
184 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_UID)
185 fattr->cf_uid = cifs_sb->mnt_uid;
75f12983 186 else
cc0bad75 187 fattr->cf_uid = le64_to_cpu(info->Uid);
75f12983 188
cc0bad75
JL
189 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_OVERR_GID)
190 fattr->cf_gid = cifs_sb->mnt_gid;
75f12983 191 else
cc0bad75 192 fattr->cf_gid = le64_to_cpu(info->Gid);
75f12983 193
cc0bad75 194 fattr->cf_nlink = le64_to_cpu(info->Nlinks);
75f12983
CH
195}
196
b9a3260f 197/*
cc0bad75
JL
198 * Fill a cifs_fattr struct with fake inode info.
199 *
200 * Needed to setup cifs_fattr data for the directory which is the
201 * junction to the new submount (ie to setup the fake directory
202 * which represents a DFS referral).
b9a3260f 203 */
cc0bad75
JL
204void
205cifs_create_dfs_fattr(struct cifs_fattr *fattr, struct super_block *sb)
0e4bbde9 206{
cc0bad75 207 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
0e4bbde9 208
cc0bad75
JL
209 cFYI(1, ("creating fake fattr for DFS referral"));
210
211 memset(fattr, 0, sizeof(*fattr));
212 fattr->cf_mode = S_IFDIR | S_IXUGO | S_IRWXU;
213 fattr->cf_uid = cifs_sb->mnt_uid;
214 fattr->cf_gid = cifs_sb->mnt_gid;
215 fattr->cf_atime = CURRENT_TIME;
216 fattr->cf_ctime = CURRENT_TIME;
217 fattr->cf_mtime = CURRENT_TIME;
218 fattr->cf_nlink = 2;
219 fattr->cf_flags |= CIFS_FATTR_DFS_REFERRAL;
0e4bbde9
SF
220}
221
132ac7b7
JL
222/**
223 * cifs_new inode - create new inode, initialize, and hash it
224 * @sb - pointer to superblock
225 * @inum - if valid pointer and serverino is enabled, replace i_ino with val
226 *
227 * Create a new inode, initialize it for CIFS and hash it. Returns the new
228 * inode or NULL if one couldn't be allocated.
229 *
230 * If the share isn't mounted with "serverino" or inum is a NULL pointer then
231 * we'll just use the inode number assigned by new_inode(). Note that this can
232 * mean i_ino collisions since the i_ino assigned by new_inode is not
233 * guaranteed to be unique.
234 */
235struct inode *
950ec528 236cifs_new_inode(struct super_block *sb, __u64 *inum)
132ac7b7
JL
237{
238 struct inode *inode;
239
240 inode = new_inode(sb);
241 if (inode == NULL)
242 return NULL;
243
244 /*
245 * BB: Is i_ino == 0 legal? Here, we assume that it is. If it isn't we
246 * stop passing inum as ptr. Are there sanity checks we can use to
247 * ensure that the server is really filling in that field? Also,
248 * if serverino is disabled, perhaps we should be using iunique()?
249 */
250 if (inum && (CIFS_SB(sb)->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM))
950ec528 251 inode->i_ino = (unsigned long) *inum;
132ac7b7
JL
252
253 /*
254 * must set this here instead of cifs_alloc_inode since VFS will
255 * clobber i_flags
256 */
257 if (sb->s_flags & MS_NOATIME)
258 inode->i_flags |= S_NOATIME | S_NOCMTIME;
259
260 insert_inode_hash(inode);
261
262 return inode;
263}
264
1da177e4 265int cifs_get_inode_info_unix(struct inode **pinode,
cc0bad75
JL
266 const unsigned char *full_path,
267 struct super_block *sb, int xid)
1da177e4 268{
cc0bad75 269 int rc;
0e4bbde9 270 FILE_UNIX_BASIC_INFO find_data;
cc0bad75
JL
271 struct cifs_fattr fattr;
272 struct cifsTconInfo *tcon;
1da177e4 273 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1da177e4 274
cc0bad75 275 tcon = cifs_sb->tcon;
646dd539 276 cFYI(1, ("Getting info on %s", full_path));
7962670e 277
1da177e4 278 /* could have done a find first instead but this returns more info */
cc0bad75 279 rc = CIFSSMBUnixQPathInfo(xid, tcon, full_path, &find_data,
737b758c
SF
280 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
281 CIFS_MOUNT_MAP_SPECIAL_CHR);
e911d0cc 282
cc0bad75
JL
283 if (!rc) {
284 cifs_unix_basic_to_fattr(&fattr, &find_data, cifs_sb);
285 } else if (rc == -EREMOTE) {
286 cifs_create_dfs_fattr(&fattr, sb);
287 rc = 0;
288 } else {
289 return rc;
290 }
1da177e4 291
0e4bbde9 292 if (*pinode == NULL) {
cc0bad75
JL
293 /* get new inode */
294 *pinode = cifs_iget(sb, &fattr);
295 if (!*pinode)
0e4bbde9 296 rc = -ENOMEM;
cc0bad75
JL
297 } else {
298 /* we already have inode, update it */
299 cifs_fattr_to_inode(*pinode, &fattr);
0e4bbde9 300 }
1da177e4 301
1da177e4
LT
302 return rc;
303}
304
fb8c4b14 305static int decode_sfu_inode(struct inode *inode, __u64 size,
d6e2f2a4
SF
306 const unsigned char *path,
307 struct cifs_sb_info *cifs_sb, int xid)
308{
309 int rc;
4b18f2a9 310 int oplock = 0;
d6e2f2a4
SF
311 __u16 netfid;
312 struct cifsTconInfo *pTcon = cifs_sb->tcon;
86c96b4b 313 char buf[24];
d6e2f2a4 314 unsigned int bytes_read;
fb8c4b14 315 char *pbuf;
d6e2f2a4
SF
316
317 pbuf = buf;
318
4523cc30 319 if (size == 0) {
d6e2f2a4
SF
320 inode->i_mode |= S_IFIFO;
321 return 0;
322 } else if (size < 8) {
323 return -EINVAL; /* EOPNOTSUPP? */
324 }
50c2f753 325
d6e2f2a4
SF
326 rc = CIFSSMBOpen(xid, pTcon, path, FILE_OPEN, GENERIC_READ,
327 CREATE_NOT_DIR, &netfid, &oplock, NULL,
328 cifs_sb->local_nls,
329 cifs_sb->mnt_cifs_flags &
330 CIFS_MOUNT_MAP_SPECIAL_CHR);
fb8c4b14 331 if (rc == 0) {
ec637e3f 332 int buf_type = CIFS_NO_BUFFER;
d6e2f2a4
SF
333 /* Read header */
334 rc = CIFSSMBRead(xid, pTcon,
fb8c4b14 335 netfid,
86c96b4b 336 24 /* length */, 0 /* offset */,
ec637e3f 337 &bytes_read, &pbuf, &buf_type);
4523cc30
SF
338 if ((rc == 0) && (bytes_read >= 8)) {
339 if (memcmp("IntxBLK", pbuf, 8) == 0) {
fb8c4b14 340 cFYI(1, ("Block device"));
3020a1f5 341 inode->i_mode |= S_IFBLK;
4523cc30 342 if (bytes_read == 24) {
86c96b4b
SF
343 /* we have enough to decode dev num */
344 __u64 mjr; /* major */
345 __u64 mnr; /* minor */
346 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
347 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
348 inode->i_rdev = MKDEV(mjr, mnr);
349 }
4523cc30 350 } else if (memcmp("IntxCHR", pbuf, 8) == 0) {
fb8c4b14 351 cFYI(1, ("Char device"));
3020a1f5 352 inode->i_mode |= S_IFCHR;
4523cc30 353 if (bytes_read == 24) {
86c96b4b
SF
354 /* we have enough to decode dev num */
355 __u64 mjr; /* major */
356 __u64 mnr; /* minor */
357 mjr = le64_to_cpu(*(__le64 *)(pbuf+8));
358 mnr = le64_to_cpu(*(__le64 *)(pbuf+16));
359 inode->i_rdev = MKDEV(mjr, mnr);
fb8c4b14 360 }
4523cc30 361 } else if (memcmp("IntxLNK", pbuf, 7) == 0) {
fb8c4b14 362 cFYI(1, ("Symlink"));
3020a1f5 363 inode->i_mode |= S_IFLNK;
86c96b4b
SF
364 } else {
365 inode->i_mode |= S_IFREG; /* file? */
fb8c4b14 366 rc = -EOPNOTSUPP;
86c96b4b 367 }
3020a1f5
SF
368 } else {
369 inode->i_mode |= S_IFREG; /* then it is a file */
fb8c4b14
SF
370 rc = -EOPNOTSUPP; /* or some unknown SFU type */
371 }
d6e2f2a4 372 CIFSSMBClose(xid, pTcon, netfid);
d6e2f2a4
SF
373 }
374 return rc;
d6e2f2a4
SF
375}
376
9e294f1c
SF
377#define SFBITS_MASK (S_ISVTX | S_ISGID | S_ISUID) /* SETFILEBITS valid bits */
378
953f8681 379static int get_sfu_mode(struct inode *inode,
9e294f1c
SF
380 const unsigned char *path,
381 struct cifs_sb_info *cifs_sb, int xid)
382{
3020a1f5 383#ifdef CONFIG_CIFS_XATTR
9e294f1c
SF
384 ssize_t rc;
385 char ea_value[4];
386 __u32 mode;
387
388 rc = CIFSSMBQueryEA(xid, cifs_sb->tcon, path, "SETFILEBITS",
389 ea_value, 4 /* size of buf */, cifs_sb->local_nls,
fb8c4b14 390 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
4523cc30 391 if (rc < 0)
9e294f1c
SF
392 return (int)rc;
393 else if (rc > 3) {
394 mode = le32_to_cpu(*((__le32 *)ea_value));
fb8c4b14
SF
395 inode->i_mode &= ~SFBITS_MASK;
396 cFYI(1, ("special bits 0%o org mode 0%o", mode, inode->i_mode));
9e294f1c 397 inode->i_mode = (mode & SFBITS_MASK) | inode->i_mode;
fb8c4b14 398 cFYI(1, ("special mode bits 0%o", mode));
9e294f1c
SF
399 return 0;
400 } else {
401 return 0;
402 }
3020a1f5
SF
403#else
404 return -EOPNOTSUPP;
405#endif
9e294f1c
SF
406}
407
b9a3260f
SF
408/*
409 * Needed to setup inode data for the directory which is the
410 * junction to the new submount (ie to setup the fake directory
411 * which represents a DFS referral)
412 */
413static void fill_fake_finddata(FILE_ALL_INFO *pfnd_dat,
414 struct super_block *sb)
415{
0a891adc 416 memset(pfnd_dat, 0, sizeof(FILE_ALL_INFO));
b9a3260f
SF
417
418/* __le64 pfnd_dat->AllocationSize = cpu_to_le64(0);
419 __le64 pfnd_dat->EndOfFile = cpu_to_le64(0);
420 __u8 pfnd_dat->DeletePending = 0;
421 __u8 pfnd_data->Directory = 0;
422 __le32 pfnd_dat->EASize = 0;
423 __u64 pfnd_dat->IndexNumber = 0;
424 __u64 pfnd_dat->IndexNumber1 = 0; */
425 pfnd_dat->CreationTime =
426 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
427 pfnd_dat->LastAccessTime =
428 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
429 pfnd_dat->LastWriteTime =
430 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
431 pfnd_dat->ChangeTime =
432 cpu_to_le64(cifs_UnixTimeToNT(CURRENT_TIME));
433 pfnd_dat->Attributes = cpu_to_le32(ATTR_DIRECTORY);
434 pfnd_dat->NumberOfLinks = cpu_to_le32(2);
435}
436
1da177e4 437int cifs_get_inode_info(struct inode **pinode,
646dd539 438 const unsigned char *full_path, FILE_ALL_INFO *pfindData,
8b1327f6 439 struct super_block *sb, int xid, const __u16 *pfid)
1da177e4
LT
440{
441 int rc = 0;
b9a3260f
SF
442 __u32 attr;
443 struct cifsInodeInfo *cifsInfo;
1da177e4
LT
444 struct cifsTconInfo *pTcon;
445 struct inode *inode;
446 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1da177e4 447 char *buf = NULL;
5ade9dea 448 bool adjustTZ = false;
7962670e 449 bool is_dfs_referral = false;
4468eb3f 450 umode_t default_mode;
1da177e4
LT
451
452 pTcon = cifs_sb->tcon;
646dd539 453 cFYI(1, ("Getting info on %s", full_path));
1da177e4 454
d0d2f2df
SF
455 if ((pfindData == NULL) && (*pinode != NULL)) {
456 if (CIFS_I(*pinode)->clientCanCacheRead) {
fb8c4b14 457 cFYI(1, ("No need to revalidate cached inode sizes"));
1da177e4
LT
458 return rc;
459 }
460 }
461
462 /* if file info not passed in then get it from server */
d0d2f2df 463 if (pfindData == NULL) {
1da177e4 464 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
d0d2f2df 465 if (buf == NULL)
1da177e4
LT
466 return -ENOMEM;
467 pfindData = (FILE_ALL_INFO *)buf;
7962670e 468
1da177e4 469 /* could do find first instead but this returns more info */
7962670e 470 rc = CIFSSMBQPathInfo(xid, pTcon, full_path, pfindData,
acf1a1b1 471 0 /* not legacy */,
6b8edfe0 472 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
737b758c 473 CIFS_MOUNT_MAP_SPECIAL_CHR);
6b8edfe0
SF
474 /* BB optimize code so we do not make the above call
475 when server claims no NT SMB support and the above call
476 failed at least once - set flag in tcon or mount */
4523cc30 477 if ((rc == -EOPNOTSUPP) || (rc == -EINVAL)) {
7962670e 478 rc = SMBQueryInformation(xid, pTcon, full_path,
fb8c4b14 479 pfindData, cifs_sb->local_nls,
6b8edfe0
SF
480 cifs_sb->mnt_cifs_flags &
481 CIFS_MOUNT_MAP_SPECIAL_CHR);
4b18f2a9 482 adjustTZ = true;
6b8edfe0 483 }
1da177e4
LT
484 }
485 /* dump_mem("\nQPathInfo return data",&findData, sizeof(findData)); */
b9a3260f
SF
486 if (rc == -EREMOTE) {
487 is_dfs_referral = true;
488 fill_fake_finddata(pfindData, sb);
489 rc = 0;
490 } else if (rc)
7962670e 491 goto cgii_exit;
1da177e4 492
b9a3260f
SF
493 attr = le32_to_cpu(pfindData->Attributes);
494
495 /* get new inode */
496 if (*pinode == NULL) {
132ac7b7 497 __u64 inode_num;
950ec528 498 __u64 *pinum = &inode_num;
132ac7b7 499
b9a3260f
SF
500 /* Is an i_ino of zero legal? Can we use that to check
501 if the server supports returning inode numbers? Are
502 there other sanity checks we can use to ensure that
503 the server is really filling in that field? */
504
505 /* We can not use the IndexNumber field by default from
506 Windows or Samba (in ALL_INFO buf) but we can request
507 it explicitly. It may not be unique presumably if
508 the server has multiple devices mounted under one share */
509
510 /* There may be higher info levels that work but are
511 there Windows server or network appliances for which
512 IndexNumber field is not guaranteed unique? */
513
514 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SERVER_INUM) {
515 int rc1 = 0;
b9a3260f
SF
516
517 rc1 = CIFSGetSrvInodeNumber(xid, pTcon,
950ec528 518 full_path, pinum,
737b758c
SF
519 cifs_sb->local_nls,
520 cifs_sb->mnt_cifs_flags &
521 CIFS_MOUNT_MAP_SPECIAL_CHR);
b9a3260f
SF
522 if (rc1) {
523 cFYI(1, ("GetSrvInodeNum rc %d", rc1));
950ec528 524 pinum = NULL;
b9a3260f 525 /* BB EOPNOSUPP disable SERVER_INUM? */
132ac7b7 526 }
132ac7b7 527 } else {
950ec528 528 pinum = NULL;
132ac7b7
JL
529 }
530
950ec528 531 *pinode = cifs_new_inode(sb, pinum);
132ac7b7
JL
532 if (*pinode == NULL) {
533 rc = -ENOMEM;
534 goto cgii_exit;
535 }
b9a3260f
SF
536 }
537 inode = *pinode;
538 cifsInfo = CIFS_I(inode);
539 cifsInfo->cifsAttrs = attr;
9a8165fc 540 cifsInfo->delete_pending = pfindData->DeletePending ? true : false;
b9a3260f
SF
541 cFYI(1, ("Old time %ld", cifsInfo->time));
542 cifsInfo->time = jiffies;
543 cFYI(1, ("New time %ld", cifsInfo->time));
544
545 /* blksize needs to be multiple of two. So safer to default to
546 blksize and blkbits set in superblock so 2**blkbits and blksize
547 will match rather than setting to:
548 (pTcon->ses->server->maxBuf - MAX_CIFS_HDR_SIZE) & 0xFFFFFE00;*/
549
550 /* Linux can not store file creation time so ignore it */
551 if (pfindData->LastAccessTime)
07119a4d 552 inode->i_atime = cifs_NTtimeToUnix(pfindData->LastAccessTime);
b9a3260f
SF
553 else /* do not need to use current_fs_time - time not stored */
554 inode->i_atime = CURRENT_TIME;
07119a4d
JL
555 inode->i_mtime = cifs_NTtimeToUnix(pfindData->LastWriteTime);
556 inode->i_ctime = cifs_NTtimeToUnix(pfindData->ChangeTime);
b9a3260f
SF
557 cFYI(DBG2, ("Attributes came in as 0x%x", attr));
558 if (adjustTZ && (pTcon->ses) && (pTcon->ses->server)) {
559 inode->i_ctime.tv_sec += pTcon->ses->server->timeAdj;
560 inode->i_mtime.tv_sec += pTcon->ses->server->timeAdj;
561 }
1da177e4 562
4468eb3f
JL
563 /* get default inode mode */
564 if (attr & ATTR_DIRECTORY)
565 default_mode = cifs_sb->mnt_dir_mode;
566 else
567 default_mode = cifs_sb->mnt_file_mode;
568
569 /* set permission bits */
570 if (atomic_read(&cifsInfo->inUse) == 0 ||
571 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
572 inode->i_mode = default_mode;
573 else {
574 /* just reenable write bits if !ATTR_READONLY */
575 if ((inode->i_mode & S_IWUGO) == 0 &&
576 (attr & ATTR_READONLY) == 0)
577 inode->i_mode |= (S_IWUGO & default_mode);
aab3a8c7
IJ
578
579 inode->i_mode &= ~S_IFMT;
4468eb3f
JL
580 }
581 /* clear write bits if ATTR_READONLY is set */
582 if (attr & ATTR_READONLY)
583 inode->i_mode &= ~S_IWUGO;
584
585 /* set inode type */
586 if ((attr & ATTR_SYSTEM) &&
587 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL)) {
588 /* no need to fix endianness on 0 */
589 if (pfindData->EndOfFile == 0)
590 inode->i_mode |= S_IFIFO;
591 else if (decode_sfu_inode(inode,
592 le64_to_cpu(pfindData->EndOfFile),
593 full_path, cifs_sb, xid))
594 cFYI(1, ("unknown SFU file type\n"));
b9a3260f 595 } else {
4468eb3f
JL
596 if (attr & ATTR_DIRECTORY)
597 inode->i_mode |= S_IFDIR;
598 else
599 inode->i_mode |= S_IFREG;
b9a3260f
SF
600 }
601
fbec9ab9 602 cifsInfo->server_eof = le64_to_cpu(pfindData->EndOfFile);
b9a3260f 603 spin_lock(&inode->i_lock);
fbec9ab9 604 if (is_size_safe_to_change(cifsInfo, cifsInfo->server_eof)) {
b9a3260f
SF
605 /* can not safely shrink the file size here if the
606 client is writing to it due to potential races */
fbec9ab9 607 i_size_write(inode, cifsInfo->server_eof);
b9a3260f
SF
608
609 /* 512 bytes (2**9) is the fake blocksize that must be
610 used for this calculation */
611 inode->i_blocks = (512 - 1 + le64_to_cpu(
612 pfindData->AllocationSize)) >> 9;
613 }
614 spin_unlock(&inode->i_lock);
1da177e4 615
b9a3260f 616 inode->i_nlink = le32_to_cpu(pfindData->NumberOfLinks);
1da177e4 617
b9a3260f
SF
618 /* BB fill in uid and gid here? with help from winbind?
619 or retrieve from NTFS stream extended attribute */
4879b448 620#ifdef CONFIG_CIFS_EXPERIMENTAL
b9a3260f
SF
621 /* fill in 0777 bits from ACL */
622 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) {
623 cFYI(1, ("Getting mode bits from ACL"));
1bf4072d 624 acl_to_uid_mode(cifs_sb, inode, full_path, pfid);
b9a3260f 625 }
4879b448 626#endif
b9a3260f
SF
627 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
628 /* fill in remaining high mode bits e.g. SUID, VTX */
629 get_sfu_mode(inode, full_path, cifs_sb, xid);
630 } else if (atomic_read(&cifsInfo->inUse) == 0) {
631 inode->i_uid = cifs_sb->mnt_uid;
632 inode->i_gid = cifs_sb->mnt_gid;
633 /* set so we do not keep refreshing these fields with
634 bad data after user has changed them in memory */
635 atomic_set(&cifsInfo->inUse, 1);
1da177e4 636 }
b9a3260f
SF
637
638 cifs_set_ops(inode, is_dfs_referral);
639
640
641
642
7962670e 643cgii_exit:
1da177e4
LT
644 kfree(buf);
645 return rc;
646}
647
7f8ed420
SF
648static const struct inode_operations cifs_ipc_inode_ops = {
649 .lookup = cifs_lookup,
650};
651
e4cce94c 652char *cifs_build_path_to_root(struct cifs_sb_info *cifs_sb)
8be0ed44
SF
653{
654 int pplen = cifs_sb->prepathlen;
655 int dfsplen;
656 char *full_path = NULL;
657
658 /* if no prefix path, simply set path to the root of share to "" */
659 if (pplen == 0) {
660 full_path = kmalloc(1, GFP_KERNEL);
661 if (full_path)
662 full_path[0] = 0;
663 return full_path;
664 }
665
666 if (cifs_sb->tcon && (cifs_sb->tcon->Flags & SMB_SHARE_IS_IN_DFS))
667 dfsplen = strnlen(cifs_sb->tcon->treeName, MAX_TREE_SIZE + 1);
668 else
669 dfsplen = 0;
670
671 full_path = kmalloc(dfsplen + pplen + 1, GFP_KERNEL);
672 if (full_path == NULL)
673 return full_path;
674
675 if (dfsplen) {
676 strncpy(full_path, cifs_sb->tcon->treeName, dfsplen);
677 /* switch slash direction in prepath depending on whether
678 * windows or posix style path names
679 */
680 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_POSIX_PATHS) {
681 int i;
682 for (i = 0; i < dfsplen; i++) {
683 if (full_path[i] == '\\')
684 full_path[i] = '/';
685 }
686 }
687 }
688 strncpy(full_path + dfsplen, cifs_sb->prepath, pplen);
689 full_path[dfsplen + pplen] = 0; /* add trailing null */
690 return full_path;
691}
692
cc0bad75
JL
693static int
694cifs_find_inode(struct inode *inode, void *opaque)
695{
696 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
697
698 if (CIFS_I(inode)->uniqueid != fattr->cf_uniqueid)
699 return 0;
700
701 return 1;
702}
703
704static int
705cifs_init_inode(struct inode *inode, void *opaque)
706{
707 struct cifs_fattr *fattr = (struct cifs_fattr *) opaque;
708
709 CIFS_I(inode)->uniqueid = fattr->cf_uniqueid;
710 return 0;
711}
712
713/* Given fattrs, get a corresponding inode */
714struct inode *
715cifs_iget(struct super_block *sb, struct cifs_fattr *fattr)
716{
717 unsigned long hash;
718 struct inode *inode;
719
720 cFYI(1, ("looking for uniqueid=%llu", fattr->cf_uniqueid));
721
722 /* hash down to 32-bits on 32-bit arch */
723 hash = cifs_uniqueid_to_ino_t(fattr->cf_uniqueid);
724
725 inode = iget5_locked(sb, hash, cifs_find_inode, cifs_init_inode, fattr);
726
727 /* we have fattrs in hand, update the inode */
728 if (inode) {
729 cifs_fattr_to_inode(inode, fattr);
730 if (sb->s_flags & MS_NOATIME)
731 inode->i_flags |= S_NOATIME | S_NOCMTIME;
732 if (inode->i_state & I_NEW) {
733 inode->i_ino = hash;
734 unlock_new_inode(inode);
735 }
736 }
737
738 return inode;
739}
740
1da177e4 741/* gets root inode */
bd433d4c 742struct inode *cifs_root_iget(struct super_block *sb, unsigned long ino)
1da177e4 743{
ce634ab2 744 int xid;
1da177e4 745 struct cifs_sb_info *cifs_sb;
cc0bad75 746 struct inode *inode = NULL;
ce634ab2 747 long rc;
8be0ed44 748 char *full_path;
ce634ab2 749
cc0bad75 750 cifs_sb = CIFS_SB(sb);
e4cce94c 751 full_path = cifs_build_path_to_root(cifs_sb);
8be0ed44
SF
752 if (full_path == NULL)
753 return ERR_PTR(-ENOMEM);
c18c842b 754
8be0ed44 755 xid = GetXid();
cc0bad75
JL
756 if (cifs_sb->tcon->unix_ext) {
757 rc = cifs_get_inode_info_unix(&inode, full_path, sb, xid);
758 if (!inode)
759 return ERR_PTR(-ENOMEM);
760 } else {
761 inode = iget_locked(sb, ino);
762 if (!inode)
763 return ERR_PTR(-ENOMEM);
764 if (!(inode->i_state & I_NEW))
765 return inode;
766
8be0ed44
SF
767 rc = cifs_get_inode_info(&inode, full_path, NULL, inode->i_sb,
768 xid, NULL);
cc0bad75
JL
769 unlock_new_inode(inode);
770 }
771
7f8ed420
SF
772 if (rc && cifs_sb->tcon->ipc) {
773 cFYI(1, ("ipc connection - fake read inode"));
774 inode->i_mode |= S_IFDIR;
775 inode->i_nlink = 2;
776 inode->i_op = &cifs_ipc_inode_ops;
777 inode->i_fop = &simple_dir_operations;
778 inode->i_uid = cifs_sb->mnt_uid;
779 inode->i_gid = cifs_sb->mnt_gid;
ad661334 780 } else if (rc) {
8be0ed44 781 kfree(full_path);
ce634ab2
DH
782 _FreeXid(xid);
783 iget_failed(inode);
784 return ERR_PTR(rc);
7f8ed420
SF
785 }
786
ce634ab2 787
8be0ed44 788 kfree(full_path);
ce634ab2
DH
789 /* can not call macro FreeXid here since in a void func
790 * TODO: This is no longer true
791 */
1da177e4 792 _FreeXid(xid);
ce634ab2 793 return inode;
1da177e4
LT
794}
795
388e57b2
SF
796static int
797cifs_set_file_info(struct inode *inode, struct iattr *attrs, int xid,
798 char *full_path, __u32 dosattr)
799{
800 int rc;
801 int oplock = 0;
802 __u16 netfid;
803 __u32 netpid;
804 bool set_time = false;
805 struct cifsFileInfo *open_file;
806 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
807 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
808 struct cifsTconInfo *pTcon = cifs_sb->tcon;
809 FILE_BASIC_INFO info_buf;
810
1adcb710
SF
811 if (attrs == NULL)
812 return -EINVAL;
813
388e57b2
SF
814 if (attrs->ia_valid & ATTR_ATIME) {
815 set_time = true;
816 info_buf.LastAccessTime =
817 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_atime));
818 } else
819 info_buf.LastAccessTime = 0;
820
821 if (attrs->ia_valid & ATTR_MTIME) {
822 set_time = true;
823 info_buf.LastWriteTime =
824 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_mtime));
825 } else
826 info_buf.LastWriteTime = 0;
827
828 /*
829 * Samba throws this field away, but windows may actually use it.
830 * Do not set ctime unless other time stamps are changed explicitly
831 * (i.e. by utimes()) since we would then have a mix of client and
832 * server times.
833 */
834 if (set_time && (attrs->ia_valid & ATTR_CTIME)) {
835 cFYI(1, ("CIFS - CTIME changed"));
836 info_buf.ChangeTime =
837 cpu_to_le64(cifs_UnixTimeToNT(attrs->ia_ctime));
838 } else
839 info_buf.ChangeTime = 0;
840
841 info_buf.CreationTime = 0; /* don't change */
842 info_buf.Attributes = cpu_to_le32(dosattr);
843
844 /*
845 * If the file is already open for write, just use that fileid
846 */
847 open_file = find_writable_file(cifsInode);
848 if (open_file) {
849 netfid = open_file->netfid;
850 netpid = open_file->pid;
851 goto set_via_filehandle;
852 }
853
854 /*
855 * NT4 apparently returns success on this call, but it doesn't
856 * really work.
857 */
858 if (!(pTcon->ses->flags & CIFS_SES_NT4)) {
859 rc = CIFSSMBSetPathInfo(xid, pTcon, full_path,
860 &info_buf, cifs_sb->local_nls,
861 cifs_sb->mnt_cifs_flags &
862 CIFS_MOUNT_MAP_SPECIAL_CHR);
6b37faa1
JL
863 if (rc == 0) {
864 cifsInode->cifsAttrs = dosattr;
865 goto out;
866 } else if (rc != -EOPNOTSUPP && rc != -EINVAL)
388e57b2
SF
867 goto out;
868 }
869
870 cFYI(1, ("calling SetFileInfo since SetPathInfo for "
871 "times not supported by this server"));
872 rc = CIFSSMBOpen(xid, pTcon, full_path, FILE_OPEN,
873 SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
874 CREATE_NOT_DIR, &netfid, &oplock,
875 NULL, cifs_sb->local_nls,
876 cifs_sb->mnt_cifs_flags &
877 CIFS_MOUNT_MAP_SPECIAL_CHR);
878
879 if (rc != 0) {
880 if (rc == -EIO)
881 rc = -EINVAL;
882 goto out;
883 }
884
885 netpid = current->tgid;
886
887set_via_filehandle:
888 rc = CIFSSMBSetFileInfo(xid, pTcon, &info_buf, netfid, netpid);
d388908e
SF
889 if (!rc)
890 cifsInode->cifsAttrs = dosattr;
891
388e57b2
SF
892 if (open_file == NULL)
893 CIFSSMBClose(xid, pTcon, netfid);
894 else
895 atomic_dec(&open_file->wrtPending);
896out:
897 return rc;
898}
899
a12a1ac7
JL
900/*
901 * open the given file (if it isn't already), set the DELETE_ON_CLOSE bit
902 * and rename it to a random name that hopefully won't conflict with
903 * anything else.
904 */
905static int
3270958b 906cifs_rename_pending_delete(char *full_path, struct dentry *dentry, int xid)
a12a1ac7
JL
907{
908 int oplock = 0;
909 int rc;
910 __u16 netfid;
3270958b 911 struct inode *inode = dentry->d_inode;
a12a1ac7
JL
912 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
913 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
914 struct cifsTconInfo *tcon = cifs_sb->tcon;
3270958b
SF
915 __u32 dosattr, origattr;
916 FILE_BASIC_INFO *info_buf = NULL;
a12a1ac7
JL
917
918 rc = CIFSSMBOpen(xid, tcon, full_path, FILE_OPEN,
dd1db2de 919 DELETE|FILE_WRITE_ATTRIBUTES, CREATE_NOT_DIR,
a12a1ac7
JL
920 &netfid, &oplock, NULL, cifs_sb->local_nls,
921 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
922 if (rc != 0)
923 goto out;
924
3270958b
SF
925 origattr = cifsInode->cifsAttrs;
926 if (origattr == 0)
927 origattr |= ATTR_NORMAL;
928
929 dosattr = origattr & ~ATTR_READONLY;
a12a1ac7
JL
930 if (dosattr == 0)
931 dosattr |= ATTR_NORMAL;
932 dosattr |= ATTR_HIDDEN;
933
3270958b
SF
934 /* set ATTR_HIDDEN and clear ATTR_READONLY, but only if needed */
935 if (dosattr != origattr) {
936 info_buf = kzalloc(sizeof(*info_buf), GFP_KERNEL);
937 if (info_buf == NULL) {
938 rc = -ENOMEM;
939 goto out_close;
940 }
941 info_buf->Attributes = cpu_to_le32(dosattr);
942 rc = CIFSSMBSetFileInfo(xid, tcon, info_buf, netfid,
943 current->tgid);
944 /* although we would like to mark the file hidden
945 if that fails we will still try to rename it */
41346098 946 if (rc != 0)
3270958b
SF
947 cifsInode->cifsAttrs = dosattr;
948 else
949 dosattr = origattr; /* since not able to change them */
a12a1ac7 950 }
a12a1ac7 951
dd1db2de
JL
952 /* rename the file */
953 rc = CIFSSMBRenameOpenFile(xid, tcon, netfid, NULL, cifs_sb->local_nls,
a12a1ac7
JL
954 cifs_sb->mnt_cifs_flags &
955 CIFS_MOUNT_MAP_SPECIAL_CHR);
3270958b
SF
956 if (rc != 0) {
957 rc = -ETXTBSY;
958 goto undo_setattr;
959 }
6d22f098 960
3270958b
SF
961 /* try to set DELETE_ON_CLOSE */
962 if (!cifsInode->delete_pending) {
963 rc = CIFSSMBSetFileDisposition(xid, tcon, true, netfid,
964 current->tgid);
965 /*
966 * some samba versions return -ENOENT when we try to set the
967 * file disposition here. Likely a samba bug, but work around
968 * it for now. This means that some cifsXXX files may hang
969 * around after they shouldn't.
970 *
971 * BB: remove this hack after more servers have the fix
972 */
973 if (rc == -ENOENT)
974 rc = 0;
975 else if (rc != 0) {
976 rc = -ETXTBSY;
977 goto undo_rename;
978 }
979 cifsInode->delete_pending = true;
980 }
7ce86d5a 981
a12a1ac7
JL
982out_close:
983 CIFSSMBClose(xid, tcon, netfid);
984out:
3270958b 985 kfree(info_buf);
a12a1ac7 986 return rc;
3270958b
SF
987
988 /*
989 * reset everything back to the original state. Don't bother
990 * dealing with errors here since we can't do anything about
991 * them anyway.
992 */
993undo_rename:
994 CIFSSMBRenameOpenFile(xid, tcon, netfid, dentry->d_name.name,
995 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
996 CIFS_MOUNT_MAP_SPECIAL_CHR);
997undo_setattr:
998 if (dosattr != origattr) {
999 info_buf->Attributes = cpu_to_le32(origattr);
1000 if (!CIFSSMBSetFileInfo(xid, tcon, info_buf, netfid,
1001 current->tgid))
1002 cifsInode->cifsAttrs = origattr;
1003 }
1004
1005 goto out_close;
a12a1ac7
JL
1006}
1007
ff694527
SF
1008
1009/*
1010 * If dentry->d_inode is null (usually meaning the cached dentry
1011 * is a negative dentry) then we would attempt a standard SMB delete, but
1012 * if that fails we can not attempt the fall back mechanisms on EACESS
1013 * but will return the EACESS to the caller. Note that the VFS does not call
1014 * unlink on negative dentries currently.
1015 */
5f0319a7 1016int cifs_unlink(struct inode *dir, struct dentry *dentry)
1da177e4
LT
1017{
1018 int rc = 0;
1019 int xid;
1da177e4 1020 char *full_path = NULL;
5f0319a7 1021 struct inode *inode = dentry->d_inode;
ff694527 1022 struct cifsInodeInfo *cifs_inode;
5f0319a7
JL
1023 struct super_block *sb = dir->i_sb;
1024 struct cifs_sb_info *cifs_sb = CIFS_SB(sb);
1025 struct cifsTconInfo *tcon = cifs_sb->tcon;
6050247d
SF
1026 struct iattr *attrs = NULL;
1027 __u32 dosattr = 0, origattr = 0;
1da177e4 1028
5f0319a7 1029 cFYI(1, ("cifs_unlink, dir=0x%p, dentry=0x%p", dir, dentry));
1da177e4
LT
1030
1031 xid = GetXid();
1032
5f0319a7
JL
1033 /* Unlink can be called from rename so we can not take the
1034 * sb->s_vfs_rename_mutex here */
1035 full_path = build_path_from_dentry(dentry);
1da177e4 1036 if (full_path == NULL) {
0f3bc09e 1037 rc = -ENOMEM;
1da177e4 1038 FreeXid(xid);
0f3bc09e 1039 return rc;
1da177e4 1040 }
2d785a50 1041
5f0319a7 1042 if ((tcon->ses->capabilities & CAP_UNIX) &&
2d785a50 1043 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
5f0319a7
JL
1044 le64_to_cpu(tcon->fsUnixInfo.Capability))) {
1045 rc = CIFSPOSIXDelFile(xid, tcon, full_path,
2d785a50 1046 SMB_POSIX_UNLINK_FILE_TARGET, cifs_sb->local_nls,
737b758c 1047 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
2d785a50
SF
1048 cFYI(1, ("posix del rc %d", rc));
1049 if ((rc == 0) || (rc == -ENOENT))
1050 goto psx_del_no_retry;
1051 }
1da177e4 1052
6050247d 1053retry_std_delete:
5f0319a7 1054 rc = CIFSSMBDelFile(xid, tcon, full_path, cifs_sb->local_nls,
2d785a50 1055 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
6050247d 1056
2d785a50 1057psx_del_no_retry:
1da177e4 1058 if (!rc) {
5f0319a7
JL
1059 if (inode)
1060 drop_nlink(inode);
1da177e4 1061 } else if (rc == -ENOENT) {
5f0319a7 1062 d_drop(dentry);
1da177e4 1063 } else if (rc == -ETXTBSY) {
3270958b 1064 rc = cifs_rename_pending_delete(full_path, dentry, xid);
a12a1ac7
JL
1065 if (rc == 0)
1066 drop_nlink(inode);
ff694527 1067 } else if ((rc == -EACCES) && (dosattr == 0) && inode) {
388e57b2
SF
1068 attrs = kzalloc(sizeof(*attrs), GFP_KERNEL);
1069 if (attrs == NULL) {
1070 rc = -ENOMEM;
1071 goto out_reval;
1da177e4 1072 }
388e57b2
SF
1073
1074 /* try to reset dos attributes */
ff694527
SF
1075 cifs_inode = CIFS_I(inode);
1076 origattr = cifs_inode->cifsAttrs;
6050247d
SF
1077 if (origattr == 0)
1078 origattr |= ATTR_NORMAL;
1079 dosattr = origattr & ~ATTR_READONLY;
388e57b2
SF
1080 if (dosattr == 0)
1081 dosattr |= ATTR_NORMAL;
1082 dosattr |= ATTR_HIDDEN;
1083
1084 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
388e57b2
SF
1085 if (rc != 0)
1086 goto out_reval;
6050247d
SF
1087
1088 goto retry_std_delete;
1da177e4 1089 }
6050247d
SF
1090
1091 /* undo the setattr if we errored out and it's needed */
1092 if (rc != 0 && dosattr != 0)
1093 cifs_set_file_info(inode, attrs, xid, full_path, origattr);
1094
388e57b2 1095out_reval:
4523cc30 1096 if (inode) {
ff694527
SF
1097 cifs_inode = CIFS_I(inode);
1098 cifs_inode->time = 0; /* will force revalidate to get info
5f0319a7
JL
1099 when needed */
1100 inode->i_ctime = current_fs_time(sb);
06bcfedd 1101 }
5f0319a7 1102 dir->i_ctime = dir->i_mtime = current_fs_time(sb);
ff694527 1103 cifs_inode = CIFS_I(dir);
6050247d 1104 CIFS_I(dir)->time = 0; /* force revalidate of dir as well */
1da177e4
LT
1105
1106 kfree(full_path);
6050247d 1107 kfree(attrs);
1da177e4
LT
1108 FreeXid(xid);
1109 return rc;
1110}
1111
1112int cifs_mkdir(struct inode *inode, struct dentry *direntry, int mode)
1113{
6b37faa1 1114 int rc = 0, tmprc;
1da177e4
LT
1115 int xid;
1116 struct cifs_sb_info *cifs_sb;
1117 struct cifsTconInfo *pTcon;
1118 char *full_path = NULL;
1119 struct inode *newinode = NULL;
cc0bad75 1120 struct cifs_fattr fattr;
1da177e4 1121
6473a559 1122 cFYI(1, ("In cifs_mkdir, mode = 0x%x inode = 0x%p", mode, inode));
1da177e4
LT
1123
1124 xid = GetXid();
1125
1126 cifs_sb = CIFS_SB(inode->i_sb);
1127 pTcon = cifs_sb->tcon;
1128
7f57356b 1129 full_path = build_path_from_dentry(direntry);
1da177e4 1130 if (full_path == NULL) {
0f3bc09e 1131 rc = -ENOMEM;
1da177e4 1132 FreeXid(xid);
0f3bc09e 1133 return rc;
1da177e4 1134 }
50c2f753 1135
fb8c4b14
SF
1136 if ((pTcon->ses->capabilities & CAP_UNIX) &&
1137 (CIFS_UNIX_POSIX_PATH_OPS_CAP &
2dd29d31
SF
1138 le64_to_cpu(pTcon->fsUnixInfo.Capability))) {
1139 u32 oplock = 0;
f6d09982 1140 FILE_UNIX_BASIC_INFO *pInfo =
2dd29d31 1141 kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL);
fb8c4b14 1142 if (pInfo == NULL) {
2dd29d31
SF
1143 rc = -ENOMEM;
1144 goto mkdir_out;
1145 }
50c2f753 1146
ce3b0f8d 1147 mode &= ~current_umask();
2dd29d31
SF
1148 rc = CIFSPOSIXCreate(xid, pTcon, SMB_O_DIRECTORY | SMB_O_CREAT,
1149 mode, NULL /* netfid */, pInfo, &oplock,
fb8c4b14
SF
1150 full_path, cifs_sb->local_nls,
1151 cifs_sb->mnt_cifs_flags &
2dd29d31 1152 CIFS_MOUNT_MAP_SPECIAL_CHR);
c45d707f
SF
1153 if (rc == -EOPNOTSUPP) {
1154 kfree(pInfo);
1155 goto mkdir_retry_old;
1156 } else if (rc) {
2dd29d31
SF
1157 cFYI(1, ("posix mkdir returned 0x%x", rc));
1158 d_drop(direntry);
1159 } else {
8f2376ad
CG
1160 if (pInfo->Type == cpu_to_le32(-1)) {
1161 /* no return info, go query for it */
5a07cdf8 1162 kfree(pInfo);
fb8c4b14 1163 goto mkdir_get_info;
5a07cdf8 1164 }
fb8c4b14
SF
1165/*BB check (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID ) to see if need
1166 to set uid/gid */
2dd29d31
SF
1167 inc_nlink(inode);
1168 if (pTcon->nocase)
1169 direntry->d_op = &cifs_ci_dentry_ops;
1170 else
1171 direntry->d_op = &cifs_dentry_ops;
cbac3cba 1172
cc0bad75
JL
1173 cifs_unix_basic_to_fattr(&fattr, pInfo, cifs_sb);
1174 newinode = cifs_iget(inode->i_sb, &fattr);
1175 if (!newinode) {
5a07cdf8 1176 kfree(pInfo);
cbac3cba 1177 goto mkdir_get_info;
5a07cdf8 1178 }
6b37faa1 1179
2dd29d31 1180 d_instantiate(direntry, newinode);
cbac3cba 1181
cbac3cba 1182#ifdef CONFIG_CIFS_DEBUG2
fb8c4b14 1183 cFYI(1, ("instantiated dentry %p %s to inode %p",
cbac3cba
SF
1184 direntry, direntry->d_name.name, newinode));
1185
fb8c4b14
SF
1186 if (newinode->i_nlink != 2)
1187 cFYI(1, ("unexpected number of links %d",
cbac3cba
SF
1188 newinode->i_nlink));
1189#endif
2dd29d31
SF
1190 }
1191 kfree(pInfo);
1192 goto mkdir_out;
fb8c4b14 1193 }
c45d707f 1194mkdir_retry_old:
1da177e4 1195 /* BB add setting the equivalent of mode via CreateX w/ACLs */
737b758c
SF
1196 rc = CIFSSMBMkDir(xid, pTcon, full_path, cifs_sb->local_nls,
1197 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4 1198 if (rc) {
26a21b98 1199 cFYI(1, ("cifs_mkdir returned 0x%x", rc));
1da177e4
LT
1200 d_drop(direntry);
1201 } else {
fb8c4b14 1202mkdir_get_info:
d8c76e6f 1203 inc_nlink(inode);
c18c842b 1204 if (pTcon->unix_ext)
1da177e4 1205 rc = cifs_get_inode_info_unix(&newinode, full_path,
fb8c4b14 1206 inode->i_sb, xid);
1da177e4
LT
1207 else
1208 rc = cifs_get_inode_info(&newinode, full_path, NULL,
8b1327f6 1209 inode->i_sb, xid, NULL);
1da177e4 1210
b92327fe
SF
1211 if (pTcon->nocase)
1212 direntry->d_op = &cifs_ci_dentry_ops;
1213 else
1214 direntry->d_op = &cifs_dentry_ops;
1da177e4 1215 d_instantiate(direntry, newinode);
2dd29d31 1216 /* setting nlink not necessary except in cases where we
fb8c4b14 1217 * failed to get it from the server or was set bogus */
2dd29d31 1218 if ((direntry->d_inode) && (direntry->d_inode->i_nlink < 2))
fb8c4b14 1219 direntry->d_inode->i_nlink = 2;
95089910 1220
ce3b0f8d 1221 mode &= ~current_umask();
95089910
JL
1222 /* must turn on setgid bit if parent dir has it */
1223 if (inode->i_mode & S_ISGID)
1224 mode |= S_ISGID;
1225
c18c842b 1226 if (pTcon->unix_ext) {
4e1e7fb9
JL
1227 struct cifs_unix_set_info_args args = {
1228 .mode = mode,
1229 .ctime = NO_CHANGE_64,
1230 .atime = NO_CHANGE_64,
1231 .mtime = NO_CHANGE_64,
1232 .device = 0,
1233 };
d0d2f2df 1234 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID) {
a001e5b5 1235 args.uid = (__u64)current_fsuid();
95089910
JL
1236 if (inode->i_mode & S_ISGID)
1237 args.gid = (__u64)inode->i_gid;
1238 else
a001e5b5 1239 args.gid = (__u64)current_fsgid();
1da177e4 1240 } else {
4e1e7fb9
JL
1241 args.uid = NO_CHANGE_64;
1242 args.gid = NO_CHANGE_64;
1da177e4 1243 }
01ea95e3
JL
1244 CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, &args,
1245 cifs_sb->local_nls,
1246 cifs_sb->mnt_cifs_flags &
1247 CIFS_MOUNT_MAP_SPECIAL_CHR);
3ce53fc4 1248 } else {
67750fb9
JL
1249 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL) &&
1250 (mode & S_IWUGO) == 0) {
1251 FILE_BASIC_INFO pInfo;
6b37faa1
JL
1252 struct cifsInodeInfo *cifsInode;
1253 u32 dosattrs;
1254
67750fb9 1255 memset(&pInfo, 0, sizeof(pInfo));
6b37faa1
JL
1256 cifsInode = CIFS_I(newinode);
1257 dosattrs = cifsInode->cifsAttrs|ATTR_READONLY;
1258 pInfo.Attributes = cpu_to_le32(dosattrs);
1259 tmprc = CIFSSMBSetPathInfo(xid, pTcon,
1260 full_path, &pInfo,
1261 cifs_sb->local_nls,
67750fb9
JL
1262 cifs_sb->mnt_cifs_flags &
1263 CIFS_MOUNT_MAP_SPECIAL_CHR);
6b37faa1
JL
1264 if (tmprc == 0)
1265 cifsInode->cifsAttrs = dosattrs;
67750fb9 1266 }
fb8c4b14 1267 if (direntry->d_inode) {
b0fd30d3
JL
1268 if (cifs_sb->mnt_cifs_flags &
1269 CIFS_MOUNT_DYNPERM)
1270 direntry->d_inode->i_mode =
1271 (mode | S_IFDIR);
4e94a105 1272
fb8c4b14 1273 if (cifs_sb->mnt_cifs_flags &
6473a559 1274 CIFS_MOUNT_SET_UID) {
fb8c4b14 1275 direntry->d_inode->i_uid =
a001e5b5 1276 current_fsuid();
95089910
JL
1277 if (inode->i_mode & S_ISGID)
1278 direntry->d_inode->i_gid =
1279 inode->i_gid;
1280 else
1281 direntry->d_inode->i_gid =
a001e5b5 1282 current_fsgid();
6473a559
SF
1283 }
1284 }
2a138ebb 1285 }
1da177e4 1286 }
fb8c4b14 1287mkdir_out:
1da177e4
LT
1288 kfree(full_path);
1289 FreeXid(xid);
1290 return rc;
1291}
1292
1293int cifs_rmdir(struct inode *inode, struct dentry *direntry)
1294{
1295 int rc = 0;
1296 int xid;
1297 struct cifs_sb_info *cifs_sb;
1298 struct cifsTconInfo *pTcon;
1299 char *full_path = NULL;
1300 struct cifsInodeInfo *cifsInode;
1301
26a21b98 1302 cFYI(1, ("cifs_rmdir, inode = 0x%p", inode));
1da177e4
LT
1303
1304 xid = GetXid();
1305
1306 cifs_sb = CIFS_SB(inode->i_sb);
1307 pTcon = cifs_sb->tcon;
1308
7f57356b 1309 full_path = build_path_from_dentry(direntry);
1da177e4 1310 if (full_path == NULL) {
0f3bc09e 1311 rc = -ENOMEM;
1da177e4 1312 FreeXid(xid);
0f3bc09e 1313 return rc;
1da177e4
LT
1314 }
1315
737b758c
SF
1316 rc = CIFSSMBRmDir(xid, pTcon, full_path, cifs_sb->local_nls,
1317 cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MAP_SPECIAL_CHR);
1da177e4
LT
1318
1319 if (!rc) {
9a53c3a7 1320 drop_nlink(inode);
3677db10 1321 spin_lock(&direntry->d_inode->i_lock);
fb8c4b14 1322 i_size_write(direntry->d_inode, 0);
ce71ec36 1323 clear_nlink(direntry->d_inode);
3677db10 1324 spin_unlock(&direntry->d_inode->i_lock);
1da177e4
LT
1325 }
1326
1327 cifsInode = CIFS_I(direntry->d_inode);
1328 cifsInode->time = 0; /* force revalidate to go get info when
1329 needed */
42c24544
SF
1330
1331 cifsInode = CIFS_I(inode);
1332 cifsInode->time = 0; /* force revalidate to get parent dir info
1333 since cached search results now invalid */
1334
1da177e4
LT
1335 direntry->d_inode->i_ctime = inode->i_ctime = inode->i_mtime =
1336 current_fs_time(inode->i_sb);
1337
1338 kfree(full_path);
1339 FreeXid(xid);
1340 return rc;
1341}
1342
ee2fd967
SF
1343static int
1344cifs_do_rename(int xid, struct dentry *from_dentry, const char *fromPath,
1345 struct dentry *to_dentry, const char *toPath)
1346{
1347 struct cifs_sb_info *cifs_sb = CIFS_SB(from_dentry->d_sb);
1348 struct cifsTconInfo *pTcon = cifs_sb->tcon;
1349 __u16 srcfid;
1350 int oplock, rc;
1351
1352 /* try path-based rename first */
1353 rc = CIFSSMBRename(xid, pTcon, fromPath, toPath, cifs_sb->local_nls,
1354 cifs_sb->mnt_cifs_flags &
1355 CIFS_MOUNT_MAP_SPECIAL_CHR);
1356
1357 /*
1358 * don't bother with rename by filehandle unless file is busy and
1359 * source Note that cross directory moves do not work with
1360 * rename by filehandle to various Windows servers.
1361 */
1362 if (rc == 0 || rc != -ETXTBSY)
1363 return rc;
1364
1365 /* open the file to be renamed -- we need DELETE perms */
1366 rc = CIFSSMBOpen(xid, pTcon, fromPath, FILE_OPEN, DELETE,
1367 CREATE_NOT_DIR, &srcfid, &oplock, NULL,
1368 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
1369 CIFS_MOUNT_MAP_SPECIAL_CHR);
1370
1371 if (rc == 0) {
1372 rc = CIFSSMBRenameOpenFile(xid, pTcon, srcfid,
1373 (const char *) to_dentry->d_name.name,
1374 cifs_sb->local_nls, cifs_sb->mnt_cifs_flags &
1375 CIFS_MOUNT_MAP_SPECIAL_CHR);
1376
1377 CIFSSMBClose(xid, pTcon, srcfid);
1378 }
1379
1380 return rc;
1381}
1382
14121bdc
JL
1383int cifs_rename(struct inode *source_dir, struct dentry *source_dentry,
1384 struct inode *target_dir, struct dentry *target_dentry)
1da177e4 1385{
ee2fd967
SF
1386 char *fromName = NULL;
1387 char *toName = NULL;
1da177e4
LT
1388 struct cifs_sb_info *cifs_sb_source;
1389 struct cifs_sb_info *cifs_sb_target;
14121bdc 1390 struct cifsTconInfo *tcon;
ee2fd967
SF
1391 FILE_UNIX_BASIC_INFO *info_buf_source = NULL;
1392 FILE_UNIX_BASIC_INFO *info_buf_target;
8d281efb 1393 int xid, rc, tmprc;
1da177e4 1394
14121bdc
JL
1395 cifs_sb_target = CIFS_SB(target_dir->i_sb);
1396 cifs_sb_source = CIFS_SB(source_dir->i_sb);
1397 tcon = cifs_sb_source->tcon;
1da177e4 1398
ee2fd967
SF
1399 xid = GetXid();
1400
1401 /*
1402 * BB: this might be allowed if same server, but different share.
1403 * Consider adding support for this
1404 */
14121bdc 1405 if (tcon != cifs_sb_target->tcon) {
ee2fd967
SF
1406 rc = -EXDEV;
1407 goto cifs_rename_exit;
1da177e4
LT
1408 }
1409
ee2fd967
SF
1410 /*
1411 * we already have the rename sem so we do not need to
1412 * grab it again here to protect the path integrity
1413 */
14121bdc 1414 fromName = build_path_from_dentry(source_dentry);
ee2fd967
SF
1415 if (fromName == NULL) {
1416 rc = -ENOMEM;
1417 goto cifs_rename_exit;
1418 }
1419
14121bdc 1420 toName = build_path_from_dentry(target_dentry);
ee2fd967 1421 if (toName == NULL) {
1da177e4
LT
1422 rc = -ENOMEM;
1423 goto cifs_rename_exit;
1424 }
1425
14121bdc
JL
1426 rc = cifs_do_rename(xid, source_dentry, fromName,
1427 target_dentry, toName);
ee2fd967 1428
14121bdc
JL
1429 if (rc == -EEXIST && tcon->unix_ext) {
1430 /*
1431 * Are src and dst hardlinks of same inode? We can
1432 * only tell with unix extensions enabled
1433 */
1434 info_buf_source =
1435 kmalloc(2 * sizeof(FILE_UNIX_BASIC_INFO),
1436 GFP_KERNEL);
1437 if (info_buf_source == NULL) {
1438 rc = -ENOMEM;
1439 goto cifs_rename_exit;
1440 }
1441
1442 info_buf_target = info_buf_source + 1;
8d281efb 1443 tmprc = CIFSSMBUnixQPathInfo(xid, tcon, fromName,
14121bdc
JL
1444 info_buf_source,
1445 cifs_sb_source->local_nls,
1446 cifs_sb_source->mnt_cifs_flags &
1447 CIFS_MOUNT_MAP_SPECIAL_CHR);
8d281efb 1448 if (tmprc != 0)
14121bdc 1449 goto unlink_target;
ee2fd967 1450
8d281efb 1451 tmprc = CIFSSMBUnixQPathInfo(xid, tcon,
14121bdc
JL
1452 toName, info_buf_target,
1453 cifs_sb_target->local_nls,
1454 /* remap based on source sb */
1455 cifs_sb_source->mnt_cifs_flags &
1456 CIFS_MOUNT_MAP_SPECIAL_CHR);
1457
8d281efb 1458 if (tmprc == 0 && (info_buf_source->UniqueId ==
ae6884a9 1459 info_buf_target->UniqueId)) {
14121bdc 1460 /* same file, POSIX says that this is a noop */
ae6884a9 1461 rc = 0;
14121bdc 1462 goto cifs_rename_exit;
ae6884a9 1463 }
14121bdc 1464 } /* else ... BB we could add the same check for Windows by
ee2fd967 1465 checking the UniqueId via FILE_INTERNAL_INFO */
14121bdc 1466
ee2fd967 1467unlink_target:
fc6f3943
JL
1468 /* Try unlinking the target dentry if it's not negative */
1469 if (target_dentry->d_inode && (rc == -EACCES || rc == -EEXIST)) {
8d281efb 1470 tmprc = cifs_unlink(target_dir, target_dentry);
14121bdc
JL
1471 if (tmprc)
1472 goto cifs_rename_exit;
1473
14121bdc
JL
1474 rc = cifs_do_rename(xid, source_dentry, fromName,
1475 target_dentry, toName);
1da177e4
LT
1476 }
1477
1478cifs_rename_exit:
ee2fd967 1479 kfree(info_buf_source);
1da177e4
LT
1480 kfree(fromName);
1481 kfree(toName);
1482 FreeXid(xid);
1483 return rc;
1484}
1485
1486int cifs_revalidate(struct dentry *direntry)
1487{
1488 int xid;
cea21805 1489 int rc = 0, wbrc = 0;
1da177e4
LT
1490 char *full_path;
1491 struct cifs_sb_info *cifs_sb;
1492 struct cifsInodeInfo *cifsInode;
1493 loff_t local_size;
1494 struct timespec local_mtime;
4b18f2a9 1495 bool invalidate_inode = false;
1da177e4
LT
1496
1497 if (direntry->d_inode == NULL)
1498 return -ENOENT;
1499
1500 cifsInode = CIFS_I(direntry->d_inode);
1501
1502 if (cifsInode == NULL)
1503 return -ENOENT;
1504
1505 /* no sense revalidating inode info on file that no one can write */
1506 if (CIFS_I(direntry->d_inode)->clientCanCacheRead)
1507 return rc;
1508
1509 xid = GetXid();
1510
1511 cifs_sb = CIFS_SB(direntry->d_sb);
1512
1513 /* can not safely grab the rename sem here if rename calls revalidate
1514 since that would deadlock */
7f57356b 1515 full_path = build_path_from_dentry(direntry);
1da177e4 1516 if (full_path == NULL) {
0f3bc09e 1517 rc = -ENOMEM;
1da177e4 1518 FreeXid(xid);
0f3bc09e 1519 return rc;
1da177e4
LT
1520 }
1521 cFYI(1, ("Revalidate: %s inode 0x%p count %d dentry: 0x%p d_time %ld "
1522 "jiffies %ld", full_path, direntry->d_inode,
1523 direntry->d_inode->i_count.counter, direntry,
1524 direntry->d_time, jiffies));
1525
1526 if (cifsInode->time == 0) {
1527 /* was set to zero previously to force revalidate */
1528 } else if (time_before(jiffies, cifsInode->time + HZ) &&
1529 lookupCacheEnabled) {
1530 if ((S_ISREG(direntry->d_inode->i_mode) == 0) ||
1531 (direntry->d_inode->i_nlink == 1)) {
1532 kfree(full_path);
1533 FreeXid(xid);
1534 return rc;
1535 } else {
1536 cFYI(1, ("Have to revalidate file due to hardlinks"));
1537 }
1538 }
1539
1540 /* save mtime and size */
1541 local_mtime = direntry->d_inode->i_mtime;
1542 local_size = direntry->d_inode->i_size;
1543
c18c842b 1544 if (cifs_sb->tcon->unix_ext) {
1da177e4 1545 rc = cifs_get_inode_info_unix(&direntry->d_inode, full_path,
fb8c4b14 1546 direntry->d_sb, xid);
1da177e4
LT
1547 if (rc) {
1548 cFYI(1, ("error on getting revalidate info %d", rc));
1549/* if (rc != -ENOENT)
1550 rc = 0; */ /* BB should we cache info on
1551 certain errors? */
1552 }
1553 } else {
1554 rc = cifs_get_inode_info(&direntry->d_inode, full_path, NULL,
8b1327f6 1555 direntry->d_sb, xid, NULL);
1da177e4
LT
1556 if (rc) {
1557 cFYI(1, ("error on getting revalidate info %d", rc));
1558/* if (rc != -ENOENT)
1559 rc = 0; */ /* BB should we cache info on
1560 certain errors? */
1561 }
1562 }
1563 /* should we remap certain errors, access denied?, to zero */
1564
1565 /* if not oplocked, we invalidate inode pages if mtime or file size
1566 had changed on server */
1567
fb8c4b14 1568 if (timespec_equal(&local_mtime, &direntry->d_inode->i_mtime) &&
1da177e4
LT
1569 (local_size == direntry->d_inode->i_size)) {
1570 cFYI(1, ("cifs_revalidate - inode unchanged"));
1571 } else {
1572 /* file may have changed on server */
1573 if (cifsInode->clientCanCacheRead) {
1574 /* no need to invalidate inode pages since we were the
1575 only ones who could have modified the file and the
1576 server copy is staler than ours */
1577 } else {
4b18f2a9 1578 invalidate_inode = true;
1da177e4
LT
1579 }
1580 }
1581
1582 /* can not grab this sem since kernel filesys locking documentation
1b1dcc1b
JS
1583 indicates i_mutex may be taken by the kernel on lookup and rename
1584 which could deadlock if we grab the i_mutex here as well */
1585/* mutex_lock(&direntry->d_inode->i_mutex);*/
1da177e4
LT
1586 /* need to write out dirty pages here */
1587 if (direntry->d_inode->i_mapping) {
1588 /* do we need to lock inode until after invalidate completes
1589 below? */
cea21805
JL
1590 wbrc = filemap_fdatawrite(direntry->d_inode->i_mapping);
1591 if (wbrc)
1592 CIFS_I(direntry->d_inode)->write_behind_rc = wbrc;
1da177e4
LT
1593 }
1594 if (invalidate_inode) {
3abb9272
SF
1595 /* shrink_dcache not necessary now that cifs dentry ops
1596 are exported for negative dentries */
fb8c4b14 1597/* if (S_ISDIR(direntry->d_inode->i_mode))
3abb9272
SF
1598 shrink_dcache_parent(direntry); */
1599 if (S_ISREG(direntry->d_inode->i_mode)) {
9e96af85 1600 if (direntry->d_inode->i_mapping) {
cea21805
JL
1601 wbrc = filemap_fdatawait(direntry->d_inode->i_mapping);
1602 if (wbrc)
1603 CIFS_I(direntry->d_inode)->write_behind_rc = wbrc;
9e96af85 1604 }
3abb9272
SF
1605 /* may eventually have to do this for open files too */
1606 if (list_empty(&(cifsInode->openFileList))) {
1607 /* changed on server - flush read ahead pages */
1608 cFYI(1, ("Invalidating read ahead data on "
1609 "closed file"));
1610 invalidate_remote_inode(direntry->d_inode);
1611 }
1da177e4
LT
1612 }
1613 }
1b1dcc1b 1614/* mutex_unlock(&direntry->d_inode->i_mutex); */
50c2f753 1615
1da177e4
LT
1616 kfree(full_path);
1617 FreeXid(xid);
1618 return rc;
1619}
1620
1621int cifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1622 struct kstat *stat)
1623{
1624 int err = cifs_revalidate(dentry);
5fe14c85 1625 if (!err) {
1da177e4 1626 generic_fillattr(dentry->d_inode, stat);
5fe14c85 1627 stat->blksize = CIFS_MAX_MSGSIZE;
cc0bad75 1628 stat->ino = CIFS_I(dentry->d_inode)->uniqueid;
5fe14c85 1629 }
1da177e4
LT
1630 return err;
1631}
1632
1633static int cifs_truncate_page(struct address_space *mapping, loff_t from)
1634{
1635 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1636 unsigned offset = from & (PAGE_CACHE_SIZE - 1);
1637 struct page *page;
1da177e4
LT
1638 int rc = 0;
1639
1640 page = grab_cache_page(mapping, index);
1641 if (!page)
1642 return -ENOMEM;
1643
eebd2aa3 1644 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
1da177e4
LT
1645 unlock_page(page);
1646 page_cache_release(page);
1647 return rc;
1648}
1649
fb8c4b14 1650static int cifs_vmtruncate(struct inode *inode, loff_t offset)
3677db10
SF
1651{
1652 struct address_space *mapping = inode->i_mapping;
1653 unsigned long limit;
1654
ba6a46a0 1655 spin_lock(&inode->i_lock);
3677db10
SF
1656 if (inode->i_size < offset)
1657 goto do_expand;
1658 /*
1659 * truncation of in-use swapfiles is disallowed - it would cause
1660 * subsequent swapout to scribble on the now-freed blocks.
1661 */
ba6a46a0
SF
1662 if (IS_SWAPFILE(inode)) {
1663 spin_unlock(&inode->i_lock);
3677db10 1664 goto out_busy;
ba6a46a0 1665 }
3677db10
SF
1666 i_size_write(inode, offset);
1667 spin_unlock(&inode->i_lock);
8064ab4d
SF
1668 /*
1669 * unmap_mapping_range is called twice, first simply for efficiency
1670 * so that truncate_inode_pages does fewer single-page unmaps. However
1671 * after this first call, and before truncate_inode_pages finishes,
1672 * it is possible for private pages to be COWed, which remain after
1673 * truncate_inode_pages finishes, hence the second unmap_mapping_range
1674 * call must be made for correctness.
1675 */
3677db10
SF
1676 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
1677 truncate_inode_pages(mapping, offset);
8064ab4d 1678 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
3677db10
SF
1679 goto out_truncate;
1680
1681do_expand:
1682 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
ba6a46a0
SF
1683 if (limit != RLIM_INFINITY && offset > limit) {
1684 spin_unlock(&inode->i_lock);
3677db10 1685 goto out_sig;
ba6a46a0
SF
1686 }
1687 if (offset > inode->i_sb->s_maxbytes) {
1688 spin_unlock(&inode->i_lock);
3677db10 1689 goto out_big;
ba6a46a0 1690 }
3677db10 1691 i_size_write(inode, offset);
ba6a46a0 1692 spin_unlock(&inode->i_lock);
3677db10 1693out_truncate:
acfa4380 1694 if (inode->i_op->truncate)
3677db10
SF
1695 inode->i_op->truncate(inode);
1696 return 0;
1697out_sig:
1698 send_sig(SIGXFSZ, current, 0);
1699out_big:
1700 return -EFBIG;
1701out_busy:
1702 return -ETXTBSY;
1703}
1704
8efdbde6
JL
1705static int
1706cifs_set_file_size(struct inode *inode, struct iattr *attrs,
1707 int xid, char *full_path)
1708{
1709 int rc;
1710 struct cifsFileInfo *open_file;
1711 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1712 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1713 struct cifsTconInfo *pTcon = cifs_sb->tcon;
1714
1715 /*
1716 * To avoid spurious oplock breaks from server, in the case of
1717 * inodes that we already have open, avoid doing path based
1718 * setting of file size if we can do it by handle.
1719 * This keeps our caching token (oplock) and avoids timeouts
1720 * when the local oplock break takes longer to flush
1721 * writebehind data than the SMB timeout for the SetPathInfo
1722 * request would allow
1723 */
1724 open_file = find_writable_file(cifsInode);
1725 if (open_file) {
1726 __u16 nfid = open_file->netfid;
1727 __u32 npid = open_file->pid;
1728 rc = CIFSSMBSetFileSize(xid, pTcon, attrs->ia_size, nfid,
1729 npid, false);
1730 atomic_dec(&open_file->wrtPending);
1731 cFYI(1, ("SetFSize for attrs rc = %d", rc));
1732 if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
1733 unsigned int bytes_written;
1734 rc = CIFSSMBWrite(xid, pTcon, nfid, 0, attrs->ia_size,
1735 &bytes_written, NULL, NULL, 1);
1736 cFYI(1, ("Wrt seteof rc %d", rc));
1737 }
1738 } else
1739 rc = -EINVAL;
1740
1741 if (rc != 0) {
1742 /* Set file size by pathname rather than by handle
1743 either because no valid, writeable file handle for
1744 it was found or because there was an error setting
1745 it by handle */
1746 rc = CIFSSMBSetEOF(xid, pTcon, full_path, attrs->ia_size,
1747 false, cifs_sb->local_nls,
1748 cifs_sb->mnt_cifs_flags &
1749 CIFS_MOUNT_MAP_SPECIAL_CHR);
1750 cFYI(1, ("SetEOF by path (setattrs) rc = %d", rc));
1751 if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) {
1752 __u16 netfid;
1753 int oplock = 0;
1754
1755 rc = SMBLegacyOpen(xid, pTcon, full_path,
1756 FILE_OPEN, GENERIC_WRITE,
1757 CREATE_NOT_DIR, &netfid, &oplock, NULL,
1758 cifs_sb->local_nls,
1759 cifs_sb->mnt_cifs_flags &
1760 CIFS_MOUNT_MAP_SPECIAL_CHR);
1761 if (rc == 0) {
1762 unsigned int bytes_written;
1763 rc = CIFSSMBWrite(xid, pTcon, netfid, 0,
1764 attrs->ia_size,
1765 &bytes_written, NULL,
1766 NULL, 1);
1767 cFYI(1, ("wrt seteof rc %d", rc));
1768 CIFSSMBClose(xid, pTcon, netfid);
1769 }
1770 }
1771 }
1772
1773 if (rc == 0) {
fbec9ab9 1774 cifsInode->server_eof = attrs->ia_size;
8efdbde6
JL
1775 rc = cifs_vmtruncate(inode, attrs->ia_size);
1776 cifs_truncate_page(inode->i_mapping, inode->i_size);
1777 }
1778
1779 return rc;
1780}
1781
3fe5c1dd
JL
1782static int
1783cifs_setattr_unix(struct dentry *direntry, struct iattr *attrs)
1784{
1785 int rc;
1786 int xid;
1787 char *full_path = NULL;
1788 struct inode *inode = direntry->d_inode;
1789 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1790 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
1791 struct cifsTconInfo *pTcon = cifs_sb->tcon;
1792 struct cifs_unix_set_info_args *args = NULL;
3bbeeb3c 1793 struct cifsFileInfo *open_file;
3fe5c1dd
JL
1794
1795 cFYI(1, ("setattr_unix on file %s attrs->ia_valid=0x%x",
1796 direntry->d_name.name, attrs->ia_valid));
1797
1798 xid = GetXid();
1799
1800 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) {
1801 /* check if we have permission to change attrs */
1802 rc = inode_change_ok(inode, attrs);
1803 if (rc < 0)
1804 goto out;
1805 else
1806 rc = 0;
1807 }
1808
1809 full_path = build_path_from_dentry(direntry);
1810 if (full_path == NULL) {
1811 rc = -ENOMEM;
1812 goto out;
1813 }
1814
0f4d634c
JL
1815 /*
1816 * Attempt to flush data before changing attributes. We need to do
1817 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
1818 * ownership or mode then we may also need to do this. Here, we take
1819 * the safe way out and just do the flush on all setattr requests. If
1820 * the flush returns error, store it to report later and continue.
1821 *
1822 * BB: This should be smarter. Why bother flushing pages that
1823 * will be truncated anyway? Also, should we error out here if
1824 * the flush returns error?
1825 */
1826 rc = filemap_write_and_wait(inode->i_mapping);
1827 if (rc != 0) {
1828 cifsInode->write_behind_rc = rc;
1829 rc = 0;
3fe5c1dd
JL
1830 }
1831
1832 if (attrs->ia_valid & ATTR_SIZE) {
1833 rc = cifs_set_file_size(inode, attrs, xid, full_path);
1834 if (rc != 0)
1835 goto out;
1836 }
1837
1838 /* skip mode change if it's just for clearing setuid/setgid */
1839 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
1840 attrs->ia_valid &= ~ATTR_MODE;
1841
1842 args = kmalloc(sizeof(*args), GFP_KERNEL);
1843 if (args == NULL) {
1844 rc = -ENOMEM;
1845 goto out;
1846 }
1847
1848 /* set up the struct */
1849 if (attrs->ia_valid & ATTR_MODE)
1850 args->mode = attrs->ia_mode;
1851 else
1852 args->mode = NO_CHANGE_64;
1853
1854 if (attrs->ia_valid & ATTR_UID)
1855 args->uid = attrs->ia_uid;
1856 else
1857 args->uid = NO_CHANGE_64;
1858
1859 if (attrs->ia_valid & ATTR_GID)
1860 args->gid = attrs->ia_gid;
1861 else
1862 args->gid = NO_CHANGE_64;
1863
1864 if (attrs->ia_valid & ATTR_ATIME)
1865 args->atime = cifs_UnixTimeToNT(attrs->ia_atime);
1866 else
1867 args->atime = NO_CHANGE_64;
1868
1869 if (attrs->ia_valid & ATTR_MTIME)
1870 args->mtime = cifs_UnixTimeToNT(attrs->ia_mtime);
1871 else
1872 args->mtime = NO_CHANGE_64;
1873
1874 if (attrs->ia_valid & ATTR_CTIME)
1875 args->ctime = cifs_UnixTimeToNT(attrs->ia_ctime);
1876 else
1877 args->ctime = NO_CHANGE_64;
1878
1879 args->device = 0;
3bbeeb3c
JL
1880 open_file = find_writable_file(cifsInode);
1881 if (open_file) {
1882 u16 nfid = open_file->netfid;
1883 u32 npid = open_file->pid;
1884 rc = CIFSSMBUnixSetFileInfo(xid, pTcon, args, nfid, npid);
1885 atomic_dec(&open_file->wrtPending);
1886 } else {
1887 rc = CIFSSMBUnixSetPathInfo(xid, pTcon, full_path, args,
01ea95e3
JL
1888 cifs_sb->local_nls,
1889 cifs_sb->mnt_cifs_flags &
1890 CIFS_MOUNT_MAP_SPECIAL_CHR);
3bbeeb3c 1891 }
3fe5c1dd
JL
1892
1893 if (!rc)
1894 rc = inode_setattr(inode, attrs);
1895out:
1896 kfree(args);
1897 kfree(full_path);
1898 FreeXid(xid);
1899 return rc;
1900}
1901
0510eeb7
JL
1902static int
1903cifs_setattr_nounix(struct dentry *direntry, struct iattr *attrs)
1da177e4
LT
1904{
1905 int xid;
3fe5c1dd
JL
1906 struct inode *inode = direntry->d_inode;
1907 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3fe5c1dd 1908 struct cifsInodeInfo *cifsInode = CIFS_I(inode);
1da177e4
LT
1909 char *full_path = NULL;
1910 int rc = -EACCES;
feb3e20c 1911 __u32 dosattr = 0;
4e1e7fb9 1912 __u64 mode = NO_CHANGE_64;
3fe5c1dd 1913
1da177e4
LT
1914 xid = GetXid();
1915
3979877e 1916 cFYI(1, ("setattr on file %s attrs->iavalid 0x%x",
1da177e4 1917 direntry->d_name.name, attrs->ia_valid));
6473a559 1918
2a138ebb 1919 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_PERM) == 0) {
6473a559 1920 /* check if we have permission to change attrs */
02eadeff 1921 rc = inode_change_ok(inode, attrs);
fb8c4b14 1922 if (rc < 0) {
6473a559
SF
1923 FreeXid(xid);
1924 return rc;
1925 } else
1926 rc = 0;
1927 }
50c2f753 1928
7f57356b 1929 full_path = build_path_from_dentry(direntry);
1da177e4 1930 if (full_path == NULL) {
0f3bc09e 1931 rc = -ENOMEM;
1da177e4 1932 FreeXid(xid);
0f3bc09e 1933 return rc;
1da177e4 1934 }
1da177e4 1935
0f4d634c
JL
1936 /*
1937 * Attempt to flush data before changing attributes. We need to do
1938 * this for ATTR_SIZE and ATTR_MTIME for sure, and if we change the
1939 * ownership or mode then we may also need to do this. Here, we take
1940 * the safe way out and just do the flush on all setattr requests. If
1941 * the flush returns error, store it to report later and continue.
1942 *
1943 * BB: This should be smarter. Why bother flushing pages that
1944 * will be truncated anyway? Also, should we error out here if
1945 * the flush returns error?
1946 */
1947 rc = filemap_write_and_wait(inode->i_mapping);
1948 if (rc != 0) {
1949 cifsInode->write_behind_rc = rc;
1950 rc = 0;
50531444 1951 }
cea21805 1952
50531444 1953 if (attrs->ia_valid & ATTR_SIZE) {
8efdbde6
JL
1954 rc = cifs_set_file_size(inode, attrs, xid, full_path);
1955 if (rc != 0)
e30dcf3a 1956 goto cifs_setattr_exit;
1da177e4 1957 }
4ca691a8
JL
1958
1959 /*
1960 * Without unix extensions we can't send ownership changes to the
1961 * server, so silently ignore them. This is consistent with how
1962 * local DOS/Windows filesystems behave (VFAT, NTFS, etc). With
1963 * CIFSACL support + proper Windows to Unix idmapping, we may be
1964 * able to support this in the future.
1965 */
3fe5c1dd 1966 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_SET_UID))
4ca691a8 1967 attrs->ia_valid &= ~(ATTR_UID | ATTR_GID);
1da177e4 1968
d32c4f26
JL
1969 /* skip mode change if it's just for clearing setuid/setgid */
1970 if (attrs->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
1971 attrs->ia_valid &= ~ATTR_MODE;
1972
1da177e4 1973 if (attrs->ia_valid & ATTR_MODE) {
5132861a 1974 cFYI(1, ("Mode changed to 0%o", attrs->ia_mode));
1da177e4 1975 mode = attrs->ia_mode;
1da177e4
LT
1976 }
1977
3fe5c1dd 1978 if (attrs->ia_valid & ATTR_MODE) {
cdbce9c8 1979 rc = 0;
97837582
SF
1980#ifdef CONFIG_CIFS_EXPERIMENTAL
1981 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_CIFS_ACL)
02eadeff 1982 rc = mode_to_acl(inode, full_path, mode);
5132861a 1983 else
97837582 1984#endif
5132861a
JL
1985 if (((mode & S_IWUGO) == 0) &&
1986 (cifsInode->cifsAttrs & ATTR_READONLY) == 0) {
feb3e20c
JL
1987
1988 dosattr = cifsInode->cifsAttrs | ATTR_READONLY;
1989
5132861a
JL
1990 /* fix up mode if we're not using dynperm */
1991 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM) == 0)
1992 attrs->ia_mode = inode->i_mode & ~S_IWUGO;
1993 } else if ((mode & S_IWUGO) &&
1994 (cifsInode->cifsAttrs & ATTR_READONLY)) {
feb3e20c
JL
1995
1996 dosattr = cifsInode->cifsAttrs & ~ATTR_READONLY;
1997 /* Attributes of 0 are ignored */
1998 if (dosattr == 0)
1999 dosattr |= ATTR_NORMAL;
5132861a
JL
2000
2001 /* reset local inode permissions to normal */
2002 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2003 attrs->ia_mode &= ~(S_IALLUGO);
2004 if (S_ISDIR(inode->i_mode))
2005 attrs->ia_mode |=
2006 cifs_sb->mnt_dir_mode;
2007 else
2008 attrs->ia_mode |=
2009 cifs_sb->mnt_file_mode;
2010 }
2011 } else if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_DYNPERM)) {
2012 /* ignore mode change - ATTR_READONLY hasn't changed */
2013 attrs->ia_valid &= ~ATTR_MODE;
1da177e4 2014 }
1da177e4
LT
2015 }
2016
feb3e20c
JL
2017 if (attrs->ia_valid & (ATTR_MTIME|ATTR_ATIME|ATTR_CTIME) ||
2018 ((attrs->ia_valid & ATTR_MODE) && dosattr)) {
2019 rc = cifs_set_file_info(inode, attrs, xid, full_path, dosattr);
2020 /* BB: check for rc = -EOPNOTSUPP and switch to legacy mode */
1da177e4 2021
e30dcf3a
SF
2022 /* Even if error on time set, no sense failing the call if
2023 the server would set the time to a reasonable value anyway,
2024 and this check ensures that we are not being called from
2025 sys_utimes in which case we ought to fail the call back to
2026 the user when the server rejects the call */
fb8c4b14 2027 if ((rc) && (attrs->ia_valid &
feb3e20c 2028 (ATTR_MODE | ATTR_GID | ATTR_UID | ATTR_SIZE)))
e30dcf3a 2029 rc = 0;
1da177e4
LT
2030 }
2031
2032 /* do not need local check to inode_check_ok since the server does
2033 that */
2034 if (!rc)
02eadeff 2035 rc = inode_setattr(inode, attrs);
e30dcf3a 2036cifs_setattr_exit:
1da177e4
LT
2037 kfree(full_path);
2038 FreeXid(xid);
2039 return rc;
2040}
2041
0510eeb7
JL
2042int
2043cifs_setattr(struct dentry *direntry, struct iattr *attrs)
2044{
2045 struct inode *inode = direntry->d_inode;
2046 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2047 struct cifsTconInfo *pTcon = cifs_sb->tcon;
2048
2049 if (pTcon->unix_ext)
2050 return cifs_setattr_unix(direntry, attrs);
2051
2052 return cifs_setattr_nounix(direntry, attrs);
2053
2054 /* BB: add cifs_setattr_legacy for really old servers */
2055}
2056
99ee4dbd 2057#if 0
1da177e4
LT
2058void cifs_delete_inode(struct inode *inode)
2059{
26a21b98 2060 cFYI(1, ("In cifs_delete_inode, inode = 0x%p", inode));
1da177e4
LT
2061 /* may have to add back in if and when safe distributed caching of
2062 directories added e.g. via FindNotify */
2063}
99ee4dbd 2064#endif