diff options
Diffstat (limited to 'fs/cifs/file.c')
-rw-r--r-- | fs/cifs/file.c | 1900 |
1 files changed, 981 insertions, 919 deletions
diff --git a/fs/cifs/file.c b/fs/cifs/file.c index de748c652d11..a9b4a24f2a16 100644 --- a/fs/cifs/file.c +++ b/fs/cifs/file.c | |||
@@ -60,34 +60,32 @@ static inline int cifs_convert_flags(unsigned int flags) | |||
60 | FILE_READ_DATA); | 60 | FILE_READ_DATA); |
61 | } | 61 | } |
62 | 62 | ||
63 | static inline fmode_t cifs_posix_convert_flags(unsigned int flags) | 63 | static u32 cifs_posix_convert_flags(unsigned int flags) |
64 | { | 64 | { |
65 | fmode_t posix_flags = 0; | 65 | u32 posix_flags = 0; |
66 | 66 | ||
67 | if ((flags & O_ACCMODE) == O_RDONLY) | 67 | if ((flags & O_ACCMODE) == O_RDONLY) |
68 | posix_flags = FMODE_READ; | 68 | posix_flags = SMB_O_RDONLY; |
69 | else if ((flags & O_ACCMODE) == O_WRONLY) | 69 | else if ((flags & O_ACCMODE) == O_WRONLY) |
70 | posix_flags = FMODE_WRITE; | 70 | posix_flags = SMB_O_WRONLY; |
71 | else if ((flags & O_ACCMODE) == O_RDWR) { | 71 | else if ((flags & O_ACCMODE) == O_RDWR) |
72 | /* GENERIC_ALL is too much permission to request | 72 | posix_flags = SMB_O_RDWR; |
73 | can cause unnecessary access denied on create */ | 73 | |
74 | /* return GENERIC_ALL; */ | 74 | if (flags & O_CREAT) |
75 | posix_flags = FMODE_READ | FMODE_WRITE; | 75 | posix_flags |= SMB_O_CREAT; |
76 | } | 76 | if (flags & O_EXCL) |
77 | /* can not map O_CREAT or O_EXCL or O_TRUNC flags when | 77 | posix_flags |= SMB_O_EXCL; |
78 | reopening a file. They had their effect on the original open */ | 78 | if (flags & O_TRUNC) |
79 | if (flags & O_APPEND) | 79 | posix_flags |= SMB_O_TRUNC; |
80 | posix_flags |= (fmode_t)O_APPEND; | 80 | /* be safe and imply O_SYNC for O_DSYNC */ |
81 | if (flags & O_DSYNC) | 81 | if (flags & O_DSYNC) |
82 | posix_flags |= (fmode_t)O_DSYNC; | 82 | posix_flags |= SMB_O_SYNC; |
83 | if (flags & __O_SYNC) | ||
84 | posix_flags |= (fmode_t)__O_SYNC; | ||
85 | if (flags & O_DIRECTORY) | 83 | if (flags & O_DIRECTORY) |
86 | posix_flags |= (fmode_t)O_DIRECTORY; | 84 | posix_flags |= SMB_O_DIRECTORY; |
87 | if (flags & O_NOFOLLOW) | 85 | if (flags & O_NOFOLLOW) |
88 | posix_flags |= (fmode_t)O_NOFOLLOW; | 86 | posix_flags |= SMB_O_NOFOLLOW; |
89 | if (flags & O_DIRECT) | 87 | if (flags & O_DIRECT) |
90 | posix_flags |= (fmode_t)O_DIRECT; | 88 | posix_flags |= SMB_O_DIRECT; |
91 | 89 | ||
92 | return posix_flags; | 90 | return posix_flags; |
93 | } | 91 | } |
@@ -106,138 +104,261 @@ static inline int cifs_get_disposition(unsigned int flags) | |||
106 | return FILE_OPEN; | 104 | return FILE_OPEN; |
107 | } | 105 | } |
108 | 106 | ||
109 | /* all arguments to this function must be checked for validity in caller */ | 107 | int cifs_posix_open(char *full_path, struct inode **pinode, |
110 | static inline int | 108 | struct super_block *sb, int mode, unsigned int f_flags, |
111 | cifs_posix_open_inode_helper(struct inode *inode, struct file *file, | 109 | __u32 *poplock, __u16 *pnetfid, int xid) |
112 | struct cifsInodeInfo *pCifsInode, __u32 oplock, | ||
113 | u16 netfid) | ||
114 | { | 110 | { |
111 | int rc; | ||
112 | FILE_UNIX_BASIC_INFO *presp_data; | ||
113 | __u32 posix_flags = 0; | ||
114 | struct cifs_sb_info *cifs_sb = CIFS_SB(sb); | ||
115 | struct cifs_fattr fattr; | ||
116 | struct tcon_link *tlink; | ||
117 | struct cifs_tcon *tcon; | ||
115 | 118 | ||
116 | write_lock(&GlobalSMBSeslock); | 119 | cFYI(1, "posix open %s", full_path); |
117 | 120 | ||
118 | pCifsInode = CIFS_I(file->f_path.dentry->d_inode); | 121 | presp_data = kzalloc(sizeof(FILE_UNIX_BASIC_INFO), GFP_KERNEL); |
119 | if (pCifsInode == NULL) { | 122 | if (presp_data == NULL) |
120 | write_unlock(&GlobalSMBSeslock); | 123 | return -ENOMEM; |
121 | return -EINVAL; | ||
122 | } | ||
123 | 124 | ||
124 | if (pCifsInode->clientCanCacheRead) { | 125 | tlink = cifs_sb_tlink(cifs_sb); |
125 | /* we have the inode open somewhere else | 126 | if (IS_ERR(tlink)) { |
126 | no need to discard cache data */ | 127 | rc = PTR_ERR(tlink); |
127 | goto psx_client_can_cache; | 128 | goto posix_open_ret; |
128 | } | 129 | } |
129 | 130 | ||
130 | /* BB FIXME need to fix this check to move it earlier into posix_open | 131 | tcon = tlink_tcon(tlink); |
131 | BB fIX following section BB FIXME */ | 132 | mode &= ~current_umask(); |
132 | 133 | ||
133 | /* if not oplocked, invalidate inode pages if mtime or file | 134 | posix_flags = cifs_posix_convert_flags(f_flags); |
134 | size changed */ | 135 | rc = CIFSPOSIXCreate(xid, tcon, posix_flags, mode, pnetfid, presp_data, |
135 | /* temp = cifs_NTtimeToUnix(le64_to_cpu(buf->LastWriteTime)); | 136 | poplock, full_path, cifs_sb->local_nls, |
136 | if (timespec_equal(&file->f_path.dentry->d_inode->i_mtime, &temp) && | 137 | cifs_sb->mnt_cifs_flags & |
137 | (file->f_path.dentry->d_inode->i_size == | 138 | CIFS_MOUNT_MAP_SPECIAL_CHR); |
138 | (loff_t)le64_to_cpu(buf->EndOfFile))) { | 139 | cifs_put_tlink(tlink); |
139 | cFYI(1, "inode unchanged on server"); | 140 | |
140 | } else { | 141 | if (rc) |
141 | if (file->f_path.dentry->d_inode->i_mapping) { | 142 | goto posix_open_ret; |
142 | rc = filemap_write_and_wait(file->f_path.dentry->d_inode->i_mapping); | 143 | |
143 | if (rc != 0) | 144 | if (presp_data->Type == cpu_to_le32(-1)) |
144 | CIFS_I(file->f_path.dentry->d_inode)->write_behind_rc = rc; | 145 | goto posix_open_ret; /* open ok, caller does qpathinfo */ |
146 | |||
147 | if (!pinode) | ||
148 | goto posix_open_ret; /* caller does not need info */ | ||
149 | |||
150 | cifs_unix_basic_to_fattr(&fattr, presp_data, cifs_sb); | ||
151 | |||
152 | /* get new inode and set it up */ | ||
153 | if (*pinode == NULL) { | ||
154 | cifs_fill_uniqueid(sb, &fattr); | ||
155 | *pinode = cifs_iget(sb, &fattr); | ||
156 | if (!*pinode) { | ||
157 | rc = -ENOMEM; | ||
158 | goto posix_open_ret; | ||
145 | } | 159 | } |
146 | cFYI(1, "invalidating remote inode since open detected it " | 160 | } else { |
147 | "changed"); | 161 | cifs_fattr_to_inode(*pinode, &fattr); |
148 | invalidate_remote_inode(file->f_path.dentry->d_inode); | 162 | } |
149 | } */ | 163 | |
150 | 164 | posix_open_ret: | |
151 | psx_client_can_cache: | 165 | kfree(presp_data); |
152 | if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) { | 166 | return rc; |
153 | pCifsInode->clientCanCacheAll = true; | ||
154 | pCifsInode->clientCanCacheRead = true; | ||
155 | cFYI(1, "Exclusive Oplock granted on inode %p", | ||
156 | file->f_path.dentry->d_inode); | ||
157 | } else if ((oplock & 0xF) == OPLOCK_READ) | ||
158 | pCifsInode->clientCanCacheRead = true; | ||
159 | |||
160 | /* will have to change the unlock if we reenable the | ||
161 | filemap_fdatawrite (which does not seem necessary */ | ||
162 | write_unlock(&GlobalSMBSeslock); | ||
163 | return 0; | ||
164 | } | 167 | } |
165 | 168 | ||
166 | /* all arguments to this function must be checked for validity in caller */ | 169 | static int |
167 | static inline int cifs_open_inode_helper(struct inode *inode, | 170 | cifs_nt_open(char *full_path, struct inode *inode, struct cifs_sb_info *cifs_sb, |
168 | struct cifsTconInfo *pTcon, int *oplock, FILE_ALL_INFO *buf, | 171 | struct cifs_tcon *tcon, unsigned int f_flags, __u32 *poplock, |
169 | char *full_path, int xid) | 172 | __u16 *pnetfid, int xid) |
170 | { | 173 | { |
171 | struct cifsInodeInfo *pCifsInode = CIFS_I(inode); | ||
172 | struct timespec temp; | ||
173 | int rc; | 174 | int rc; |
175 | int desiredAccess; | ||
176 | int disposition; | ||
177 | FILE_ALL_INFO *buf; | ||
174 | 178 | ||
175 | if (pCifsInode->clientCanCacheRead) { | 179 | desiredAccess = cifs_convert_flags(f_flags); |
176 | /* we have the inode open somewhere else | ||
177 | no need to discard cache data */ | ||
178 | goto client_can_cache; | ||
179 | } | ||
180 | 180 | ||
181 | /* BB need same check in cifs_create too? */ | 181 | /********************************************************************* |
182 | /* if not oplocked, invalidate inode pages if mtime or file | 182 | * open flag mapping table: |
183 | size changed */ | 183 | * |
184 | temp = cifs_NTtimeToUnix(buf->LastWriteTime); | 184 | * POSIX Flag CIFS Disposition |
185 | if (timespec_equal(&inode->i_mtime, &temp) && | 185 | * ---------- ---------------- |
186 | (inode->i_size == | 186 | * O_CREAT FILE_OPEN_IF |
187 | (loff_t)le64_to_cpu(buf->EndOfFile))) { | 187 | * O_CREAT | O_EXCL FILE_CREATE |
188 | cFYI(1, "inode unchanged on server"); | 188 | * O_CREAT | O_TRUNC FILE_OVERWRITE_IF |
189 | } else { | 189 | * O_TRUNC FILE_OVERWRITE |
190 | if (inode->i_mapping) { | 190 | * none of the above FILE_OPEN |
191 | /* BB no need to lock inode until after invalidate | 191 | * |
192 | since namei code should already have it locked? */ | 192 | * Note that there is not a direct match between disposition |
193 | rc = filemap_write_and_wait(inode->i_mapping); | 193 | * FILE_SUPERSEDE (ie create whether or not file exists although |
194 | if (rc != 0) | 194 | * O_CREAT | O_TRUNC is similar but truncates the existing |
195 | pCifsInode->write_behind_rc = rc; | 195 | * file rather than creating a new file as FILE_SUPERSEDE does |
196 | } | 196 | * (which uses the attributes / metadata passed in on open call) |
197 | cFYI(1, "invalidating remote inode since open detected it " | 197 | *? |
198 | "changed"); | 198 | *? O_SYNC is a reasonable match to CIFS writethrough flag |
199 | invalidate_remote_inode(inode); | 199 | *? and the read write flags match reasonably. O_LARGEFILE |
200 | } | 200 | *? is irrelevant because largefile support is always used |
201 | *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY, | ||
202 | * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation | ||
203 | *********************************************************************/ | ||
204 | |||
205 | disposition = cifs_get_disposition(f_flags); | ||
206 | |||
207 | /* BB pass O_SYNC flag through on file attributes .. BB */ | ||
208 | |||
209 | buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); | ||
210 | if (!buf) | ||
211 | return -ENOMEM; | ||
212 | |||
213 | if (tcon->ses->capabilities & CAP_NT_SMBS) | ||
214 | rc = CIFSSMBOpen(xid, tcon, full_path, disposition, | ||
215 | desiredAccess, CREATE_NOT_DIR, pnetfid, poplock, buf, | ||
216 | cifs_sb->local_nls, cifs_sb->mnt_cifs_flags | ||
217 | & CIFS_MOUNT_MAP_SPECIAL_CHR); | ||
218 | else | ||
219 | rc = SMBLegacyOpen(xid, tcon, full_path, disposition, | ||
220 | desiredAccess, CREATE_NOT_DIR, pnetfid, poplock, buf, | ||
221 | cifs_sb->local_nls, cifs_sb->mnt_cifs_flags | ||
222 | & CIFS_MOUNT_MAP_SPECIAL_CHR); | ||
201 | 223 | ||
202 | client_can_cache: | 224 | if (rc) |
203 | if (pTcon->unix_ext) | 225 | goto out; |
226 | |||
227 | if (tcon->unix_ext) | ||
204 | rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb, | 228 | rc = cifs_get_inode_info_unix(&inode, full_path, inode->i_sb, |
205 | xid); | 229 | xid); |
206 | else | 230 | else |
207 | rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb, | 231 | rc = cifs_get_inode_info(&inode, full_path, buf, inode->i_sb, |
208 | xid, NULL); | 232 | xid, pnetfid); |
209 | |||
210 | if ((*oplock & 0xF) == OPLOCK_EXCLUSIVE) { | ||
211 | pCifsInode->clientCanCacheAll = true; | ||
212 | pCifsInode->clientCanCacheRead = true; | ||
213 | cFYI(1, "Exclusive Oplock granted on inode %p", inode); | ||
214 | } else if ((*oplock & 0xF) == OPLOCK_READ) | ||
215 | pCifsInode->clientCanCacheRead = true; | ||
216 | 233 | ||
234 | out: | ||
235 | kfree(buf); | ||
217 | return rc; | 236 | return rc; |
218 | } | 237 | } |
219 | 238 | ||
239 | struct cifsFileInfo * | ||
240 | cifs_new_fileinfo(__u16 fileHandle, struct file *file, | ||
241 | struct tcon_link *tlink, __u32 oplock) | ||
242 | { | ||
243 | struct dentry *dentry = file->f_path.dentry; | ||
244 | struct inode *inode = dentry->d_inode; | ||
245 | struct cifsInodeInfo *pCifsInode = CIFS_I(inode); | ||
246 | struct cifsFileInfo *pCifsFile; | ||
247 | |||
248 | pCifsFile = kzalloc(sizeof(struct cifsFileInfo), GFP_KERNEL); | ||
249 | if (pCifsFile == NULL) | ||
250 | return pCifsFile; | ||
251 | |||
252 | pCifsFile->count = 1; | ||
253 | pCifsFile->netfid = fileHandle; | ||
254 | pCifsFile->pid = current->tgid; | ||
255 | pCifsFile->uid = current_fsuid(); | ||
256 | pCifsFile->dentry = dget(dentry); | ||
257 | pCifsFile->f_flags = file->f_flags; | ||
258 | pCifsFile->invalidHandle = false; | ||
259 | pCifsFile->tlink = cifs_get_tlink(tlink); | ||
260 | mutex_init(&pCifsFile->fh_mutex); | ||
261 | mutex_init(&pCifsFile->lock_mutex); | ||
262 | INIT_LIST_HEAD(&pCifsFile->llist); | ||
263 | INIT_WORK(&pCifsFile->oplock_break, cifs_oplock_break); | ||
264 | |||
265 | spin_lock(&cifs_file_list_lock); | ||
266 | list_add(&pCifsFile->tlist, &(tlink_tcon(tlink)->openFileList)); | ||
267 | /* if readable file instance put first in list*/ | ||
268 | if (file->f_mode & FMODE_READ) | ||
269 | list_add(&pCifsFile->flist, &pCifsInode->openFileList); | ||
270 | else | ||
271 | list_add_tail(&pCifsFile->flist, &pCifsInode->openFileList); | ||
272 | spin_unlock(&cifs_file_list_lock); | ||
273 | |||
274 | cifs_set_oplock_level(pCifsInode, oplock); | ||
275 | |||
276 | file->private_data = pCifsFile; | ||
277 | return pCifsFile; | ||
278 | } | ||
279 | |||
280 | /* | ||
281 | * Release a reference on the file private data. This may involve closing | ||
282 | * the filehandle out on the server. Must be called without holding | ||
283 | * cifs_file_list_lock. | ||
284 | */ | ||
285 | void cifsFileInfo_put(struct cifsFileInfo *cifs_file) | ||
286 | { | ||
287 | struct inode *inode = cifs_file->dentry->d_inode; | ||
288 | struct cifs_tcon *tcon = tlink_tcon(cifs_file->tlink); | ||
289 | struct cifsInodeInfo *cifsi = CIFS_I(inode); | ||
290 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); | ||
291 | struct cifsLockInfo *li, *tmp; | ||
292 | |||
293 | spin_lock(&cifs_file_list_lock); | ||
294 | if (--cifs_file->count > 0) { | ||
295 | spin_unlock(&cifs_file_list_lock); | ||
296 | return; | ||
297 | } | ||
298 | |||
299 | /* remove it from the lists */ | ||
300 | list_del(&cifs_file->flist); | ||
301 | list_del(&cifs_file->tlist); | ||
302 | |||
303 | if (list_empty(&cifsi->openFileList)) { | ||
304 | cFYI(1, "closing last open instance for inode %p", | ||
305 | cifs_file->dentry->d_inode); | ||
306 | |||
307 | /* in strict cache mode we need invalidate mapping on the last | ||
308 | close because it may cause a error when we open this file | ||
309 | again and get at least level II oplock */ | ||
310 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_STRICT_IO) | ||
311 | CIFS_I(inode)->invalid_mapping = true; | ||
312 | |||
313 | cifs_set_oplock_level(cifsi, 0); | ||
314 | } | ||
315 | spin_unlock(&cifs_file_list_lock); | ||
316 | |||
317 | if (!tcon->need_reconnect && !cifs_file->invalidHandle) { | ||
318 | int xid, rc; | ||
319 | |||
320 | xid = GetXid(); | ||
321 | rc = CIFSSMBClose(xid, tcon, cifs_file->netfid); | ||
322 | FreeXid(xid); | ||
323 | } | ||
324 | |||
325 | /* Delete any outstanding lock records. We'll lose them when the file | ||
326 | * is closed anyway. | ||
327 | */ | ||
328 | mutex_lock(&cifs_file->lock_mutex); | ||
329 | list_for_each_entry_safe(li, tmp, &cifs_file->llist, llist) { | ||
330 | list_del(&li->llist); | ||
331 | kfree(li); | ||
332 | } | ||
333 | mutex_unlock(&cifs_file->lock_mutex); | ||
334 | |||
335 | cifs_put_tlink(cifs_file->tlink); | ||
336 | dput(cifs_file->dentry); | ||
337 | kfree(cifs_file); | ||
338 | } | ||
339 | |||
220 | int cifs_open(struct inode *inode, struct file *file) | 340 | int cifs_open(struct inode *inode, struct file *file) |
221 | { | 341 | { |
222 | int rc = -EACCES; | 342 | int rc = -EACCES; |
223 | int xid; | 343 | int xid; |
224 | __u32 oplock; | 344 | __u32 oplock; |
225 | struct cifs_sb_info *cifs_sb; | 345 | struct cifs_sb_info *cifs_sb; |
226 | struct cifsTconInfo *tcon; | 346 | struct cifs_tcon *tcon; |
347 | struct tcon_link *tlink; | ||
227 | struct cifsFileInfo *pCifsFile = NULL; | 348 | struct cifsFileInfo *pCifsFile = NULL; |
228 | struct cifsInodeInfo *pCifsInode; | ||
229 | char *full_path = NULL; | 349 | char *full_path = NULL; |
230 | int desiredAccess; | 350 | bool posix_open_ok = false; |
231 | int disposition; | ||
232 | __u16 netfid; | 351 | __u16 netfid; |
233 | FILE_ALL_INFO *buf = NULL; | ||
234 | 352 | ||
235 | xid = GetXid(); | 353 | xid = GetXid(); |
236 | 354 | ||
237 | cifs_sb = CIFS_SB(inode->i_sb); | 355 | cifs_sb = CIFS_SB(inode->i_sb); |
238 | tcon = cifs_sb->tcon; | 356 | tlink = cifs_sb_tlink(cifs_sb); |
239 | 357 | if (IS_ERR(tlink)) { | |
240 | pCifsInode = CIFS_I(file->f_path.dentry->d_inode); | 358 | FreeXid(xid); |
359 | return PTR_ERR(tlink); | ||
360 | } | ||
361 | tcon = tlink_tcon(tlink); | ||
241 | 362 | ||
242 | full_path = build_path_from_dentry(file->f_path.dentry); | 363 | full_path = build_path_from_dentry(file->f_path.dentry); |
243 | if (full_path == NULL) { | 364 | if (full_path == NULL) { |
@@ -257,35 +378,13 @@ int cifs_open(struct inode *inode, struct file *file) | |||
257 | (tcon->ses->capabilities & CAP_UNIX) && | 378 | (tcon->ses->capabilities & CAP_UNIX) && |
258 | (CIFS_UNIX_POSIX_PATH_OPS_CAP & | 379 | (CIFS_UNIX_POSIX_PATH_OPS_CAP & |
259 | le64_to_cpu(tcon->fsUnixInfo.Capability))) { | 380 | le64_to_cpu(tcon->fsUnixInfo.Capability))) { |
260 | int oflags = (int) cifs_posix_convert_flags(file->f_flags); | ||
261 | oflags |= SMB_O_CREAT; | ||
262 | /* can not refresh inode info since size could be stale */ | 381 | /* can not refresh inode info since size could be stale */ |
263 | rc = cifs_posix_open(full_path, &inode, inode->i_sb, | 382 | rc = cifs_posix_open(full_path, &inode, inode->i_sb, |
264 | cifs_sb->mnt_file_mode /* ignored */, | 383 | cifs_sb->mnt_file_mode /* ignored */, |
265 | oflags, &oplock, &netfid, xid); | 384 | file->f_flags, &oplock, &netfid, xid); |
266 | if (rc == 0) { | 385 | if (rc == 0) { |
267 | cFYI(1, "posix open succeeded"); | 386 | cFYI(1, "posix open succeeded"); |
268 | /* no need for special case handling of setting mode | 387 | posix_open_ok = true; |
269 | on read only files needed here */ | ||
270 | |||
271 | rc = cifs_posix_open_inode_helper(inode, file, | ||
272 | pCifsInode, oplock, netfid); | ||
273 | if (rc != 0) { | ||
274 | CIFSSMBClose(xid, tcon, netfid); | ||
275 | goto out; | ||
276 | } | ||
277 | |||
278 | pCifsFile = cifs_new_fileinfo(inode, netfid, file, | ||
279 | file->f_path.mnt, | ||
280 | oflags); | ||
281 | if (pCifsFile == NULL) { | ||
282 | CIFSSMBClose(xid, tcon, netfid); | ||
283 | rc = -ENOMEM; | ||
284 | } | ||
285 | |||
286 | cifs_fscache_set_inode_cookie(inode, file); | ||
287 | |||
288 | goto out; | ||
289 | } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) { | 388 | } else if ((rc == -EINVAL) || (rc == -EOPNOTSUPP)) { |
290 | if (tcon->ses->serverNOS) | 389 | if (tcon->ses->serverNOS) |
291 | cERROR(1, "server %s of type %s returned" | 390 | cERROR(1, "server %s of type %s returned" |
@@ -302,106 +401,42 @@ int cifs_open(struct inode *inode, struct file *file) | |||
302 | or DFS errors */ | 401 | or DFS errors */ |
303 | } | 402 | } |
304 | 403 | ||
305 | desiredAccess = cifs_convert_flags(file->f_flags); | 404 | if (!posix_open_ok) { |
306 | 405 | rc = cifs_nt_open(full_path, inode, cifs_sb, tcon, | |
307 | /********************************************************************* | 406 | file->f_flags, &oplock, &netfid, xid); |
308 | * open flag mapping table: | 407 | if (rc) |
309 | * | 408 | goto out; |
310 | * POSIX Flag CIFS Disposition | ||
311 | * ---------- ---------------- | ||
312 | * O_CREAT FILE_OPEN_IF | ||
313 | * O_CREAT | O_EXCL FILE_CREATE | ||
314 | * O_CREAT | O_TRUNC FILE_OVERWRITE_IF | ||
315 | * O_TRUNC FILE_OVERWRITE | ||
316 | * none of the above FILE_OPEN | ||
317 | * | ||
318 | * Note that there is not a direct match between disposition | ||
319 | * FILE_SUPERSEDE (ie create whether or not file exists although | ||
320 | * O_CREAT | O_TRUNC is similar but truncates the existing | ||
321 | * file rather than creating a new file as FILE_SUPERSEDE does | ||
322 | * (which uses the attributes / metadata passed in on open call) | ||
323 | *? | ||
324 | *? O_SYNC is a reasonable match to CIFS writethrough flag | ||
325 | *? and the read write flags match reasonably. O_LARGEFILE | ||
326 | *? is irrelevant because largefile support is always used | ||
327 | *? by this client. Flags O_APPEND, O_DIRECT, O_DIRECTORY, | ||
328 | * O_FASYNC, O_NOFOLLOW, O_NONBLOCK need further investigation | ||
329 | *********************************************************************/ | ||
330 | |||
331 | disposition = cifs_get_disposition(file->f_flags); | ||
332 | |||
333 | /* BB pass O_SYNC flag through on file attributes .. BB */ | ||
334 | |||
335 | /* Also refresh inode by passing in file_info buf returned by SMBOpen | ||
336 | and calling get_inode_info with returned buf (at least helps | ||
337 | non-Unix server case) */ | ||
338 | |||
339 | /* BB we can not do this if this is the second open of a file | ||
340 | and the first handle has writebehind data, we might be | ||
341 | able to simply do a filemap_fdatawrite/filemap_fdatawait first */ | ||
342 | buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL); | ||
343 | if (!buf) { | ||
344 | rc = -ENOMEM; | ||
345 | goto out; | ||
346 | } | ||
347 | |||
348 | if (cifs_sb->tcon->ses->capabilities & CAP_NT_SMBS) | ||
349 | rc = CIFSSMBOpen(xid, tcon, full_path, disposition, | ||
350 | desiredAccess, CREATE_NOT_DIR, &netfid, &oplock, buf, | ||
351 | cifs_sb->local_nls, cifs_sb->mnt_cifs_flags | ||
352 | & CIFS_MOUNT_MAP_SPECIAL_CHR); | ||
353 | else | ||
354 | rc = -EIO; /* no NT SMB support fall into legacy open below */ | ||
355 | |||
356 | if (rc == -EIO) { | ||
357 | /* Old server, try legacy style OpenX */ | ||
358 | rc = SMBLegacyOpen(xid, tcon, full_path, disposition, | ||
359 | desiredAccess, CREATE_NOT_DIR, &netfid, &oplock, buf, | ||
360 | cifs_sb->local_nls, cifs_sb->mnt_cifs_flags | ||
361 | & CIFS_MOUNT_MAP_SPECIAL_CHR); | ||
362 | } | ||
363 | if (rc) { | ||
364 | cFYI(1, "cifs_open returned 0x%x", rc); | ||
365 | goto out; | ||
366 | } | 409 | } |
367 | 410 | ||
368 | rc = cifs_open_inode_helper(inode, tcon, &oplock, buf, full_path, xid); | 411 | pCifsFile = cifs_new_fileinfo(netfid, file, tlink, oplock); |
369 | if (rc != 0) | ||
370 | goto out; | ||
371 | |||
372 | pCifsFile = cifs_new_fileinfo(inode, netfid, file, file->f_path.mnt, | ||
373 | file->f_flags); | ||
374 | if (pCifsFile == NULL) { | 412 | if (pCifsFile == NULL) { |
413 | CIFSSMBClose(xid, tcon, netfid); | ||
375 | rc = -ENOMEM; | 414 | rc = -ENOMEM; |
376 | goto out; | 415 | goto out; |
377 | } | 416 | } |
378 | 417 | ||
379 | cifs_fscache_set_inode_cookie(inode, file); | 418 | cifs_fscache_set_inode_cookie(inode, file); |
380 | 419 | ||
381 | if (oplock & CIFS_CREATE_ACTION) { | 420 | if ((oplock & CIFS_CREATE_ACTION) && !posix_open_ok && tcon->unix_ext) { |
382 | /* time to set mode which we can not set earlier due to | 421 | /* time to set mode which we can not set earlier due to |
383 | problems creating new read-only files */ | 422 | problems creating new read-only files */ |
384 | if (tcon->unix_ext) { | 423 | struct cifs_unix_set_info_args args = { |
385 | struct cifs_unix_set_info_args args = { | 424 | .mode = inode->i_mode, |
386 | .mode = inode->i_mode, | 425 | .uid = NO_CHANGE_64, |
387 | .uid = NO_CHANGE_64, | 426 | .gid = NO_CHANGE_64, |
388 | .gid = NO_CHANGE_64, | 427 | .ctime = NO_CHANGE_64, |
389 | .ctime = NO_CHANGE_64, | 428 | .atime = NO_CHANGE_64, |
390 | .atime = NO_CHANGE_64, | 429 | .mtime = NO_CHANGE_64, |
391 | .mtime = NO_CHANGE_64, | 430 | .device = 0, |
392 | .device = 0, | 431 | }; |
393 | }; | 432 | CIFSSMBUnixSetFileInfo(xid, tcon, &args, netfid, |
394 | CIFSSMBUnixSetPathInfo(xid, tcon, full_path, &args, | 433 | pCifsFile->pid); |
395 | cifs_sb->local_nls, | ||
396 | cifs_sb->mnt_cifs_flags & | ||
397 | CIFS_MOUNT_MAP_SPECIAL_CHR); | ||
398 | } | ||
399 | } | 434 | } |
400 | 435 | ||
401 | out: | 436 | out: |
402 | kfree(buf); | ||
403 | kfree(full_path); | 437 | kfree(full_path); |
404 | FreeXid(xid); | 438 | FreeXid(xid); |
439 | cifs_put_tlink(tlink); | ||
405 | return rc; | 440 | return rc; |
406 | } | 441 | } |
407 | 442 | ||
@@ -416,14 +451,13 @@ static int cifs_relock_file(struct cifsFileInfo *cifsFile) | |||
416 | return rc; | 451 | return rc; |
417 | } | 452 | } |
418 | 453 | ||
419 | static int cifs_reopen_file(struct file *file, bool can_flush) | 454 | static int cifs_reopen_file(struct cifsFileInfo *pCifsFile, bool can_flush) |
420 | { | 455 | { |
421 | int rc = -EACCES; | 456 | int rc = -EACCES; |
422 | int xid; | 457 | int xid; |
423 | __u32 oplock; | 458 | __u32 oplock; |
424 | struct cifs_sb_info *cifs_sb; | 459 | struct cifs_sb_info *cifs_sb; |
425 | struct cifsTconInfo *tcon; | 460 | struct cifs_tcon *tcon; |
426 | struct cifsFileInfo *pCifsFile; | ||
427 | struct cifsInodeInfo *pCifsInode; | 461 | struct cifsInodeInfo *pCifsInode; |
428 | struct inode *inode; | 462 | struct inode *inode; |
429 | char *full_path = NULL; | 463 | char *full_path = NULL; |
@@ -431,11 +465,6 @@ static int cifs_reopen_file(struct file *file, bool can_flush) | |||
431 | int disposition = FILE_OPEN; | 465 | int disposition = FILE_OPEN; |
432 | __u16 netfid; | 466 | __u16 netfid; |
433 | 467 | ||
434 | if (file->private_data) | ||
435 | pCifsFile = file->private_data; | ||
436 | else | ||
437 | return -EBADF; | ||
438 | |||
439 | xid = GetXid(); | 468 | xid = GetXid(); |
440 | mutex_lock(&pCifsFile->fh_mutex); | 469 | mutex_lock(&pCifsFile->fh_mutex); |
441 | if (!pCifsFile->invalidHandle) { | 470 | if (!pCifsFile->invalidHandle) { |
@@ -445,39 +474,24 @@ static int cifs_reopen_file(struct file *file, bool can_flush) | |||
445 | return rc; | 474 | return rc; |
446 | } | 475 | } |
447 | 476 | ||
448 | if (file->f_path.dentry == NULL) { | 477 | inode = pCifsFile->dentry->d_inode; |
449 | cERROR(1, "no valid name if dentry freed"); | ||
450 | dump_stack(); | ||
451 | rc = -EBADF; | ||
452 | goto reopen_error_exit; | ||
453 | } | ||
454 | |||
455 | inode = file->f_path.dentry->d_inode; | ||
456 | if (inode == NULL) { | ||
457 | cERROR(1, "inode not valid"); | ||
458 | dump_stack(); | ||
459 | rc = -EBADF; | ||
460 | goto reopen_error_exit; | ||
461 | } | ||
462 | |||
463 | cifs_sb = CIFS_SB(inode->i_sb); | 478 | cifs_sb = CIFS_SB(inode->i_sb); |
464 | tcon = cifs_sb->tcon; | 479 | tcon = tlink_tcon(pCifsFile->tlink); |
465 | 480 | ||
466 | /* can not grab rename sem here because various ops, including | 481 | /* can not grab rename sem here because various ops, including |
467 | those that already have the rename sem can end up causing writepage | 482 | those that already have the rename sem can end up causing writepage |
468 | to get called and if the server was down that means we end up here, | 483 | to get called and if the server was down that means we end up here, |
469 | and we can never tell if the caller already has the rename_sem */ | 484 | and we can never tell if the caller already has the rename_sem */ |
470 | full_path = build_path_from_dentry(file->f_path.dentry); | 485 | full_path = build_path_from_dentry(pCifsFile->dentry); |
471 | if (full_path == NULL) { | 486 | if (full_path == NULL) { |
472 | rc = -ENOMEM; | 487 | rc = -ENOMEM; |
473 | reopen_error_exit: | ||
474 | mutex_unlock(&pCifsFile->fh_mutex); | 488 | mutex_unlock(&pCifsFile->fh_mutex); |
475 | FreeXid(xid); | 489 | FreeXid(xid); |
476 | return rc; | 490 | return rc; |
477 | } | 491 | } |
478 | 492 | ||
479 | cFYI(1, "inode = 0x%p file flags 0x%x for %s", | 493 | cFYI(1, "inode = 0x%p file flags 0x%x for %s", |
480 | inode, file->f_flags, full_path); | 494 | inode, pCifsFile->f_flags, full_path); |
481 | 495 | ||
482 | if (oplockEnabled) | 496 | if (oplockEnabled) |
483 | oplock = REQ_OPLOCK; | 497 | oplock = REQ_OPLOCK; |
@@ -487,8 +501,14 @@ reopen_error_exit: | |||
487 | if (tcon->unix_ext && (tcon->ses->capabilities & CAP_UNIX) && | 501 | if (tcon->unix_ext && (tcon->ses->capabilities & CAP_UNIX) && |
488 | (CIFS_UNIX_POSIX_PATH_OPS_CAP & | 502 | (CIFS_UNIX_POSIX_PATH_OPS_CAP & |
489 | le64_to_cpu(tcon->fsUnixInfo.Capability))) { | 503 | le64_to_cpu(tcon->fsUnixInfo.Capability))) { |
490 | int oflags = (int) cifs_posix_convert_flags(file->f_flags); | 504 | |
491 | /* can not refresh inode info since size could be stale */ | 505 | /* |
506 | * O_CREAT, O_EXCL and O_TRUNC already had their effect on the | ||
507 | * original open. Must mask them off for a reopen. | ||
508 | */ | ||
509 | unsigned int oflags = pCifsFile->f_flags & | ||
510 | ~(O_CREAT | O_EXCL | O_TRUNC); | ||
511 | |||
492 | rc = cifs_posix_open(full_path, NULL, inode->i_sb, | 512 | rc = cifs_posix_open(full_path, NULL, inode->i_sb, |
493 | cifs_sb->mnt_file_mode /* ignored */, | 513 | cifs_sb->mnt_file_mode /* ignored */, |
494 | oflags, &oplock, &netfid, xid); | 514 | oflags, &oplock, &netfid, xid); |
@@ -500,7 +520,7 @@ reopen_error_exit: | |||
500 | in the reconnect path it is important to retry hard */ | 520 | in the reconnect path it is important to retry hard */ |
501 | } | 521 | } |
502 | 522 | ||
503 | desiredAccess = cifs_convert_flags(file->f_flags); | 523 | desiredAccess = cifs_convert_flags(pCifsFile->f_flags); |
504 | 524 | ||
505 | /* Can not refresh inode by passing in file_info buf to be returned | 525 | /* Can not refresh inode by passing in file_info buf to be returned |
506 | by SMBOpen and then calling get_inode_info with returned buf | 526 | by SMBOpen and then calling get_inode_info with returned buf |
@@ -516,49 +536,38 @@ reopen_error_exit: | |||
516 | mutex_unlock(&pCifsFile->fh_mutex); | 536 | mutex_unlock(&pCifsFile->fh_mutex); |
517 | cFYI(1, "cifs_open returned 0x%x", rc); | 537 | cFYI(1, "cifs_open returned 0x%x", rc); |
518 | cFYI(1, "oplock: %d", oplock); | 538 | cFYI(1, "oplock: %d", oplock); |
519 | } else { | 539 | goto reopen_error_exit; |
520 | reopen_success: | ||
521 | pCifsFile->netfid = netfid; | ||
522 | pCifsFile->invalidHandle = false; | ||
523 | mutex_unlock(&pCifsFile->fh_mutex); | ||
524 | pCifsInode = CIFS_I(inode); | ||
525 | if (pCifsInode) { | ||
526 | if (can_flush) { | ||
527 | rc = filemap_write_and_wait(inode->i_mapping); | ||
528 | if (rc != 0) | ||
529 | CIFS_I(inode)->write_behind_rc = rc; | ||
530 | /* temporarily disable caching while we | ||
531 | go to server to get inode info */ | ||
532 | pCifsInode->clientCanCacheAll = false; | ||
533 | pCifsInode->clientCanCacheRead = false; | ||
534 | if (tcon->unix_ext) | ||
535 | rc = cifs_get_inode_info_unix(&inode, | ||
536 | full_path, inode->i_sb, xid); | ||
537 | else | ||
538 | rc = cifs_get_inode_info(&inode, | ||
539 | full_path, NULL, inode->i_sb, | ||
540 | xid, NULL); | ||
541 | } /* else we are writing out data to server already | ||
542 | and could deadlock if we tried to flush data, and | ||
543 | since we do not know if we have data that would | ||
544 | invalidate the current end of file on the server | ||
545 | we can not go to the server to get the new inod | ||
546 | info */ | ||
547 | if ((oplock & 0xF) == OPLOCK_EXCLUSIVE) { | ||
548 | pCifsInode->clientCanCacheAll = true; | ||
549 | pCifsInode->clientCanCacheRead = true; | ||
550 | cFYI(1, "Exclusive Oplock granted on inode %p", | ||
551 | file->f_path.dentry->d_inode); | ||
552 | } else if ((oplock & 0xF) == OPLOCK_READ) { | ||
553 | pCifsInode->clientCanCacheRead = true; | ||
554 | pCifsInode->clientCanCacheAll = false; | ||
555 | } else { | ||
556 | pCifsInode->clientCanCacheRead = false; | ||
557 | pCifsInode->clientCanCacheAll = false; | ||
558 | } | ||
559 | cifs_relock_file(pCifsFile); | ||
560 | } | ||
561 | } | 540 | } |
541 | |||
542 | reopen_success: | ||
543 | pCifsFile->netfid = netfid; | ||
544 | pCifsFile->invalidHandle = false; | ||
545 | mutex_unlock(&pCifsFile->fh_mutex); | ||
546 | pCifsInode = CIFS_I(inode); | ||
547 | |||
548 | if (can_flush) { | ||
549 | rc = filemap_write_and_wait(inode->i_mapping); | ||
550 | mapping_set_error(inode->i_mapping, rc); | ||
551 | |||
552 | if (tcon->unix_ext) | ||
553 | rc = cifs_get_inode_info_unix(&inode, | ||
554 | full_path, inode->i_sb, xid); | ||
555 | else | ||
556 | rc = cifs_get_inode_info(&inode, | ||
557 | full_path, NULL, inode->i_sb, | ||
558 | xid, NULL); | ||
559 | } /* else we are writing out data to server already | ||
560 | and could deadlock if we tried to flush data, and | ||
561 | since we do not know if we have data that would | ||
562 | invalidate the current end of file on the server | ||
563 | we can not go to the server to get the new inod | ||
564 | info */ | ||
565 | |||
566 | cifs_set_oplock_level(pCifsInode, oplock); | ||
567 | |||
568 | cifs_relock_file(pCifsFile); | ||
569 | |||
570 | reopen_error_exit: | ||
562 | kfree(full_path); | 571 | kfree(full_path); |
563 | FreeXid(xid); | 572 | FreeXid(xid); |
564 | return rc; | 573 | return rc; |
@@ -566,79 +575,13 @@ reopen_success: | |||
566 | 575 | ||
567 | int cifs_close(struct inode *inode, struct file *file) | 576 | int cifs_close(struct inode *inode, struct file *file) |
568 | { | 577 | { |
569 | int rc = 0; | 578 | if (file->private_data != NULL) { |
570 | int xid, timeout; | ||
571 | struct cifs_sb_info *cifs_sb; | ||
572 | struct cifsTconInfo *pTcon; | ||
573 | struct cifsFileInfo *pSMBFile = file->private_data; | ||
574 | |||
575 | xid = GetXid(); | ||
576 | |||
577 | cifs_sb = CIFS_SB(inode->i_sb); | ||
578 | pTcon = cifs_sb->tcon; | ||
579 | if (pSMBFile) { | ||
580 | struct cifsLockInfo *li, *tmp; | ||
581 | write_lock(&GlobalSMBSeslock); | ||
582 | pSMBFile->closePend = true; | ||
583 | if (pTcon) { | ||
584 | /* no sense reconnecting to close a file that is | ||
585 | already closed */ | ||
586 | if (!pTcon->need_reconnect) { | ||
587 | write_unlock(&GlobalSMBSeslock); | ||
588 | timeout = 2; | ||
589 | while ((atomic_read(&pSMBFile->count) != 1) | ||
590 | && (timeout <= 2048)) { | ||
591 | /* Give write a better chance to get to | ||
592 | server ahead of the close. We do not | ||
593 | want to add a wait_q here as it would | ||
594 | increase the memory utilization as | ||
595 | the struct would be in each open file, | ||
596 | but this should give enough time to | ||
597 | clear the socket */ | ||
598 | cFYI(DBG2, "close delay, write pending"); | ||
599 | msleep(timeout); | ||
600 | timeout *= 4; | ||
601 | } | ||
602 | if (!pTcon->need_reconnect && | ||
603 | !pSMBFile->invalidHandle) | ||
604 | rc = CIFSSMBClose(xid, pTcon, | ||
605 | pSMBFile->netfid); | ||
606 | } else | ||
607 | write_unlock(&GlobalSMBSeslock); | ||
608 | } else | ||
609 | write_unlock(&GlobalSMBSeslock); | ||
610 | |||
611 | /* Delete any outstanding lock records. | ||
612 | We'll lose them when the file is closed anyway. */ | ||
613 | mutex_lock(&pSMBFile->lock_mutex); | ||
614 | list_for_each_entry_safe(li, tmp, &pSMBFile->llist, llist) { | ||
615 | list_del(&li->llist); | ||
616 | kfree(li); | ||
617 | } | ||
618 | mutex_unlock(&pSMBFile->lock_mutex); | ||
619 | |||
620 | write_lock(&GlobalSMBSeslock); | ||
621 | list_del(&pSMBFile->flist); | ||
622 | list_del(&pSMBFile->tlist); | ||
623 | write_unlock(&GlobalSMBSeslock); | ||
624 | cifsFileInfo_put(file->private_data); | 579 | cifsFileInfo_put(file->private_data); |
625 | file->private_data = NULL; | 580 | file->private_data = NULL; |
626 | } else | ||
627 | rc = -EBADF; | ||
628 | |||
629 | read_lock(&GlobalSMBSeslock); | ||
630 | if (list_empty(&(CIFS_I(inode)->openFileList))) { | ||
631 | cFYI(1, "closing last open instance for inode %p", inode); | ||
632 | /* if the file is not open we do not know if we can cache info | ||
633 | on this inode, much less write behind and read ahead */ | ||
634 | CIFS_I(inode)->clientCanCacheRead = false; | ||
635 | CIFS_I(inode)->clientCanCacheAll = false; | ||
636 | } | 581 | } |
637 | read_unlock(&GlobalSMBSeslock); | 582 | |
638 | if ((rc == 0) && CIFS_I(inode)->write_behind_rc) | 583 | /* return code from the ->release op is always ignored */ |
639 | rc = CIFS_I(inode)->write_behind_rc; | 584 | return 0; |
640 | FreeXid(xid); | ||
641 | return rc; | ||
642 | } | 585 | } |
643 | 586 | ||
644 | int cifs_closedir(struct inode *inode, struct file *file) | 587 | int cifs_closedir(struct inode *inode, struct file *file) |
@@ -653,25 +596,21 @@ int cifs_closedir(struct inode *inode, struct file *file) | |||
653 | xid = GetXid(); | 596 | xid = GetXid(); |
654 | 597 | ||
655 | if (pCFileStruct) { | 598 | if (pCFileStruct) { |
656 | struct cifsTconInfo *pTcon; | 599 | struct cifs_tcon *pTcon = tlink_tcon(pCFileStruct->tlink); |
657 | struct cifs_sb_info *cifs_sb = | ||
658 | CIFS_SB(file->f_path.dentry->d_sb); | ||
659 | |||
660 | pTcon = cifs_sb->tcon; | ||
661 | 600 | ||
662 | cFYI(1, "Freeing private data in close dir"); | 601 | cFYI(1, "Freeing private data in close dir"); |
663 | write_lock(&GlobalSMBSeslock); | 602 | spin_lock(&cifs_file_list_lock); |
664 | if (!pCFileStruct->srch_inf.endOfSearch && | 603 | if (!pCFileStruct->srch_inf.endOfSearch && |
665 | !pCFileStruct->invalidHandle) { | 604 | !pCFileStruct->invalidHandle) { |
666 | pCFileStruct->invalidHandle = true; | 605 | pCFileStruct->invalidHandle = true; |
667 | write_unlock(&GlobalSMBSeslock); | 606 | spin_unlock(&cifs_file_list_lock); |
668 | rc = CIFSFindClose(xid, pTcon, pCFileStruct->netfid); | 607 | rc = CIFSFindClose(xid, pTcon, pCFileStruct->netfid); |
669 | cFYI(1, "Closing uncompleted readdir with rc %d", | 608 | cFYI(1, "Closing uncompleted readdir with rc %d", |
670 | rc); | 609 | rc); |
671 | /* not much we can do if it fails anyway, ignore rc */ | 610 | /* not much we can do if it fails anyway, ignore rc */ |
672 | rc = 0; | 611 | rc = 0; |
673 | } else | 612 | } else |
674 | write_unlock(&GlobalSMBSeslock); | 613 | spin_unlock(&cifs_file_list_lock); |
675 | ptmp = pCFileStruct->srch_inf.ntwrk_buf_start; | 614 | ptmp = pCFileStruct->srch_inf.ntwrk_buf_start; |
676 | if (ptmp) { | 615 | if (ptmp) { |
677 | cFYI(1, "closedir free smb buf in srch struct"); | 616 | cFYI(1, "closedir free smb buf in srch struct"); |
@@ -681,6 +620,7 @@ int cifs_closedir(struct inode *inode, struct file *file) | |||
681 | else | 620 | else |
682 | cifs_buf_release(ptmp); | 621 | cifs_buf_release(ptmp); |
683 | } | 622 | } |
623 | cifs_put_tlink(pCFileStruct->tlink); | ||
684 | kfree(file->private_data); | 624 | kfree(file->private_data); |
685 | file->private_data = NULL; | 625 | file->private_data = NULL; |
686 | } | 626 | } |
@@ -713,7 +653,7 @@ int cifs_lock(struct file *file, int cmd, struct file_lock *pfLock) | |||
713 | __u64 length; | 653 | __u64 length; |
714 | bool wait_flag = false; | 654 | bool wait_flag = false; |
715 | struct cifs_sb_info *cifs_sb; | 655 | struct cifs_sb_info *cifs_sb; |
716 | struct cifsTconInfo *tcon; | 656 | struct cifs_tcon *tcon; |
717 | __u16 netfid; | 657 | __u16 netfid; |
718 | __u8 lockType = LOCKING_ANDX_LARGE_FILES; | 658 | __u8 lockType = LOCKING_ANDX_LARGE_FILES; |
719 | bool posix_locking = 0; | 659 | bool posix_locking = 0; |
@@ -767,13 +707,7 @@ int cifs_lock(struct file *file, int cmd, struct file_lock *pfLock) | |||
767 | cFYI(1, "Unknown type of lock"); | 707 | cFYI(1, "Unknown type of lock"); |
768 | 708 | ||
769 | cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); | 709 | cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); |
770 | tcon = cifs_sb->tcon; | 710 | tcon = tlink_tcon(((struct cifsFileInfo *)file->private_data)->tlink); |
771 | |||
772 | if (file->private_data == NULL) { | ||
773 | rc = -EBADF; | ||
774 | FreeXid(xid); | ||
775 | return rc; | ||
776 | } | ||
777 | netfid = ((struct cifsFileInfo *)file->private_data)->netfid; | 711 | netfid = ((struct cifsFileInfo *)file->private_data)->netfid; |
778 | 712 | ||
779 | if ((tcon->ses->capabilities & CAP_UNIX) && | 713 | if ((tcon->ses->capabilities & CAP_UNIX) && |
@@ -791,20 +725,20 @@ int cifs_lock(struct file *file, int cmd, struct file_lock *pfLock) | |||
791 | else | 725 | else |
792 | posix_lock_type = CIFS_WRLCK; | 726 | posix_lock_type = CIFS_WRLCK; |
793 | rc = CIFSSMBPosixLock(xid, tcon, netfid, 1 /* get */, | 727 | rc = CIFSSMBPosixLock(xid, tcon, netfid, 1 /* get */, |
794 | length, pfLock, | 728 | length, pfLock, posix_lock_type, |
795 | posix_lock_type, wait_flag); | 729 | wait_flag); |
796 | FreeXid(xid); | 730 | FreeXid(xid); |
797 | return rc; | 731 | return rc; |
798 | } | 732 | } |
799 | 733 | ||
800 | /* BB we could chain these into one lock request BB */ | 734 | /* BB we could chain these into one lock request BB */ |
801 | rc = CIFSSMBLock(xid, tcon, netfid, length, pfLock->fl_start, | 735 | rc = CIFSSMBLock(xid, tcon, netfid, length, pfLock->fl_start, |
802 | 0, 1, lockType, 0 /* wait flag */ ); | 736 | 0, 1, lockType, 0 /* wait flag */, 0); |
803 | if (rc == 0) { | 737 | if (rc == 0) { |
804 | rc = CIFSSMBLock(xid, tcon, netfid, length, | 738 | rc = CIFSSMBLock(xid, tcon, netfid, length, |
805 | pfLock->fl_start, 1 /* numUnlock */ , | 739 | pfLock->fl_start, 1 /* numUnlock */ , |
806 | 0 /* numLock */ , lockType, | 740 | 0 /* numLock */ , lockType, |
807 | 0 /* wait flag */ ); | 741 | 0 /* wait flag */, 0); |
808 | pfLock->fl_type = F_UNLCK; | 742 | pfLock->fl_type = F_UNLCK; |
809 | if (rc != 0) | 743 | if (rc != 0) |
810 | cERROR(1, "Error unlocking previously locked " | 744 | cERROR(1, "Error unlocking previously locked " |
@@ -821,13 +755,13 @@ int cifs_lock(struct file *file, int cmd, struct file_lock *pfLock) | |||
821 | rc = CIFSSMBLock(xid, tcon, netfid, length, | 755 | rc = CIFSSMBLock(xid, tcon, netfid, length, |
822 | pfLock->fl_start, 0, 1, | 756 | pfLock->fl_start, 0, 1, |
823 | lockType | LOCKING_ANDX_SHARED_LOCK, | 757 | lockType | LOCKING_ANDX_SHARED_LOCK, |
824 | 0 /* wait flag */); | 758 | 0 /* wait flag */, 0); |
825 | if (rc == 0) { | 759 | if (rc == 0) { |
826 | rc = CIFSSMBLock(xid, tcon, netfid, | 760 | rc = CIFSSMBLock(xid, tcon, netfid, |
827 | length, pfLock->fl_start, 1, 0, | 761 | length, pfLock->fl_start, 1, 0, |
828 | lockType | | 762 | lockType | |
829 | LOCKING_ANDX_SHARED_LOCK, | 763 | LOCKING_ANDX_SHARED_LOCK, |
830 | 0 /* wait flag */); | 764 | 0 /* wait flag */, 0); |
831 | pfLock->fl_type = F_RDLCK; | 765 | pfLock->fl_type = F_RDLCK; |
832 | if (rc != 0) | 766 | if (rc != 0) |
833 | cERROR(1, "Error unlocking " | 767 | cERROR(1, "Error unlocking " |
@@ -863,15 +797,15 @@ int cifs_lock(struct file *file, int cmd, struct file_lock *pfLock) | |||
863 | posix_lock_type = CIFS_UNLCK; | 797 | posix_lock_type = CIFS_UNLCK; |
864 | 798 | ||
865 | rc = CIFSSMBPosixLock(xid, tcon, netfid, 0 /* set */, | 799 | rc = CIFSSMBPosixLock(xid, tcon, netfid, 0 /* set */, |
866 | length, pfLock, | 800 | length, pfLock, posix_lock_type, |
867 | posix_lock_type, wait_flag); | 801 | wait_flag); |
868 | } else { | 802 | } else { |
869 | struct cifsFileInfo *fid = file->private_data; | 803 | struct cifsFileInfo *fid = file->private_data; |
870 | 804 | ||
871 | if (numLock) { | 805 | if (numLock) { |
872 | rc = CIFSSMBLock(xid, tcon, netfid, length, | 806 | rc = CIFSSMBLock(xid, tcon, netfid, length, |
873 | pfLock->fl_start, | 807 | pfLock->fl_start, 0, numLock, lockType, |
874 | 0, numLock, lockType, wait_flag); | 808 | wait_flag, 0); |
875 | 809 | ||
876 | if (rc == 0) { | 810 | if (rc == 0) { |
877 | /* For Windows locks we must store them. */ | 811 | /* For Windows locks we must store them. */ |
@@ -891,9 +825,9 @@ int cifs_lock(struct file *file, int cmd, struct file_lock *pfLock) | |||
891 | (pfLock->fl_start + length) >= | 825 | (pfLock->fl_start + length) >= |
892 | (li->offset + li->length)) { | 826 | (li->offset + li->length)) { |
893 | stored_rc = CIFSSMBLock(xid, tcon, | 827 | stored_rc = CIFSSMBLock(xid, tcon, |
894 | netfid, | 828 | netfid, li->length, |
895 | li->length, li->offset, | 829 | li->offset, 1, 0, |
896 | 1, 0, li->type, false); | 830 | li->type, false, 0); |
897 | if (stored_rc) | 831 | if (stored_rc) |
898 | rc = stored_rc; | 832 | rc = stored_rc; |
899 | else { | 833 | else { |
@@ -912,31 +846,8 @@ int cifs_lock(struct file *file, int cmd, struct file_lock *pfLock) | |||
912 | return rc; | 846 | return rc; |
913 | } | 847 | } |
914 | 848 | ||
915 | /* | ||
916 | * Set the timeout on write requests past EOF. For some servers (Windows) | ||
917 | * these calls can be very long. | ||
918 | * | ||
919 | * If we're writing >10M past the EOF we give a 180s timeout. Anything less | ||
920 | * than that gets a 45s timeout. Writes not past EOF get 15s timeouts. | ||
921 | * The 10M cutoff is totally arbitrary. A better scheme for this would be | ||
922 | * welcome if someone wants to suggest one. | ||
923 | * | ||
924 | * We may be able to do a better job with this if there were some way to | ||
925 | * declare that a file should be sparse. | ||
926 | */ | ||
927 | static int | ||
928 | cifs_write_timeout(struct cifsInodeInfo *cifsi, loff_t offset) | ||
929 | { | ||
930 | if (offset <= cifsi->server_eof) | ||
931 | return CIFS_STD_OP; | ||
932 | else if (offset > (cifsi->server_eof + (10 * 1024 * 1024))) | ||
933 | return CIFS_VLONG_OP; | ||
934 | else | ||
935 | return CIFS_LONG_OP; | ||
936 | } | ||
937 | |||
938 | /* update the file size (if needed) after a write */ | 849 | /* update the file size (if needed) after a write */ |
939 | static void | 850 | void |
940 | cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset, | 851 | cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset, |
941 | unsigned int bytes_written) | 852 | unsigned int bytes_written) |
942 | { | 853 | { |
@@ -946,188 +857,58 @@ cifs_update_eof(struct cifsInodeInfo *cifsi, loff_t offset, | |||
946 | cifsi->server_eof = end_of_write; | 857 | cifsi->server_eof = end_of_write; |
947 | } | 858 | } |
948 | 859 | ||
949 | ssize_t cifs_user_write(struct file *file, const char __user *write_data, | 860 | static ssize_t cifs_write(struct cifsFileInfo *open_file, __u32 pid, |
950 | size_t write_size, loff_t *poffset) | 861 | const char *write_data, size_t write_size, |
862 | loff_t *poffset) | ||
951 | { | 863 | { |
952 | int rc = 0; | 864 | int rc = 0; |
953 | unsigned int bytes_written = 0; | 865 | unsigned int bytes_written = 0; |
954 | unsigned int total_written; | 866 | unsigned int total_written; |
955 | struct cifs_sb_info *cifs_sb; | 867 | struct cifs_sb_info *cifs_sb; |
956 | struct cifsTconInfo *pTcon; | 868 | struct cifs_tcon *pTcon; |
957 | int xid, long_op; | 869 | int xid; |
958 | struct cifsFileInfo *open_file; | 870 | struct dentry *dentry = open_file->dentry; |
959 | struct cifsInodeInfo *cifsi = CIFS_I(file->f_path.dentry->d_inode); | 871 | struct cifsInodeInfo *cifsi = CIFS_I(dentry->d_inode); |
960 | 872 | struct cifs_io_parms io_parms; | |
961 | cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); | ||
962 | |||
963 | pTcon = cifs_sb->tcon; | ||
964 | |||
965 | /* cFYI(1, " write %d bytes to offset %lld of %s", write_size, | ||
966 | *poffset, file->f_path.dentry->d_name.name); */ | ||
967 | |||
968 | if (file->private_data == NULL) | ||
969 | return -EBADF; | ||
970 | open_file = file->private_data; | ||
971 | |||
972 | rc = generic_write_checks(file, poffset, &write_size, 0); | ||
973 | if (rc) | ||
974 | return rc; | ||
975 | |||
976 | xid = GetXid(); | ||
977 | |||
978 | long_op = cifs_write_timeout(cifsi, *poffset); | ||
979 | for (total_written = 0; write_size > total_written; | ||
980 | total_written += bytes_written) { | ||
981 | rc = -EAGAIN; | ||
982 | while (rc == -EAGAIN) { | ||
983 | if (file->private_data == NULL) { | ||
984 | /* file has been closed on us */ | ||
985 | FreeXid(xid); | ||
986 | /* if we have gotten here we have written some data | ||
987 | and blocked, and the file has been freed on us while | ||
988 | we blocked so return what we managed to write */ | ||
989 | return total_written; | ||
990 | } | ||
991 | if (open_file->closePend) { | ||
992 | FreeXid(xid); | ||
993 | if (total_written) | ||
994 | return total_written; | ||
995 | else | ||
996 | return -EBADF; | ||
997 | } | ||
998 | if (open_file->invalidHandle) { | ||
999 | /* we could deadlock if we called | ||
1000 | filemap_fdatawait from here so tell | ||
1001 | reopen_file not to flush data to server | ||
1002 | now */ | ||
1003 | rc = cifs_reopen_file(file, false); | ||
1004 | if (rc != 0) | ||
1005 | break; | ||
1006 | } | ||
1007 | |||
1008 | rc = CIFSSMBWrite(xid, pTcon, | ||
1009 | open_file->netfid, | ||
1010 | min_t(const int, cifs_sb->wsize, | ||
1011 | write_size - total_written), | ||
1012 | *poffset, &bytes_written, | ||
1013 | NULL, write_data + total_written, long_op); | ||
1014 | } | ||
1015 | if (rc || (bytes_written == 0)) { | ||
1016 | if (total_written) | ||
1017 | break; | ||
1018 | else { | ||
1019 | FreeXid(xid); | ||
1020 | return rc; | ||
1021 | } | ||
1022 | } else { | ||
1023 | cifs_update_eof(cifsi, *poffset, bytes_written); | ||
1024 | *poffset += bytes_written; | ||
1025 | } | ||
1026 | long_op = CIFS_STD_OP; /* subsequent writes fast - | ||
1027 | 15 seconds is plenty */ | ||
1028 | } | ||
1029 | |||
1030 | cifs_stats_bytes_written(pTcon, total_written); | ||
1031 | |||
1032 | /* since the write may have blocked check these pointers again */ | ||
1033 | if ((file->f_path.dentry) && (file->f_path.dentry->d_inode)) { | ||
1034 | struct inode *inode = file->f_path.dentry->d_inode; | ||
1035 | /* Do not update local mtime - server will set its actual value on write | ||
1036 | * inode->i_ctime = inode->i_mtime = | ||
1037 | * current_fs_time(inode->i_sb);*/ | ||
1038 | if (total_written > 0) { | ||
1039 | spin_lock(&inode->i_lock); | ||
1040 | if (*poffset > file->f_path.dentry->d_inode->i_size) | ||
1041 | i_size_write(file->f_path.dentry->d_inode, | ||
1042 | *poffset); | ||
1043 | spin_unlock(&inode->i_lock); | ||
1044 | } | ||
1045 | mark_inode_dirty_sync(file->f_path.dentry->d_inode); | ||
1046 | } | ||
1047 | FreeXid(xid); | ||
1048 | return total_written; | ||
1049 | } | ||
1050 | |||
1051 | static ssize_t cifs_write(struct file *file, const char *write_data, | ||
1052 | size_t write_size, loff_t *poffset) | ||
1053 | { | ||
1054 | int rc = 0; | ||
1055 | unsigned int bytes_written = 0; | ||
1056 | unsigned int total_written; | ||
1057 | struct cifs_sb_info *cifs_sb; | ||
1058 | struct cifsTconInfo *pTcon; | ||
1059 | int xid, long_op; | ||
1060 | struct cifsFileInfo *open_file; | ||
1061 | struct cifsInodeInfo *cifsi = CIFS_I(file->f_path.dentry->d_inode); | ||
1062 | |||
1063 | cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); | ||
1064 | 873 | ||
1065 | pTcon = cifs_sb->tcon; | 874 | cifs_sb = CIFS_SB(dentry->d_sb); |
1066 | 875 | ||
1067 | cFYI(1, "write %zd bytes to offset %lld of %s", write_size, | 876 | cFYI(1, "write %zd bytes to offset %lld of %s", write_size, |
1068 | *poffset, file->f_path.dentry->d_name.name); | 877 | *poffset, dentry->d_name.name); |
1069 | 878 | ||
1070 | if (file->private_data == NULL) | 879 | pTcon = tlink_tcon(open_file->tlink); |
1071 | return -EBADF; | ||
1072 | open_file = file->private_data; | ||
1073 | 880 | ||
1074 | xid = GetXid(); | 881 | xid = GetXid(); |
1075 | 882 | ||
1076 | long_op = cifs_write_timeout(cifsi, *poffset); | ||
1077 | for (total_written = 0; write_size > total_written; | 883 | for (total_written = 0; write_size > total_written; |
1078 | total_written += bytes_written) { | 884 | total_written += bytes_written) { |
1079 | rc = -EAGAIN; | 885 | rc = -EAGAIN; |
1080 | while (rc == -EAGAIN) { | 886 | while (rc == -EAGAIN) { |
1081 | if (file->private_data == NULL) { | 887 | struct kvec iov[2]; |
1082 | /* file has been closed on us */ | 888 | unsigned int len; |
1083 | FreeXid(xid); | 889 | |
1084 | /* if we have gotten here we have written some data | ||
1085 | and blocked, and the file has been freed on us | ||
1086 | while we blocked so return what we managed to | ||
1087 | write */ | ||
1088 | return total_written; | ||
1089 | } | ||
1090 | if (open_file->closePend) { | ||
1091 | FreeXid(xid); | ||
1092 | if (total_written) | ||
1093 | return total_written; | ||
1094 | else | ||
1095 | return -EBADF; | ||
1096 | } | ||
1097 | if (open_file->invalidHandle) { | 890 | if (open_file->invalidHandle) { |
1098 | /* we could deadlock if we called | 891 | /* we could deadlock if we called |
1099 | filemap_fdatawait from here so tell | 892 | filemap_fdatawait from here so tell |
1100 | reopen_file not to flush data to | 893 | reopen_file not to flush data to |
1101 | server now */ | 894 | server now */ |
1102 | rc = cifs_reopen_file(file, false); | 895 | rc = cifs_reopen_file(open_file, false); |
1103 | if (rc != 0) | 896 | if (rc != 0) |
1104 | break; | 897 | break; |
1105 | } | 898 | } |
1106 | if (experimEnabled || (pTcon->ses->server && | 899 | |
1107 | ((pTcon->ses->server->secMode & | 900 | len = min((size_t)cifs_sb->wsize, |
1108 | (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) | 901 | write_size - total_written); |
1109 | == 0))) { | 902 | /* iov[0] is reserved for smb header */ |
1110 | struct kvec iov[2]; | 903 | iov[1].iov_base = (char *)write_data + total_written; |
1111 | unsigned int len; | 904 | iov[1].iov_len = len; |
1112 | 905 | io_parms.netfid = open_file->netfid; | |
1113 | len = min((size_t)cifs_sb->wsize, | 906 | io_parms.pid = pid; |
1114 | write_size - total_written); | 907 | io_parms.tcon = pTcon; |
1115 | /* iov[0] is reserved for smb header */ | 908 | io_parms.offset = *poffset; |
1116 | iov[1].iov_base = (char *)write_data + | 909 | io_parms.length = len; |
1117 | total_written; | 910 | rc = CIFSSMBWrite2(xid, &io_parms, &bytes_written, iov, |
1118 | iov[1].iov_len = len; | 911 | 1, 0); |
1119 | rc = CIFSSMBWrite2(xid, pTcon, | ||
1120 | open_file->netfid, len, | ||
1121 | *poffset, &bytes_written, | ||
1122 | iov, 1, long_op); | ||
1123 | } else | ||
1124 | rc = CIFSSMBWrite(xid, pTcon, | ||
1125 | open_file->netfid, | ||
1126 | min_t(const int, cifs_sb->wsize, | ||
1127 | write_size - total_written), | ||
1128 | *poffset, &bytes_written, | ||
1129 | write_data + total_written, | ||
1130 | NULL, long_op); | ||
1131 | } | 912 | } |
1132 | if (rc || (bytes_written == 0)) { | 913 | if (rc || (bytes_written == 0)) { |
1133 | if (total_written) | 914 | if (total_written) |
@@ -1140,49 +921,44 @@ static ssize_t cifs_write(struct file *file, const char *write_data, | |||
1140 | cifs_update_eof(cifsi, *poffset, bytes_written); | 921 | cifs_update_eof(cifsi, *poffset, bytes_written); |
1141 | *poffset += bytes_written; | 922 | *poffset += bytes_written; |
1142 | } | 923 | } |
1143 | long_op = CIFS_STD_OP; /* subsequent writes fast - | ||
1144 | 15 seconds is plenty */ | ||
1145 | } | 924 | } |
1146 | 925 | ||
1147 | cifs_stats_bytes_written(pTcon, total_written); | 926 | cifs_stats_bytes_written(pTcon, total_written); |
1148 | 927 | ||
1149 | /* since the write may have blocked check these pointers again */ | 928 | if (total_written > 0) { |
1150 | if ((file->f_path.dentry) && (file->f_path.dentry->d_inode)) { | 929 | spin_lock(&dentry->d_inode->i_lock); |
1151 | /*BB We could make this contingent on superblock ATIME flag too */ | 930 | if (*poffset > dentry->d_inode->i_size) |
1152 | /* file->f_path.dentry->d_inode->i_ctime = | 931 | i_size_write(dentry->d_inode, *poffset); |
1153 | file->f_path.dentry->d_inode->i_mtime = CURRENT_TIME;*/ | 932 | spin_unlock(&dentry->d_inode->i_lock); |
1154 | if (total_written > 0) { | ||
1155 | spin_lock(&file->f_path.dentry->d_inode->i_lock); | ||
1156 | if (*poffset > file->f_path.dentry->d_inode->i_size) | ||
1157 | i_size_write(file->f_path.dentry->d_inode, | ||
1158 | *poffset); | ||
1159 | spin_unlock(&file->f_path.dentry->d_inode->i_lock); | ||
1160 | } | ||
1161 | mark_inode_dirty_sync(file->f_path.dentry->d_inode); | ||
1162 | } | 933 | } |
934 | mark_inode_dirty_sync(dentry->d_inode); | ||
1163 | FreeXid(xid); | 935 | FreeXid(xid); |
1164 | return total_written; | 936 | return total_written; |
1165 | } | 937 | } |
1166 | 938 | ||
1167 | #ifdef CONFIG_CIFS_EXPERIMENTAL | 939 | struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode, |
1168 | struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode) | 940 | bool fsuid_only) |
1169 | { | 941 | { |
1170 | struct cifsFileInfo *open_file = NULL; | 942 | struct cifsFileInfo *open_file = NULL; |
943 | struct cifs_sb_info *cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb); | ||
1171 | 944 | ||
1172 | read_lock(&GlobalSMBSeslock); | 945 | /* only filter by fsuid on multiuser mounts */ |
946 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)) | ||
947 | fsuid_only = false; | ||
948 | |||
949 | spin_lock(&cifs_file_list_lock); | ||
1173 | /* we could simply get the first_list_entry since write-only entries | 950 | /* we could simply get the first_list_entry since write-only entries |
1174 | are always at the end of the list but since the first entry might | 951 | are always at the end of the list but since the first entry might |
1175 | have a close pending, we go through the whole list */ | 952 | have a close pending, we go through the whole list */ |
1176 | list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { | 953 | list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { |
1177 | if (open_file->closePend) | 954 | if (fsuid_only && open_file->uid != current_fsuid()) |
1178 | continue; | 955 | continue; |
1179 | if (open_file->pfile && ((open_file->pfile->f_flags & O_RDWR) || | 956 | if (OPEN_FMODE(open_file->f_flags) & FMODE_READ) { |
1180 | (open_file->pfile->f_flags & O_RDONLY))) { | ||
1181 | if (!open_file->invalidHandle) { | 957 | if (!open_file->invalidHandle) { |
1182 | /* found a good file */ | 958 | /* found a good file */ |
1183 | /* lock it so it will not be closed on us */ | 959 | /* lock it so it will not be closed on us */ |
1184 | cifsFileInfo_get(open_file); | 960 | cifsFileInfo_get(open_file); |
1185 | read_unlock(&GlobalSMBSeslock); | 961 | spin_unlock(&cifs_file_list_lock); |
1186 | return open_file; | 962 | return open_file; |
1187 | } /* else might as well continue, and look for | 963 | } /* else might as well continue, and look for |
1188 | another, or simply have the caller reopen it | 964 | another, or simply have the caller reopen it |
@@ -1190,14 +966,15 @@ struct cifsFileInfo *find_readable_file(struct cifsInodeInfo *cifs_inode) | |||
1190 | } else /* write only file */ | 966 | } else /* write only file */ |
1191 | break; /* write only files are last so must be done */ | 967 | break; /* write only files are last so must be done */ |
1192 | } | 968 | } |
1193 | read_unlock(&GlobalSMBSeslock); | 969 | spin_unlock(&cifs_file_list_lock); |
1194 | return NULL; | 970 | return NULL; |
1195 | } | 971 | } |
1196 | #endif | ||
1197 | 972 | ||
1198 | struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *cifs_inode) | 973 | struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *cifs_inode, |
974 | bool fsuid_only) | ||
1199 | { | 975 | { |
1200 | struct cifsFileInfo *open_file; | 976 | struct cifsFileInfo *open_file; |
977 | struct cifs_sb_info *cifs_sb; | ||
1201 | bool any_available = false; | 978 | bool any_available = false; |
1202 | int rc; | 979 | int rc; |
1203 | 980 | ||
@@ -1211,53 +988,41 @@ struct cifsFileInfo *find_writable_file(struct cifsInodeInfo *cifs_inode) | |||
1211 | return NULL; | 988 | return NULL; |
1212 | } | 989 | } |
1213 | 990 | ||
1214 | read_lock(&GlobalSMBSeslock); | 991 | cifs_sb = CIFS_SB(cifs_inode->vfs_inode.i_sb); |
992 | |||
993 | /* only filter by fsuid on multiuser mounts */ | ||
994 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER)) | ||
995 | fsuid_only = false; | ||
996 | |||
997 | spin_lock(&cifs_file_list_lock); | ||
1215 | refind_writable: | 998 | refind_writable: |
1216 | list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { | 999 | list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { |
1217 | if (open_file->closePend || | 1000 | if (!any_available && open_file->pid != current->tgid) |
1218 | (!any_available && open_file->pid != current->tgid)) | ||
1219 | continue; | 1001 | continue; |
1220 | 1002 | if (fsuid_only && open_file->uid != current_fsuid()) | |
1221 | if (open_file->pfile && | 1003 | continue; |
1222 | ((open_file->pfile->f_flags & O_RDWR) || | 1004 | if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) { |
1223 | (open_file->pfile->f_flags & O_WRONLY))) { | ||
1224 | cifsFileInfo_get(open_file); | 1005 | cifsFileInfo_get(open_file); |
1225 | 1006 | ||
1226 | if (!open_file->invalidHandle) { | 1007 | if (!open_file->invalidHandle) { |
1227 | /* found a good writable file */ | 1008 | /* found a good writable file */ |
1228 | read_unlock(&GlobalSMBSeslock); | 1009 | spin_unlock(&cifs_file_list_lock); |
1229 | return open_file; | 1010 | return open_file; |
1230 | } | 1011 | } |
1231 | 1012 | ||
1232 | read_unlock(&GlobalSMBSeslock); | 1013 | spin_unlock(&cifs_file_list_lock); |
1014 | |||
1233 | /* Had to unlock since following call can block */ | 1015 | /* Had to unlock since following call can block */ |
1234 | rc = cifs_reopen_file(open_file->pfile, false); | 1016 | rc = cifs_reopen_file(open_file, false); |
1235 | if (!rc) { | 1017 | if (!rc) |
1236 | if (!open_file->closePend) | 1018 | return open_file; |
1237 | return open_file; | ||
1238 | else { /* start over in case this was deleted */ | ||
1239 | /* since the list could be modified */ | ||
1240 | read_lock(&GlobalSMBSeslock); | ||
1241 | cifsFileInfo_put(open_file); | ||
1242 | goto refind_writable; | ||
1243 | } | ||
1244 | } | ||
1245 | 1019 | ||
1246 | /* if it fails, try another handle if possible - | 1020 | /* if it fails, try another handle if possible */ |
1247 | (we can not do this if closePending since | ||
1248 | loop could be modified - in which case we | ||
1249 | have to start at the beginning of the list | ||
1250 | again. Note that it would be bad | ||
1251 | to hold up writepages here (rather than | ||
1252 | in caller) with continuous retries */ | ||
1253 | cFYI(1, "wp failed on reopen file"); | 1021 | cFYI(1, "wp failed on reopen file"); |
1254 | read_lock(&GlobalSMBSeslock); | ||
1255 | /* can not use this handle, no write | ||
1256 | pending on this one after all */ | ||
1257 | cifsFileInfo_put(open_file); | 1022 | cifsFileInfo_put(open_file); |
1258 | 1023 | ||
1259 | if (open_file->closePend) /* list could have changed */ | 1024 | spin_lock(&cifs_file_list_lock); |
1260 | goto refind_writable; | 1025 | |
1261 | /* else we simply continue to the next entry. Thus | 1026 | /* else we simply continue to the next entry. Thus |
1262 | we do not loop on reopen errors. If we | 1027 | we do not loop on reopen errors. If we |
1263 | can not reopen the file, for example if we | 1028 | can not reopen the file, for example if we |
@@ -1272,7 +1037,7 @@ refind_writable: | |||
1272 | any_available = true; | 1037 | any_available = true; |
1273 | goto refind_writable; | 1038 | goto refind_writable; |
1274 | } | 1039 | } |
1275 | read_unlock(&GlobalSMBSeslock); | 1040 | spin_unlock(&cifs_file_list_lock); |
1276 | return NULL; | 1041 | return NULL; |
1277 | } | 1042 | } |
1278 | 1043 | ||
@@ -1283,8 +1048,6 @@ static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to) | |||
1283 | char *write_data; | 1048 | char *write_data; |
1284 | int rc = -EFAULT; | 1049 | int rc = -EFAULT; |
1285 | int bytes_written = 0; | 1050 | int bytes_written = 0; |
1286 | struct cifs_sb_info *cifs_sb; | ||
1287 | struct cifsTconInfo *pTcon; | ||
1288 | struct inode *inode; | 1051 | struct inode *inode; |
1289 | struct cifsFileInfo *open_file; | 1052 | struct cifsFileInfo *open_file; |
1290 | 1053 | ||
@@ -1292,8 +1055,6 @@ static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to) | |||
1292 | return -EFAULT; | 1055 | return -EFAULT; |
1293 | 1056 | ||
1294 | inode = page->mapping->host; | 1057 | inode = page->mapping->host; |
1295 | cifs_sb = CIFS_SB(inode->i_sb); | ||
1296 | pTcon = cifs_sb->tcon; | ||
1297 | 1058 | ||
1298 | offset += (loff_t)from; | 1059 | offset += (loff_t)from; |
1299 | write_data = kmap(page); | 1060 | write_data = kmap(page); |
@@ -1314,10 +1075,10 @@ static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to) | |||
1314 | if (mapping->host->i_size - offset < (loff_t)to) | 1075 | if (mapping->host->i_size - offset < (loff_t)to) |
1315 | to = (unsigned)(mapping->host->i_size - offset); | 1076 | to = (unsigned)(mapping->host->i_size - offset); |
1316 | 1077 | ||
1317 | open_file = find_writable_file(CIFS_I(mapping->host)); | 1078 | open_file = find_writable_file(CIFS_I(mapping->host), false); |
1318 | if (open_file) { | 1079 | if (open_file) { |
1319 | bytes_written = cifs_write(open_file->pfile, write_data, | 1080 | bytes_written = cifs_write(open_file, open_file->pid, |
1320 | to-from, &offset); | 1081 | write_data, to - from, &offset); |
1321 | cifsFileInfo_put(open_file); | 1082 | cifsFileInfo_put(open_file); |
1322 | /* Does mm or vfs already set times? */ | 1083 | /* Does mm or vfs already set times? */ |
1323 | inode->i_atime = inode->i_mtime = current_fs_time(inode->i_sb); | 1084 | inode->i_atime = inode->i_mtime = current_fs_time(inode->i_sb); |
@@ -1337,61 +1098,20 @@ static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to) | |||
1337 | static int cifs_writepages(struct address_space *mapping, | 1098 | static int cifs_writepages(struct address_space *mapping, |
1338 | struct writeback_control *wbc) | 1099 | struct writeback_control *wbc) |
1339 | { | 1100 | { |
1340 | struct backing_dev_info *bdi = mapping->backing_dev_info; | 1101 | struct cifs_sb_info *cifs_sb = CIFS_SB(mapping->host->i_sb); |
1341 | unsigned int bytes_to_write; | 1102 | bool done = false, scanned = false, range_whole = false; |
1342 | unsigned int bytes_written; | 1103 | pgoff_t end, index; |
1343 | struct cifs_sb_info *cifs_sb; | 1104 | struct cifs_writedata *wdata; |
1344 | int done = 0; | ||
1345 | pgoff_t end; | ||
1346 | pgoff_t index; | ||
1347 | int range_whole = 0; | ||
1348 | struct kvec *iov; | ||
1349 | int len; | ||
1350 | int n_iov = 0; | ||
1351 | pgoff_t next; | ||
1352 | int nr_pages; | ||
1353 | __u64 offset = 0; | ||
1354 | struct cifsFileInfo *open_file; | ||
1355 | struct cifsInodeInfo *cifsi = CIFS_I(mapping->host); | ||
1356 | struct page *page; | 1105 | struct page *page; |
1357 | struct pagevec pvec; | ||
1358 | int rc = 0; | 1106 | int rc = 0; |
1359 | int scanned = 0; | ||
1360 | int xid, long_op; | ||
1361 | |||
1362 | cifs_sb = CIFS_SB(mapping->host->i_sb); | ||
1363 | 1107 | ||
1364 | /* | 1108 | /* |
1365 | * If wsize is smaller that the page cache size, default to writing | 1109 | * If wsize is smaller than the page cache size, default to writing |
1366 | * one page at a time via cifs_writepage | 1110 | * one page at a time via cifs_writepage |
1367 | */ | 1111 | */ |
1368 | if (cifs_sb->wsize < PAGE_CACHE_SIZE) | 1112 | if (cifs_sb->wsize < PAGE_CACHE_SIZE) |
1369 | return generic_writepages(mapping, wbc); | 1113 | return generic_writepages(mapping, wbc); |
1370 | 1114 | ||
1371 | if ((cifs_sb->tcon->ses) && (cifs_sb->tcon->ses->server)) | ||
1372 | if (cifs_sb->tcon->ses->server->secMode & | ||
1373 | (SECMODE_SIGN_REQUIRED | SECMODE_SIGN_ENABLED)) | ||
1374 | if (!experimEnabled) | ||
1375 | return generic_writepages(mapping, wbc); | ||
1376 | |||
1377 | iov = kmalloc(32 * sizeof(struct kvec), GFP_KERNEL); | ||
1378 | if (iov == NULL) | ||
1379 | return generic_writepages(mapping, wbc); | ||
1380 | |||
1381 | |||
1382 | /* | ||
1383 | * BB: Is this meaningful for a non-block-device file system? | ||
1384 | * If it is, we should test it again after we do I/O | ||
1385 | */ | ||
1386 | if (wbc->nonblocking && bdi_write_congested(bdi)) { | ||
1387 | wbc->encountered_congestion = 1; | ||
1388 | kfree(iov); | ||
1389 | return 0; | ||
1390 | } | ||
1391 | |||
1392 | xid = GetXid(); | ||
1393 | |||
1394 | pagevec_init(&pvec, 0); | ||
1395 | if (wbc->range_cyclic) { | 1115 | if (wbc->range_cyclic) { |
1396 | index = mapping->writeback_index; /* Start from prev offset */ | 1116 | index = mapping->writeback_index; /* Start from prev offset */ |
1397 | end = -1; | 1117 | end = -1; |
@@ -1399,24 +1119,49 @@ static int cifs_writepages(struct address_space *mapping, | |||
1399 | index = wbc->range_start >> PAGE_CACHE_SHIFT; | 1119 | index = wbc->range_start >> PAGE_CACHE_SHIFT; |
1400 | end = wbc->range_end >> PAGE_CACHE_SHIFT; | 1120 | end = wbc->range_end >> PAGE_CACHE_SHIFT; |
1401 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) | 1121 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
1402 | range_whole = 1; | 1122 | range_whole = true; |
1403 | scanned = 1; | 1123 | scanned = true; |
1404 | } | 1124 | } |
1405 | retry: | 1125 | retry: |
1406 | while (!done && (index <= end) && | 1126 | while (!done && index <= end) { |
1407 | (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, | 1127 | unsigned int i, nr_pages, found_pages; |
1408 | PAGECACHE_TAG_DIRTY, | 1128 | pgoff_t next = 0, tofind; |
1409 | min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1))) { | 1129 | struct page **pages; |
1410 | int first; | 1130 | |
1411 | unsigned int i; | 1131 | tofind = min((cifs_sb->wsize / PAGE_CACHE_SIZE) - 1, |
1412 | 1132 | end - index) + 1; | |
1413 | first = -1; | 1133 | |
1414 | next = 0; | 1134 | wdata = cifs_writedata_alloc((unsigned int)tofind); |
1415 | n_iov = 0; | 1135 | if (!wdata) { |
1416 | bytes_to_write = 0; | 1136 | rc = -ENOMEM; |
1417 | 1137 | break; | |
1418 | for (i = 0; i < nr_pages; i++) { | 1138 | } |
1419 | page = pvec.pages[i]; | 1139 | |
1140 | /* | ||
1141 | * find_get_pages_tag seems to return a max of 256 on each | ||
1142 | * iteration, so we must call it several times in order to | ||
1143 | * fill the array or the wsize is effectively limited to | ||
1144 | * 256 * PAGE_CACHE_SIZE. | ||
1145 | */ | ||
1146 | found_pages = 0; | ||
1147 | pages = wdata->pages; | ||
1148 | do { | ||
1149 | nr_pages = find_get_pages_tag(mapping, &index, | ||
1150 | PAGECACHE_TAG_DIRTY, | ||
1151 | tofind, pages); | ||
1152 | found_pages += nr_pages; | ||
1153 | tofind -= nr_pages; | ||
1154 | pages += nr_pages; | ||
1155 | } while (nr_pages && tofind && index <= end); | ||
1156 | |||
1157 | if (found_pages == 0) { | ||
1158 | kref_put(&wdata->refcount, cifs_writedata_release); | ||
1159 | break; | ||
1160 | } | ||
1161 | |||
1162 | nr_pages = 0; | ||
1163 | for (i = 0; i < found_pages; i++) { | ||
1164 | page = wdata->pages[i]; | ||
1420 | /* | 1165 | /* |
1421 | * At this point we hold neither mapping->tree_lock nor | 1166 | * At this point we hold neither mapping->tree_lock nor |
1422 | * lock on the page itself: the page may be truncated or | 1167 | * lock on the page itself: the page may be truncated or |
@@ -1425,7 +1170,7 @@ retry: | |||
1425 | * mapping | 1170 | * mapping |
1426 | */ | 1171 | */ |
1427 | 1172 | ||
1428 | if (first < 0) | 1173 | if (nr_pages == 0) |
1429 | lock_page(page); | 1174 | lock_page(page); |
1430 | else if (!trylock_page(page)) | 1175 | else if (!trylock_page(page)) |
1431 | break; | 1176 | break; |
@@ -1436,7 +1181,7 @@ retry: | |||
1436 | } | 1181 | } |
1437 | 1182 | ||
1438 | if (!wbc->range_cyclic && page->index > end) { | 1183 | if (!wbc->range_cyclic && page->index > end) { |
1439 | done = 1; | 1184 | done = true; |
1440 | unlock_page(page); | 1185 | unlock_page(page); |
1441 | break; | 1186 | break; |
1442 | } | 1187 | } |
@@ -1463,110 +1208,96 @@ retry: | |||
1463 | set_page_writeback(page); | 1208 | set_page_writeback(page); |
1464 | 1209 | ||
1465 | if (page_offset(page) >= mapping->host->i_size) { | 1210 | if (page_offset(page) >= mapping->host->i_size) { |
1466 | done = 1; | 1211 | done = true; |
1467 | unlock_page(page); | 1212 | unlock_page(page); |
1468 | end_page_writeback(page); | 1213 | end_page_writeback(page); |
1469 | break; | 1214 | break; |
1470 | } | 1215 | } |
1471 | 1216 | ||
1472 | /* | 1217 | wdata->pages[i] = page; |
1473 | * BB can we get rid of this? pages are held by pvec | 1218 | next = page->index + 1; |
1474 | */ | 1219 | ++nr_pages; |
1475 | page_cache_get(page); | 1220 | } |
1476 | 1221 | ||
1477 | len = min(mapping->host->i_size - page_offset(page), | 1222 | /* reset index to refind any pages skipped */ |
1478 | (loff_t)PAGE_CACHE_SIZE); | 1223 | if (nr_pages == 0) |
1224 | index = wdata->pages[0]->index + 1; | ||
1479 | 1225 | ||
1480 | /* reserve iov[0] for the smb header */ | 1226 | /* put any pages we aren't going to use */ |
1481 | n_iov++; | 1227 | for (i = nr_pages; i < found_pages; i++) { |
1482 | iov[n_iov].iov_base = kmap(page); | 1228 | page_cache_release(wdata->pages[i]); |
1483 | iov[n_iov].iov_len = len; | 1229 | wdata->pages[i] = NULL; |
1484 | bytes_to_write += len; | 1230 | } |
1485 | 1231 | ||
1486 | if (first < 0) { | 1232 | /* nothing to write? */ |
1487 | first = i; | 1233 | if (nr_pages == 0) { |
1488 | offset = page_offset(page); | 1234 | kref_put(&wdata->refcount, cifs_writedata_release); |
1489 | } | 1235 | continue; |
1490 | next = page->index + 1; | ||
1491 | if (bytes_to_write + PAGE_CACHE_SIZE > cifs_sb->wsize) | ||
1492 | break; | ||
1493 | } | 1236 | } |
1494 | if (n_iov) { | 1237 | |
1495 | /* Search for a writable handle every time we call | 1238 | wdata->sync_mode = wbc->sync_mode; |
1496 | * CIFSSMBWrite2. We can't rely on the last handle | 1239 | wdata->nr_pages = nr_pages; |
1497 | * we used to still be valid | 1240 | wdata->offset = page_offset(wdata->pages[0]); |
1498 | */ | 1241 | |
1499 | open_file = find_writable_file(CIFS_I(mapping->host)); | 1242 | do { |
1500 | if (!open_file) { | 1243 | if (wdata->cfile != NULL) |
1244 | cifsFileInfo_put(wdata->cfile); | ||
1245 | wdata->cfile = find_writable_file(CIFS_I(mapping->host), | ||
1246 | false); | ||
1247 | if (!wdata->cfile) { | ||
1501 | cERROR(1, "No writable handles for inode"); | 1248 | cERROR(1, "No writable handles for inode"); |
1502 | rc = -EBADF; | 1249 | rc = -EBADF; |
1503 | } else { | 1250 | break; |
1504 | long_op = cifs_write_timeout(cifsi, offset); | ||
1505 | rc = CIFSSMBWrite2(xid, cifs_sb->tcon, | ||
1506 | open_file->netfid, | ||
1507 | bytes_to_write, offset, | ||
1508 | &bytes_written, iov, n_iov, | ||
1509 | long_op); | ||
1510 | cifsFileInfo_put(open_file); | ||
1511 | cifs_update_eof(cifsi, offset, bytes_written); | ||
1512 | |||
1513 | if (rc || bytes_written < bytes_to_write) { | ||
1514 | cERROR(1, "Write2 ret %d, wrote %d", | ||
1515 | rc, bytes_written); | ||
1516 | /* BB what if continued retry is | ||
1517 | requested via mount flags? */ | ||
1518 | if (rc == -ENOSPC) | ||
1519 | set_bit(AS_ENOSPC, &mapping->flags); | ||
1520 | else | ||
1521 | set_bit(AS_EIO, &mapping->flags); | ||
1522 | } else { | ||
1523 | cifs_stats_bytes_written(cifs_sb->tcon, | ||
1524 | bytes_written); | ||
1525 | } | ||
1526 | } | 1251 | } |
1527 | for (i = 0; i < n_iov; i++) { | 1252 | rc = cifs_async_writev(wdata); |
1528 | page = pvec.pages[first + i]; | 1253 | } while (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN); |
1529 | /* Should we also set page error on | 1254 | |
1530 | success rc but too little data written? */ | 1255 | for (i = 0; i < nr_pages; ++i) |
1531 | /* BB investigate retry logic on temporary | 1256 | unlock_page(wdata->pages[i]); |
1532 | server crash cases and how recovery works | 1257 | |
1533 | when page marked as error */ | 1258 | /* send failure -- clean up the mess */ |
1534 | if (rc) | 1259 | if (rc != 0) { |
1535 | SetPageError(page); | 1260 | for (i = 0; i < nr_pages; ++i) { |
1536 | kunmap(page); | 1261 | if (rc == -EAGAIN) |
1537 | unlock_page(page); | 1262 | redirty_page_for_writepage(wbc, |
1538 | end_page_writeback(page); | 1263 | wdata->pages[i]); |
1539 | page_cache_release(page); | 1264 | else |
1265 | SetPageError(wdata->pages[i]); | ||
1266 | end_page_writeback(wdata->pages[i]); | ||
1267 | page_cache_release(wdata->pages[i]); | ||
1540 | } | 1268 | } |
1541 | if ((wbc->nr_to_write -= n_iov) <= 0) | 1269 | if (rc != -EAGAIN) |
1542 | done = 1; | 1270 | mapping_set_error(mapping, rc); |
1543 | index = next; | 1271 | } |
1544 | } else | 1272 | kref_put(&wdata->refcount, cifs_writedata_release); |
1545 | /* Need to re-find the pages we skipped */ | 1273 | |
1546 | index = pvec.pages[0]->index + 1; | 1274 | wbc->nr_to_write -= nr_pages; |
1275 | if (wbc->nr_to_write <= 0) | ||
1276 | done = true; | ||
1547 | 1277 | ||
1548 | pagevec_release(&pvec); | 1278 | index = next; |
1549 | } | 1279 | } |
1280 | |||
1550 | if (!scanned && !done) { | 1281 | if (!scanned && !done) { |
1551 | /* | 1282 | /* |
1552 | * We hit the last page and there is more work to be done: wrap | 1283 | * We hit the last page and there is more work to be done: wrap |
1553 | * back to the start of the file | 1284 | * back to the start of the file |
1554 | */ | 1285 | */ |
1555 | scanned = 1; | 1286 | scanned = true; |
1556 | index = 0; | 1287 | index = 0; |
1557 | goto retry; | 1288 | goto retry; |
1558 | } | 1289 | } |
1290 | |||
1559 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) | 1291 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
1560 | mapping->writeback_index = index; | 1292 | mapping->writeback_index = index; |
1561 | 1293 | ||
1562 | FreeXid(xid); | ||
1563 | kfree(iov); | ||
1564 | return rc; | 1294 | return rc; |
1565 | } | 1295 | } |
1566 | 1296 | ||
1567 | static int cifs_writepage(struct page *page, struct writeback_control *wbc) | 1297 | static int |
1298 | cifs_writepage_locked(struct page *page, struct writeback_control *wbc) | ||
1568 | { | 1299 | { |
1569 | int rc = -EFAULT; | 1300 | int rc; |
1570 | int xid; | 1301 | int xid; |
1571 | 1302 | ||
1572 | xid = GetXid(); | 1303 | xid = GetXid(); |
@@ -1586,21 +1317,43 @@ static int cifs_writepage(struct page *page, struct writeback_control *wbc) | |||
1586 | * to fail to update with the state of the page correctly. | 1317 | * to fail to update with the state of the page correctly. |
1587 | */ | 1318 | */ |
1588 | set_page_writeback(page); | 1319 | set_page_writeback(page); |
1320 | retry_write: | ||
1589 | rc = cifs_partialpagewrite(page, 0, PAGE_CACHE_SIZE); | 1321 | rc = cifs_partialpagewrite(page, 0, PAGE_CACHE_SIZE); |
1590 | SetPageUptodate(page); /* BB add check for error and Clearuptodate? */ | 1322 | if (rc == -EAGAIN && wbc->sync_mode == WB_SYNC_ALL) |
1591 | unlock_page(page); | 1323 | goto retry_write; |
1324 | else if (rc == -EAGAIN) | ||
1325 | redirty_page_for_writepage(wbc, page); | ||
1326 | else if (rc != 0) | ||
1327 | SetPageError(page); | ||
1328 | else | ||
1329 | SetPageUptodate(page); | ||
1592 | end_page_writeback(page); | 1330 | end_page_writeback(page); |
1593 | page_cache_release(page); | 1331 | page_cache_release(page); |
1594 | FreeXid(xid); | 1332 | FreeXid(xid); |
1595 | return rc; | 1333 | return rc; |
1596 | } | 1334 | } |
1597 | 1335 | ||
1336 | static int cifs_writepage(struct page *page, struct writeback_control *wbc) | ||
1337 | { | ||
1338 | int rc = cifs_writepage_locked(page, wbc); | ||
1339 | unlock_page(page); | ||
1340 | return rc; | ||
1341 | } | ||
1342 | |||
1598 | static int cifs_write_end(struct file *file, struct address_space *mapping, | 1343 | static int cifs_write_end(struct file *file, struct address_space *mapping, |
1599 | loff_t pos, unsigned len, unsigned copied, | 1344 | loff_t pos, unsigned len, unsigned copied, |
1600 | struct page *page, void *fsdata) | 1345 | struct page *page, void *fsdata) |
1601 | { | 1346 | { |
1602 | int rc; | 1347 | int rc; |
1603 | struct inode *inode = mapping->host; | 1348 | struct inode *inode = mapping->host; |
1349 | struct cifsFileInfo *cfile = file->private_data; | ||
1350 | struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->dentry->d_sb); | ||
1351 | __u32 pid; | ||
1352 | |||
1353 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) | ||
1354 | pid = cfile->pid; | ||
1355 | else | ||
1356 | pid = current->tgid; | ||
1604 | 1357 | ||
1605 | cFYI(1, "write_end for page %p from pos %lld with %d bytes", | 1358 | cFYI(1, "write_end for page %p from pos %lld with %d bytes", |
1606 | page, pos, copied); | 1359 | page, pos, copied); |
@@ -1624,7 +1377,7 @@ static int cifs_write_end(struct file *file, struct address_space *mapping, | |||
1624 | /* BB check if anything else missing out of ppw | 1377 | /* BB check if anything else missing out of ppw |
1625 | such as updating last write time */ | 1378 | such as updating last write time */ |
1626 | page_data = kmap(page); | 1379 | page_data = kmap(page); |
1627 | rc = cifs_write(file, page_data + offset, copied, &pos); | 1380 | rc = cifs_write(cfile, pid, page_data + offset, copied, &pos); |
1628 | /* if (rc < 0) should we set writebehind rc? */ | 1381 | /* if (rc < 0) should we set writebehind rc? */ |
1629 | kunmap(page); | 1382 | kunmap(page); |
1630 | 1383 | ||
@@ -1648,60 +1401,56 @@ static int cifs_write_end(struct file *file, struct address_space *mapping, | |||
1648 | return rc; | 1401 | return rc; |
1649 | } | 1402 | } |
1650 | 1403 | ||
1651 | int cifs_fsync(struct file *file, int datasync) | 1404 | int cifs_strict_fsync(struct file *file, int datasync) |
1652 | { | 1405 | { |
1653 | int xid; | 1406 | int xid; |
1654 | int rc = 0; | 1407 | int rc = 0; |
1655 | struct cifsTconInfo *tcon; | 1408 | struct cifs_tcon *tcon; |
1656 | struct cifsFileInfo *smbfile = file->private_data; | 1409 | struct cifsFileInfo *smbfile = file->private_data; |
1657 | struct inode *inode = file->f_path.dentry->d_inode; | 1410 | struct inode *inode = file->f_path.dentry->d_inode; |
1411 | struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb); | ||
1658 | 1412 | ||
1659 | xid = GetXid(); | 1413 | xid = GetXid(); |
1660 | 1414 | ||
1661 | cFYI(1, "Sync file - name: %s datasync: 0x%x", | 1415 | cFYI(1, "Sync file - name: %s datasync: 0x%x", |
1662 | file->f_path.dentry->d_name.name, datasync); | 1416 | file->f_path.dentry->d_name.name, datasync); |
1663 | 1417 | ||
1664 | rc = filemap_write_and_wait(inode->i_mapping); | 1418 | if (!CIFS_I(inode)->clientCanCacheRead) { |
1665 | if (rc == 0) { | 1419 | rc = cifs_invalidate_mapping(inode); |
1666 | rc = CIFS_I(inode)->write_behind_rc; | 1420 | if (rc) { |
1667 | CIFS_I(inode)->write_behind_rc = 0; | 1421 | cFYI(1, "rc: %d during invalidate phase", rc); |
1668 | tcon = CIFS_SB(inode->i_sb)->tcon; | 1422 | rc = 0; /* don't care about it in fsync */ |
1669 | if (!rc && tcon && smbfile && | 1423 | } |
1670 | !(CIFS_SB(inode->i_sb)->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) | ||
1671 | rc = CIFSSMBFlush(xid, tcon, smbfile->netfid); | ||
1672 | } | 1424 | } |
1673 | 1425 | ||
1426 | tcon = tlink_tcon(smbfile->tlink); | ||
1427 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) | ||
1428 | rc = CIFSSMBFlush(xid, tcon, smbfile->netfid); | ||
1429 | |||
1674 | FreeXid(xid); | 1430 | FreeXid(xid); |
1675 | return rc; | 1431 | return rc; |
1676 | } | 1432 | } |
1677 | 1433 | ||
1678 | /* static void cifs_sync_page(struct page *page) | 1434 | int cifs_fsync(struct file *file, int datasync) |
1679 | { | 1435 | { |
1680 | struct address_space *mapping; | 1436 | int xid; |
1681 | struct inode *inode; | ||
1682 | unsigned long index = page->index; | ||
1683 | unsigned int rpages = 0; | ||
1684 | int rc = 0; | 1437 | int rc = 0; |
1438 | struct cifs_tcon *tcon; | ||
1439 | struct cifsFileInfo *smbfile = file->private_data; | ||
1440 | struct cifs_sb_info *cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); | ||
1685 | 1441 | ||
1686 | cFYI(1, "sync page %p", page); | 1442 | xid = GetXid(); |
1687 | mapping = page->mapping; | ||
1688 | if (!mapping) | ||
1689 | return 0; | ||
1690 | inode = mapping->host; | ||
1691 | if (!inode) | ||
1692 | return; */ | ||
1693 | 1443 | ||
1694 | /* fill in rpages then | 1444 | cFYI(1, "Sync file - name: %s datasync: 0x%x", |
1695 | result = cifs_pagein_inode(inode, index, rpages); */ /* BB finish */ | 1445 | file->f_path.dentry->d_name.name, datasync); |
1696 | 1446 | ||
1697 | /* cFYI(1, "rpages is %d for sync page of Index %ld", rpages, index); | 1447 | tcon = tlink_tcon(smbfile->tlink); |
1448 | if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NOSSYNC)) | ||
1449 | rc = CIFSSMBFlush(xid, tcon, smbfile->netfid); | ||
1698 | 1450 | ||
1699 | #if 0 | 1451 | FreeXid(xid); |
1700 | if (rc < 0) | 1452 | return rc; |
1701 | return rc; | 1453 | } |
1702 | return 0; | ||
1703 | #endif | ||
1704 | } */ | ||
1705 | 1454 | ||
1706 | /* | 1455 | /* |
1707 | * As file closes, flush all cached write data for this inode checking | 1456 | * As file closes, flush all cached write data for this inode checking |
@@ -1712,92 +1461,301 @@ int cifs_flush(struct file *file, fl_owner_t id) | |||
1712 | struct inode *inode = file->f_path.dentry->d_inode; | 1461 | struct inode *inode = file->f_path.dentry->d_inode; |
1713 | int rc = 0; | 1462 | int rc = 0; |
1714 | 1463 | ||
1715 | /* Rather than do the steps manually: | 1464 | if (file->f_mode & FMODE_WRITE) |
1716 | lock the inode for writing | 1465 | rc = filemap_write_and_wait(inode->i_mapping); |
1717 | loop through pages looking for write behind data (dirty pages) | ||
1718 | coalesce into contiguous 16K (or smaller) chunks to write to server | ||
1719 | send to server (prefer in parallel) | ||
1720 | deal with writebehind errors | ||
1721 | unlock inode for writing | ||
1722 | filemapfdatawrite appears easier for the time being */ | ||
1723 | |||
1724 | rc = filemap_fdatawrite(inode->i_mapping); | ||
1725 | /* reset wb rc if we were able to write out dirty pages */ | ||
1726 | if (!rc) { | ||
1727 | rc = CIFS_I(inode)->write_behind_rc; | ||
1728 | CIFS_I(inode)->write_behind_rc = 0; | ||
1729 | } | ||
1730 | 1466 | ||
1731 | cFYI(1, "Flush inode %p file %p rc %d", inode, file, rc); | 1467 | cFYI(1, "Flush inode %p file %p rc %d", inode, file, rc); |
1732 | 1468 | ||
1733 | return rc; | 1469 | return rc; |
1734 | } | 1470 | } |
1735 | 1471 | ||
1736 | ssize_t cifs_user_read(struct file *file, char __user *read_data, | 1472 | static int |
1737 | size_t read_size, loff_t *poffset) | 1473 | cifs_write_allocate_pages(struct page **pages, unsigned long num_pages) |
1738 | { | 1474 | { |
1739 | int rc = -EACCES; | 1475 | int rc = 0; |
1740 | unsigned int bytes_read = 0; | 1476 | unsigned long i; |
1741 | unsigned int total_read = 0; | 1477 | |
1742 | unsigned int current_read_size; | 1478 | for (i = 0; i < num_pages; i++) { |
1479 | pages[i] = alloc_page(__GFP_HIGHMEM); | ||
1480 | if (!pages[i]) { | ||
1481 | /* | ||
1482 | * save number of pages we have already allocated and | ||
1483 | * return with ENOMEM error | ||
1484 | */ | ||
1485 | num_pages = i; | ||
1486 | rc = -ENOMEM; | ||
1487 | goto error; | ||
1488 | } | ||
1489 | } | ||
1490 | |||
1491 | return rc; | ||
1492 | |||
1493 | error: | ||
1494 | for (i = 0; i < num_pages; i++) | ||
1495 | put_page(pages[i]); | ||
1496 | return rc; | ||
1497 | } | ||
1498 | |||
1499 | static inline | ||
1500 | size_t get_numpages(const size_t wsize, const size_t len, size_t *cur_len) | ||
1501 | { | ||
1502 | size_t num_pages; | ||
1503 | size_t clen; | ||
1504 | |||
1505 | clen = min_t(const size_t, len, wsize); | ||
1506 | num_pages = clen / PAGE_CACHE_SIZE; | ||
1507 | if (clen % PAGE_CACHE_SIZE) | ||
1508 | num_pages++; | ||
1509 | |||
1510 | if (cur_len) | ||
1511 | *cur_len = clen; | ||
1512 | |||
1513 | return num_pages; | ||
1514 | } | ||
1515 | |||
1516 | static ssize_t | ||
1517 | cifs_iovec_write(struct file *file, const struct iovec *iov, | ||
1518 | unsigned long nr_segs, loff_t *poffset) | ||
1519 | { | ||
1520 | unsigned int written; | ||
1521 | unsigned long num_pages, npages, i; | ||
1522 | size_t copied, len, cur_len; | ||
1523 | ssize_t total_written = 0; | ||
1524 | struct kvec *to_send; | ||
1525 | struct page **pages; | ||
1526 | struct iov_iter it; | ||
1527 | struct inode *inode; | ||
1528 | struct cifsFileInfo *open_file; | ||
1529 | struct cifs_tcon *pTcon; | ||
1743 | struct cifs_sb_info *cifs_sb; | 1530 | struct cifs_sb_info *cifs_sb; |
1744 | struct cifsTconInfo *pTcon; | 1531 | struct cifs_io_parms io_parms; |
1532 | int xid, rc; | ||
1533 | __u32 pid; | ||
1534 | |||
1535 | len = iov_length(iov, nr_segs); | ||
1536 | if (!len) | ||
1537 | return 0; | ||
1538 | |||
1539 | rc = generic_write_checks(file, poffset, &len, 0); | ||
1540 | if (rc) | ||
1541 | return rc; | ||
1542 | |||
1543 | cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); | ||
1544 | num_pages = get_numpages(cifs_sb->wsize, len, &cur_len); | ||
1545 | |||
1546 | pages = kmalloc(sizeof(struct pages *)*num_pages, GFP_KERNEL); | ||
1547 | if (!pages) | ||
1548 | return -ENOMEM; | ||
1549 | |||
1550 | to_send = kmalloc(sizeof(struct kvec)*(num_pages + 1), GFP_KERNEL); | ||
1551 | if (!to_send) { | ||
1552 | kfree(pages); | ||
1553 | return -ENOMEM; | ||
1554 | } | ||
1555 | |||
1556 | rc = cifs_write_allocate_pages(pages, num_pages); | ||
1557 | if (rc) { | ||
1558 | kfree(pages); | ||
1559 | kfree(to_send); | ||
1560 | return rc; | ||
1561 | } | ||
1562 | |||
1563 | xid = GetXid(); | ||
1564 | open_file = file->private_data; | ||
1565 | |||
1566 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) | ||
1567 | pid = open_file->pid; | ||
1568 | else | ||
1569 | pid = current->tgid; | ||
1570 | |||
1571 | pTcon = tlink_tcon(open_file->tlink); | ||
1572 | inode = file->f_path.dentry->d_inode; | ||
1573 | |||
1574 | iov_iter_init(&it, iov, nr_segs, len, 0); | ||
1575 | npages = num_pages; | ||
1576 | |||
1577 | do { | ||
1578 | size_t save_len = cur_len; | ||
1579 | for (i = 0; i < npages; i++) { | ||
1580 | copied = min_t(const size_t, cur_len, PAGE_CACHE_SIZE); | ||
1581 | copied = iov_iter_copy_from_user(pages[i], &it, 0, | ||
1582 | copied); | ||
1583 | cur_len -= copied; | ||
1584 | iov_iter_advance(&it, copied); | ||
1585 | to_send[i+1].iov_base = kmap(pages[i]); | ||
1586 | to_send[i+1].iov_len = copied; | ||
1587 | } | ||
1588 | |||
1589 | cur_len = save_len - cur_len; | ||
1590 | |||
1591 | do { | ||
1592 | if (open_file->invalidHandle) { | ||
1593 | rc = cifs_reopen_file(open_file, false); | ||
1594 | if (rc != 0) | ||
1595 | break; | ||
1596 | } | ||
1597 | io_parms.netfid = open_file->netfid; | ||
1598 | io_parms.pid = pid; | ||
1599 | io_parms.tcon = pTcon; | ||
1600 | io_parms.offset = *poffset; | ||
1601 | io_parms.length = cur_len; | ||
1602 | rc = CIFSSMBWrite2(xid, &io_parms, &written, to_send, | ||
1603 | npages, 0); | ||
1604 | } while (rc == -EAGAIN); | ||
1605 | |||
1606 | for (i = 0; i < npages; i++) | ||
1607 | kunmap(pages[i]); | ||
1608 | |||
1609 | if (written) { | ||
1610 | len -= written; | ||
1611 | total_written += written; | ||
1612 | cifs_update_eof(CIFS_I(inode), *poffset, written); | ||
1613 | *poffset += written; | ||
1614 | } else if (rc < 0) { | ||
1615 | if (!total_written) | ||
1616 | total_written = rc; | ||
1617 | break; | ||
1618 | } | ||
1619 | |||
1620 | /* get length and number of kvecs of the next write */ | ||
1621 | npages = get_numpages(cifs_sb->wsize, len, &cur_len); | ||
1622 | } while (len > 0); | ||
1623 | |||
1624 | if (total_written > 0) { | ||
1625 | spin_lock(&inode->i_lock); | ||
1626 | if (*poffset > inode->i_size) | ||
1627 | i_size_write(inode, *poffset); | ||
1628 | spin_unlock(&inode->i_lock); | ||
1629 | } | ||
1630 | |||
1631 | cifs_stats_bytes_written(pTcon, total_written); | ||
1632 | mark_inode_dirty_sync(inode); | ||
1633 | |||
1634 | for (i = 0; i < num_pages; i++) | ||
1635 | put_page(pages[i]); | ||
1636 | kfree(to_send); | ||
1637 | kfree(pages); | ||
1638 | FreeXid(xid); | ||
1639 | return total_written; | ||
1640 | } | ||
1641 | |||
1642 | ssize_t cifs_user_writev(struct kiocb *iocb, const struct iovec *iov, | ||
1643 | unsigned long nr_segs, loff_t pos) | ||
1644 | { | ||
1645 | ssize_t written; | ||
1646 | struct inode *inode; | ||
1647 | |||
1648 | inode = iocb->ki_filp->f_path.dentry->d_inode; | ||
1649 | |||
1650 | /* | ||
1651 | * BB - optimize the way when signing is disabled. We can drop this | ||
1652 | * extra memory-to-memory copying and use iovec buffers for constructing | ||
1653 | * write request. | ||
1654 | */ | ||
1655 | |||
1656 | written = cifs_iovec_write(iocb->ki_filp, iov, nr_segs, &pos); | ||
1657 | if (written > 0) { | ||
1658 | CIFS_I(inode)->invalid_mapping = true; | ||
1659 | iocb->ki_pos = pos; | ||
1660 | } | ||
1661 | |||
1662 | return written; | ||
1663 | } | ||
1664 | |||
1665 | ssize_t cifs_strict_writev(struct kiocb *iocb, const struct iovec *iov, | ||
1666 | unsigned long nr_segs, loff_t pos) | ||
1667 | { | ||
1668 | struct inode *inode; | ||
1669 | |||
1670 | inode = iocb->ki_filp->f_path.dentry->d_inode; | ||
1671 | |||
1672 | if (CIFS_I(inode)->clientCanCacheAll) | ||
1673 | return generic_file_aio_write(iocb, iov, nr_segs, pos); | ||
1674 | |||
1675 | /* | ||
1676 | * In strict cache mode we need to write the data to the server exactly | ||
1677 | * from the pos to pos+len-1 rather than flush all affected pages | ||
1678 | * because it may cause a error with mandatory locks on these pages but | ||
1679 | * not on the region from pos to ppos+len-1. | ||
1680 | */ | ||
1681 | |||
1682 | return cifs_user_writev(iocb, iov, nr_segs, pos); | ||
1683 | } | ||
1684 | |||
1685 | static ssize_t | ||
1686 | cifs_iovec_read(struct file *file, const struct iovec *iov, | ||
1687 | unsigned long nr_segs, loff_t *poffset) | ||
1688 | { | ||
1689 | int rc; | ||
1745 | int xid; | 1690 | int xid; |
1691 | ssize_t total_read; | ||
1692 | unsigned int bytes_read = 0; | ||
1693 | size_t len, cur_len; | ||
1694 | int iov_offset = 0; | ||
1695 | struct cifs_sb_info *cifs_sb; | ||
1696 | struct cifs_tcon *pTcon; | ||
1746 | struct cifsFileInfo *open_file; | 1697 | struct cifsFileInfo *open_file; |
1747 | char *smb_read_data; | ||
1748 | char __user *current_offset; | ||
1749 | struct smb_com_read_rsp *pSMBr; | 1698 | struct smb_com_read_rsp *pSMBr; |
1699 | struct cifs_io_parms io_parms; | ||
1700 | char *read_data; | ||
1701 | __u32 pid; | ||
1702 | |||
1703 | if (!nr_segs) | ||
1704 | return 0; | ||
1705 | |||
1706 | len = iov_length(iov, nr_segs); | ||
1707 | if (!len) | ||
1708 | return 0; | ||
1750 | 1709 | ||
1751 | xid = GetXid(); | 1710 | xid = GetXid(); |
1752 | cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); | 1711 | cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); |
1753 | pTcon = cifs_sb->tcon; | ||
1754 | 1712 | ||
1755 | if (file->private_data == NULL) { | ||
1756 | rc = -EBADF; | ||
1757 | FreeXid(xid); | ||
1758 | return rc; | ||
1759 | } | ||
1760 | open_file = file->private_data; | 1713 | open_file = file->private_data; |
1714 | pTcon = tlink_tcon(open_file->tlink); | ||
1715 | |||
1716 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) | ||
1717 | pid = open_file->pid; | ||
1718 | else | ||
1719 | pid = current->tgid; | ||
1761 | 1720 | ||
1762 | if ((file->f_flags & O_ACCMODE) == O_WRONLY) | 1721 | if ((file->f_flags & O_ACCMODE) == O_WRONLY) |
1763 | cFYI(1, "attempting read on write only file instance"); | 1722 | cFYI(1, "attempting read on write only file instance"); |
1764 | 1723 | ||
1765 | for (total_read = 0, current_offset = read_data; | 1724 | for (total_read = 0; total_read < len; total_read += bytes_read) { |
1766 | read_size > total_read; | 1725 | cur_len = min_t(const size_t, len - total_read, cifs_sb->rsize); |
1767 | total_read += bytes_read, current_offset += bytes_read) { | ||
1768 | current_read_size = min_t(const int, read_size - total_read, | ||
1769 | cifs_sb->rsize); | ||
1770 | rc = -EAGAIN; | 1726 | rc = -EAGAIN; |
1771 | smb_read_data = NULL; | 1727 | read_data = NULL; |
1728 | |||
1772 | while (rc == -EAGAIN) { | 1729 | while (rc == -EAGAIN) { |
1773 | int buf_type = CIFS_NO_BUFFER; | 1730 | int buf_type = CIFS_NO_BUFFER; |
1774 | if ((open_file->invalidHandle) && | 1731 | if (open_file->invalidHandle) { |
1775 | (!open_file->closePend)) { | 1732 | rc = cifs_reopen_file(open_file, true); |
1776 | rc = cifs_reopen_file(file, true); | ||
1777 | if (rc != 0) | 1733 | if (rc != 0) |
1778 | break; | 1734 | break; |
1779 | } | 1735 | } |
1780 | rc = CIFSSMBRead(xid, pTcon, | 1736 | io_parms.netfid = open_file->netfid; |
1781 | open_file->netfid, | 1737 | io_parms.pid = pid; |
1782 | current_read_size, *poffset, | 1738 | io_parms.tcon = pTcon; |
1783 | &bytes_read, &smb_read_data, | 1739 | io_parms.offset = *poffset; |
1784 | &buf_type); | 1740 | io_parms.length = cur_len; |
1785 | pSMBr = (struct smb_com_read_rsp *)smb_read_data; | 1741 | rc = CIFSSMBRead(xid, &io_parms, &bytes_read, |
1786 | if (smb_read_data) { | 1742 | &read_data, &buf_type); |
1787 | if (copy_to_user(current_offset, | 1743 | pSMBr = (struct smb_com_read_rsp *)read_data; |
1788 | smb_read_data + | 1744 | if (read_data) { |
1789 | 4 /* RFC1001 length field */ + | 1745 | char *data_offset = read_data + 4 + |
1790 | le16_to_cpu(pSMBr->DataOffset), | 1746 | le16_to_cpu(pSMBr->DataOffset); |
1791 | bytes_read)) | 1747 | if (memcpy_toiovecend(iov, data_offset, |
1748 | iov_offset, bytes_read)) | ||
1792 | rc = -EFAULT; | 1749 | rc = -EFAULT; |
1793 | |||
1794 | if (buf_type == CIFS_SMALL_BUFFER) | 1750 | if (buf_type == CIFS_SMALL_BUFFER) |
1795 | cifs_small_buf_release(smb_read_data); | 1751 | cifs_small_buf_release(read_data); |
1796 | else if (buf_type == CIFS_LARGE_BUFFER) | 1752 | else if (buf_type == CIFS_LARGE_BUFFER) |
1797 | cifs_buf_release(smb_read_data); | 1753 | cifs_buf_release(read_data); |
1798 | smb_read_data = NULL; | 1754 | read_data = NULL; |
1755 | iov_offset += bytes_read; | ||
1799 | } | 1756 | } |
1800 | } | 1757 | } |
1758 | |||
1801 | if (rc || (bytes_read == 0)) { | 1759 | if (rc || (bytes_read == 0)) { |
1802 | if (total_read) { | 1760 | if (total_read) { |
1803 | break; | 1761 | break; |
@@ -1810,28 +1768,63 @@ ssize_t cifs_user_read(struct file *file, char __user *read_data, | |||
1810 | *poffset += bytes_read; | 1768 | *poffset += bytes_read; |
1811 | } | 1769 | } |
1812 | } | 1770 | } |
1771 | |||
1813 | FreeXid(xid); | 1772 | FreeXid(xid); |
1814 | return total_read; | 1773 | return total_read; |
1815 | } | 1774 | } |
1816 | 1775 | ||
1776 | ssize_t cifs_user_readv(struct kiocb *iocb, const struct iovec *iov, | ||
1777 | unsigned long nr_segs, loff_t pos) | ||
1778 | { | ||
1779 | ssize_t read; | ||
1780 | |||
1781 | read = cifs_iovec_read(iocb->ki_filp, iov, nr_segs, &pos); | ||
1782 | if (read > 0) | ||
1783 | iocb->ki_pos = pos; | ||
1784 | |||
1785 | return read; | ||
1786 | } | ||
1787 | |||
1788 | ssize_t cifs_strict_readv(struct kiocb *iocb, const struct iovec *iov, | ||
1789 | unsigned long nr_segs, loff_t pos) | ||
1790 | { | ||
1791 | struct inode *inode; | ||
1792 | |||
1793 | inode = iocb->ki_filp->f_path.dentry->d_inode; | ||
1794 | |||
1795 | if (CIFS_I(inode)->clientCanCacheRead) | ||
1796 | return generic_file_aio_read(iocb, iov, nr_segs, pos); | ||
1797 | |||
1798 | /* | ||
1799 | * In strict cache mode we need to read from the server all the time | ||
1800 | * if we don't have level II oplock because the server can delay mtime | ||
1801 | * change - so we can't make a decision about inode invalidating. | ||
1802 | * And we can also fail with pagereading if there are mandatory locks | ||
1803 | * on pages affected by this read but not on the region from pos to | ||
1804 | * pos+len-1. | ||
1805 | */ | ||
1806 | |||
1807 | return cifs_user_readv(iocb, iov, nr_segs, pos); | ||
1808 | } | ||
1817 | 1809 | ||
1818 | static ssize_t cifs_read(struct file *file, char *read_data, size_t read_size, | 1810 | static ssize_t cifs_read(struct file *file, char *read_data, size_t read_size, |
1819 | loff_t *poffset) | 1811 | loff_t *poffset) |
1820 | { | 1812 | { |
1821 | int rc = -EACCES; | 1813 | int rc = -EACCES; |
1822 | unsigned int bytes_read = 0; | 1814 | unsigned int bytes_read = 0; |
1823 | unsigned int total_read; | 1815 | unsigned int total_read; |
1824 | unsigned int current_read_size; | 1816 | unsigned int current_read_size; |
1825 | struct cifs_sb_info *cifs_sb; | 1817 | struct cifs_sb_info *cifs_sb; |
1826 | struct cifsTconInfo *pTcon; | 1818 | struct cifs_tcon *pTcon; |
1827 | int xid; | 1819 | int xid; |
1828 | char *current_offset; | 1820 | char *current_offset; |
1829 | struct cifsFileInfo *open_file; | 1821 | struct cifsFileInfo *open_file; |
1822 | struct cifs_io_parms io_parms; | ||
1830 | int buf_type = CIFS_NO_BUFFER; | 1823 | int buf_type = CIFS_NO_BUFFER; |
1824 | __u32 pid; | ||
1831 | 1825 | ||
1832 | xid = GetXid(); | 1826 | xid = GetXid(); |
1833 | cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); | 1827 | cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); |
1834 | pTcon = cifs_sb->tcon; | ||
1835 | 1828 | ||
1836 | if (file->private_data == NULL) { | 1829 | if (file->private_data == NULL) { |
1837 | rc = -EBADF; | 1830 | rc = -EBADF; |
@@ -1839,6 +1832,12 @@ static ssize_t cifs_read(struct file *file, char *read_data, size_t read_size, | |||
1839 | return rc; | 1832 | return rc; |
1840 | } | 1833 | } |
1841 | open_file = file->private_data; | 1834 | open_file = file->private_data; |
1835 | pTcon = tlink_tcon(open_file->tlink); | ||
1836 | |||
1837 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) | ||
1838 | pid = open_file->pid; | ||
1839 | else | ||
1840 | pid = current->tgid; | ||
1842 | 1841 | ||
1843 | if ((file->f_flags & O_ACCMODE) == O_WRONLY) | 1842 | if ((file->f_flags & O_ACCMODE) == O_WRONLY) |
1844 | cFYI(1, "attempting read on write only file instance"); | 1843 | cFYI(1, "attempting read on write only file instance"); |
@@ -1857,17 +1856,18 @@ static ssize_t cifs_read(struct file *file, char *read_data, size_t read_size, | |||
1857 | } | 1856 | } |
1858 | rc = -EAGAIN; | 1857 | rc = -EAGAIN; |
1859 | while (rc == -EAGAIN) { | 1858 | while (rc == -EAGAIN) { |
1860 | if ((open_file->invalidHandle) && | 1859 | if (open_file->invalidHandle) { |
1861 | (!open_file->closePend)) { | 1860 | rc = cifs_reopen_file(open_file, true); |
1862 | rc = cifs_reopen_file(file, true); | ||
1863 | if (rc != 0) | 1861 | if (rc != 0) |
1864 | break; | 1862 | break; |
1865 | } | 1863 | } |
1866 | rc = CIFSSMBRead(xid, pTcon, | 1864 | io_parms.netfid = open_file->netfid; |
1867 | open_file->netfid, | 1865 | io_parms.pid = pid; |
1868 | current_read_size, *poffset, | 1866 | io_parms.tcon = pTcon; |
1869 | &bytes_read, ¤t_offset, | 1867 | io_parms.offset = *poffset; |
1870 | &buf_type); | 1868 | io_parms.length = current_read_size; |
1869 | rc = CIFSSMBRead(xid, &io_parms, &bytes_read, | ||
1870 | ¤t_offset, &buf_type); | ||
1871 | } | 1871 | } |
1872 | if (rc || (bytes_read == 0)) { | 1872 | if (rc || (bytes_read == 0)) { |
1873 | if (total_read) { | 1873 | if (total_read) { |
@@ -1885,6 +1885,44 @@ static ssize_t cifs_read(struct file *file, char *read_data, size_t read_size, | |||
1885 | return total_read; | 1885 | return total_read; |
1886 | } | 1886 | } |
1887 | 1887 | ||
1888 | /* | ||
1889 | * If the page is mmap'ed into a process' page tables, then we need to make | ||
1890 | * sure that it doesn't change while being written back. | ||
1891 | */ | ||
1892 | static int | ||
1893 | cifs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) | ||
1894 | { | ||
1895 | struct page *page = vmf->page; | ||
1896 | |||
1897 | lock_page(page); | ||
1898 | return VM_FAULT_LOCKED; | ||
1899 | } | ||
1900 | |||
1901 | static struct vm_operations_struct cifs_file_vm_ops = { | ||
1902 | .fault = filemap_fault, | ||
1903 | .page_mkwrite = cifs_page_mkwrite, | ||
1904 | }; | ||
1905 | |||
1906 | int cifs_file_strict_mmap(struct file *file, struct vm_area_struct *vma) | ||
1907 | { | ||
1908 | int rc, xid; | ||
1909 | struct inode *inode = file->f_path.dentry->d_inode; | ||
1910 | |||
1911 | xid = GetXid(); | ||
1912 | |||
1913 | if (!CIFS_I(inode)->clientCanCacheRead) { | ||
1914 | rc = cifs_invalidate_mapping(inode); | ||
1915 | if (rc) | ||
1916 | return rc; | ||
1917 | } | ||
1918 | |||
1919 | rc = generic_file_mmap(file, vma); | ||
1920 | if (rc == 0) | ||
1921 | vma->vm_ops = &cifs_file_vm_ops; | ||
1922 | FreeXid(xid); | ||
1923 | return rc; | ||
1924 | } | ||
1925 | |||
1888 | int cifs_file_mmap(struct file *file, struct vm_area_struct *vma) | 1926 | int cifs_file_mmap(struct file *file, struct vm_area_struct *vma) |
1889 | { | 1927 | { |
1890 | int rc, xid; | 1928 | int rc, xid; |
@@ -1897,6 +1935,8 @@ int cifs_file_mmap(struct file *file, struct vm_area_struct *vma) | |||
1897 | return rc; | 1935 | return rc; |
1898 | } | 1936 | } |
1899 | rc = generic_file_mmap(file, vma); | 1937 | rc = generic_file_mmap(file, vma); |
1938 | if (rc == 0) | ||
1939 | vma->vm_ops = &cifs_file_vm_ops; | ||
1900 | FreeXid(xid); | 1940 | FreeXid(xid); |
1901 | return rc; | 1941 | return rc; |
1902 | } | 1942 | } |
@@ -1958,13 +1998,15 @@ static int cifs_readpages(struct file *file, struct address_space *mapping, | |||
1958 | loff_t offset; | 1998 | loff_t offset; |
1959 | struct page *page; | 1999 | struct page *page; |
1960 | struct cifs_sb_info *cifs_sb; | 2000 | struct cifs_sb_info *cifs_sb; |
1961 | struct cifsTconInfo *pTcon; | 2001 | struct cifs_tcon *pTcon; |
1962 | unsigned int bytes_read = 0; | 2002 | unsigned int bytes_read = 0; |
1963 | unsigned int read_size, i; | 2003 | unsigned int read_size, i; |
1964 | char *smb_read_data = NULL; | 2004 | char *smb_read_data = NULL; |
1965 | struct smb_com_read_rsp *pSMBr; | 2005 | struct smb_com_read_rsp *pSMBr; |
1966 | struct cifsFileInfo *open_file; | 2006 | struct cifsFileInfo *open_file; |
2007 | struct cifs_io_parms io_parms; | ||
1967 | int buf_type = CIFS_NO_BUFFER; | 2008 | int buf_type = CIFS_NO_BUFFER; |
2009 | __u32 pid; | ||
1968 | 2010 | ||
1969 | xid = GetXid(); | 2011 | xid = GetXid(); |
1970 | if (file->private_data == NULL) { | 2012 | if (file->private_data == NULL) { |
@@ -1974,7 +2016,7 @@ static int cifs_readpages(struct file *file, struct address_space *mapping, | |||
1974 | } | 2016 | } |
1975 | open_file = file->private_data; | 2017 | open_file = file->private_data; |
1976 | cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); | 2018 | cifs_sb = CIFS_SB(file->f_path.dentry->d_sb); |
1977 | pTcon = cifs_sb->tcon; | 2019 | pTcon = tlink_tcon(open_file->tlink); |
1978 | 2020 | ||
1979 | /* | 2021 | /* |
1980 | * Reads as many pages as possible from fscache. Returns -ENOBUFS | 2022 | * Reads as many pages as possible from fscache. Returns -ENOBUFS |
@@ -1986,6 +2028,11 @@ static int cifs_readpages(struct file *file, struct address_space *mapping, | |||
1986 | goto read_complete; | 2028 | goto read_complete; |
1987 | 2029 | ||
1988 | cFYI(DBG2, "rpages: num pages %d", num_pages); | 2030 | cFYI(DBG2, "rpages: num pages %d", num_pages); |
2031 | if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD) | ||
2032 | pid = open_file->pid; | ||
2033 | else | ||
2034 | pid = current->tgid; | ||
2035 | |||
1989 | for (i = 0; i < num_pages; ) { | 2036 | for (i = 0; i < num_pages; ) { |
1990 | unsigned contig_pages; | 2037 | unsigned contig_pages; |
1991 | struct page *tmp_page; | 2038 | struct page *tmp_page; |
@@ -2022,18 +2069,18 @@ static int cifs_readpages(struct file *file, struct address_space *mapping, | |||
2022 | read_size, contig_pages); | 2069 | read_size, contig_pages); |
2023 | rc = -EAGAIN; | 2070 | rc = -EAGAIN; |
2024 | while (rc == -EAGAIN) { | 2071 | while (rc == -EAGAIN) { |
2025 | if ((open_file->invalidHandle) && | 2072 | if (open_file->invalidHandle) { |
2026 | (!open_file->closePend)) { | 2073 | rc = cifs_reopen_file(open_file, true); |
2027 | rc = cifs_reopen_file(file, true); | ||
2028 | if (rc != 0) | 2074 | if (rc != 0) |
2029 | break; | 2075 | break; |
2030 | } | 2076 | } |
2031 | 2077 | io_parms.netfid = open_file->netfid; | |
2032 | rc = CIFSSMBRead(xid, pTcon, | 2078 | io_parms.pid = pid; |
2033 | open_file->netfid, | 2079 | io_parms.tcon = pTcon; |
2034 | read_size, offset, | 2080 | io_parms.offset = offset; |
2035 | &bytes_read, &smb_read_data, | 2081 | io_parms.length = read_size; |
2036 | &buf_type); | 2082 | rc = CIFSSMBRead(xid, &io_parms, &bytes_read, |
2083 | &smb_read_data, &buf_type); | ||
2037 | /* BB more RC checks ? */ | 2084 | /* BB more RC checks ? */ |
2038 | if (rc == -EAGAIN) { | 2085 | if (rc == -EAGAIN) { |
2039 | if (smb_read_data) { | 2086 | if (smb_read_data) { |
@@ -2173,18 +2220,14 @@ static int is_inode_writable(struct cifsInodeInfo *cifs_inode) | |||
2173 | { | 2220 | { |
2174 | struct cifsFileInfo *open_file; | 2221 | struct cifsFileInfo *open_file; |
2175 | 2222 | ||
2176 | read_lock(&GlobalSMBSeslock); | 2223 | spin_lock(&cifs_file_list_lock); |
2177 | list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { | 2224 | list_for_each_entry(open_file, &cifs_inode->openFileList, flist) { |
2178 | if (open_file->closePend) | 2225 | if (OPEN_FMODE(open_file->f_flags) & FMODE_WRITE) { |
2179 | continue; | 2226 | spin_unlock(&cifs_file_list_lock); |
2180 | if (open_file->pfile && | ||
2181 | ((open_file->pfile->f_flags & O_RDWR) || | ||
2182 | (open_file->pfile->f_flags & O_WRONLY))) { | ||
2183 | read_unlock(&GlobalSMBSeslock); | ||
2184 | return 1; | 2227 | return 1; |
2185 | } | 2228 | } |
2186 | } | 2229 | } |
2187 | read_unlock(&GlobalSMBSeslock); | 2230 | spin_unlock(&cifs_file_list_lock); |
2188 | return 0; | 2231 | return 0; |
2189 | } | 2232 | } |
2190 | 2233 | ||
@@ -2306,14 +2349,34 @@ static void cifs_invalidate_page(struct page *page, unsigned long offset) | |||
2306 | cifs_fscache_invalidate_page(page, &cifsi->vfs_inode); | 2349 | cifs_fscache_invalidate_page(page, &cifsi->vfs_inode); |
2307 | } | 2350 | } |
2308 | 2351 | ||
2352 | static int cifs_launder_page(struct page *page) | ||
2353 | { | ||
2354 | int rc = 0; | ||
2355 | loff_t range_start = page_offset(page); | ||
2356 | loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); | ||
2357 | struct writeback_control wbc = { | ||
2358 | .sync_mode = WB_SYNC_ALL, | ||
2359 | .nr_to_write = 0, | ||
2360 | .range_start = range_start, | ||
2361 | .range_end = range_end, | ||
2362 | }; | ||
2363 | |||
2364 | cFYI(1, "Launder page: %p", page); | ||
2365 | |||
2366 | if (clear_page_dirty_for_io(page)) | ||
2367 | rc = cifs_writepage_locked(page, &wbc); | ||
2368 | |||
2369 | cifs_fscache_invalidate_page(page, page->mapping->host); | ||
2370 | return rc; | ||
2371 | } | ||
2372 | |||
2309 | void cifs_oplock_break(struct work_struct *work) | 2373 | void cifs_oplock_break(struct work_struct *work) |
2310 | { | 2374 | { |
2311 | struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo, | 2375 | struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo, |
2312 | oplock_break); | 2376 | oplock_break); |
2313 | struct inode *inode = cfile->pInode; | 2377 | struct inode *inode = cfile->dentry->d_inode; |
2314 | struct cifsInodeInfo *cinode = CIFS_I(inode); | 2378 | struct cifsInodeInfo *cinode = CIFS_I(inode); |
2315 | struct cifs_sb_info *cifs_sb = CIFS_SB(cfile->mnt->mnt_sb); | 2379 | int rc = 0; |
2316 | int rc, waitrc = 0; | ||
2317 | 2380 | ||
2318 | if (inode && S_ISREG(inode->i_mode)) { | 2381 | if (inode && S_ISREG(inode->i_mode)) { |
2319 | if (cinode->clientCanCacheRead) | 2382 | if (cinode->clientCanCacheRead) |
@@ -2322,13 +2385,10 @@ void cifs_oplock_break(struct work_struct *work) | |||
2322 | break_lease(inode, O_WRONLY); | 2385 | break_lease(inode, O_WRONLY); |
2323 | rc = filemap_fdatawrite(inode->i_mapping); | 2386 | rc = filemap_fdatawrite(inode->i_mapping); |
2324 | if (cinode->clientCanCacheRead == 0) { | 2387 | if (cinode->clientCanCacheRead == 0) { |
2325 | waitrc = filemap_fdatawait(inode->i_mapping); | 2388 | rc = filemap_fdatawait(inode->i_mapping); |
2389 | mapping_set_error(inode->i_mapping, rc); | ||
2326 | invalidate_remote_inode(inode); | 2390 | invalidate_remote_inode(inode); |
2327 | } | 2391 | } |
2328 | if (!rc) | ||
2329 | rc = waitrc; | ||
2330 | if (rc) | ||
2331 | cinode->write_behind_rc = rc; | ||
2332 | cFYI(1, "Oplock flush inode %p rc %d", inode, rc); | 2392 | cFYI(1, "Oplock flush inode %p rc %d", inode, rc); |
2333 | } | 2393 | } |
2334 | 2394 | ||
@@ -2338,33 +2398,37 @@ void cifs_oplock_break(struct work_struct *work) | |||
2338 | * not bother sending an oplock release if session to server still is | 2398 | * not bother sending an oplock release if session to server still is |
2339 | * disconnected since oplock already released by the server | 2399 | * disconnected since oplock already released by the server |
2340 | */ | 2400 | */ |
2341 | if (!cfile->closePend && !cfile->oplock_break_cancelled) { | 2401 | if (!cfile->oplock_break_cancelled) { |
2342 | rc = CIFSSMBLock(0, cifs_sb->tcon, cfile->netfid, 0, 0, 0, 0, | 2402 | rc = CIFSSMBLock(0, tlink_tcon(cfile->tlink), cfile->netfid, 0, |
2343 | LOCKING_ANDX_OPLOCK_RELEASE, false); | 2403 | 0, 0, 0, LOCKING_ANDX_OPLOCK_RELEASE, false, |
2404 | cinode->clientCanCacheRead ? 1 : 0); | ||
2344 | cFYI(1, "Oplock release rc = %d", rc); | 2405 | cFYI(1, "Oplock release rc = %d", rc); |
2345 | } | 2406 | } |
2346 | 2407 | ||
2347 | /* | 2408 | /* |
2348 | * We might have kicked in before is_valid_oplock_break() | 2409 | * We might have kicked in before is_valid_oplock_break() |
2349 | * finished grabbing reference for us. Make sure it's done by | 2410 | * finished grabbing reference for us. Make sure it's done by |
2350 | * waiting for GlobalSMSSeslock. | 2411 | * waiting for cifs_file_list_lock. |
2351 | */ | 2412 | */ |
2352 | write_lock(&GlobalSMBSeslock); | 2413 | spin_lock(&cifs_file_list_lock); |
2353 | write_unlock(&GlobalSMBSeslock); | 2414 | spin_unlock(&cifs_file_list_lock); |
2354 | 2415 | ||
2355 | cifs_oplock_break_put(cfile); | 2416 | cifs_oplock_break_put(cfile); |
2356 | } | 2417 | } |
2357 | 2418 | ||
2419 | /* must be called while holding cifs_file_list_lock */ | ||
2358 | void cifs_oplock_break_get(struct cifsFileInfo *cfile) | 2420 | void cifs_oplock_break_get(struct cifsFileInfo *cfile) |
2359 | { | 2421 | { |
2360 | mntget(cfile->mnt); | 2422 | cifs_sb_active(cfile->dentry->d_sb); |
2361 | cifsFileInfo_get(cfile); | 2423 | cifsFileInfo_get(cfile); |
2362 | } | 2424 | } |
2363 | 2425 | ||
2364 | void cifs_oplock_break_put(struct cifsFileInfo *cfile) | 2426 | void cifs_oplock_break_put(struct cifsFileInfo *cfile) |
2365 | { | 2427 | { |
2366 | mntput(cfile->mnt); | 2428 | struct super_block *sb = cfile->dentry->d_sb; |
2429 | |||
2367 | cifsFileInfo_put(cfile); | 2430 | cifsFileInfo_put(cfile); |
2431 | cifs_sb_deactive(sb); | ||
2368 | } | 2432 | } |
2369 | 2433 | ||
2370 | const struct address_space_operations cifs_addr_ops = { | 2434 | const struct address_space_operations cifs_addr_ops = { |
@@ -2377,8 +2441,7 @@ const struct address_space_operations cifs_addr_ops = { | |||
2377 | .set_page_dirty = __set_page_dirty_nobuffers, | 2441 | .set_page_dirty = __set_page_dirty_nobuffers, |
2378 | .releasepage = cifs_release_page, | 2442 | .releasepage = cifs_release_page, |
2379 | .invalidatepage = cifs_invalidate_page, | 2443 | .invalidatepage = cifs_invalidate_page, |
2380 | /* .sync_page = cifs_sync_page, */ | 2444 | .launder_page = cifs_launder_page, |
2381 | /* .direct_IO = */ | ||
2382 | }; | 2445 | }; |
2383 | 2446 | ||
2384 | /* | 2447 | /* |
@@ -2395,6 +2458,5 @@ const struct address_space_operations cifs_addr_ops_smallbuf = { | |||
2395 | .set_page_dirty = __set_page_dirty_nobuffers, | 2458 | .set_page_dirty = __set_page_dirty_nobuffers, |
2396 | .releasepage = cifs_release_page, | 2459 | .releasepage = cifs_release_page, |
2397 | .invalidatepage = cifs_invalidate_page, | 2460 | .invalidatepage = cifs_invalidate_page, |
2398 | /* .sync_page = cifs_sync_page, */ | 2461 | .launder_page = cifs_launder_page, |
2399 | /* .direct_IO = */ | ||
2400 | }; | 2462 | }; |