Whamcloud - gitweb
Remove the locking warning added yesterday.
[fs/lustre-release.git] / lustre / llite / file.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/fs/ext2/file.c
5  *
6  * This code is issued under the GNU General Public License.
7  * See the file COPYING in this distribution
8  *
9  * Copyright (C) 1992, 1993, 1994, 1995
10  * Remy Card (card@masi.ibp.fr)
11  * Laboratoire MASI - Institut Blaise Pascal
12  * Universite Pierre et Marie Curie (Paris VI)
13  *
14  *  from
15  *
16  *  linux/fs/minix/file.c
17  *
18  *  Copyright (C) 1991, 1992  Linus Torvalds
19  *
20  *  ext2 fs regular file handling primitives
21  *
22  *  64-bit file support on 64-bit platforms by Jakub Jelinek
23  *      (jj@sunsite.ms.mff.cuni.cz)
24  */
25
26 #define DEBUG_SUBSYSTEM S_LLITE
27
28 #include <linux/lustre_dlm.h>
29 #include <linux/lustre_lite.h>
30
31 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc);
32 extern int ll_setattr(struct dentry *de, struct iattr *attr);
33
34 static int ll_file_open(struct inode *inode, struct file *file)
35 {
36         int rc;
37         struct ptlrpc_request *req = NULL;
38         struct ll_file_data *fd;
39         struct obdo *oa = NULL;
40         struct lov_stripe_md *lsm = NULL;
41         struct ll_sb_info *sbi = ll_i2sbi(inode);
42         struct ll_inode_info *lli = ll_i2info(inode);
43         ENTRY;
44
45         if (file->private_data)
46                 LBUG();
47
48         lsm = lli->lli_smd;
49
50         /*  delayed create of object (intent created inode) */
51         /*  XXX object needs to be cleaned up if mdc_open fails */
52         /*  XXX error handling appropriate here? */
53         if (lsm == NULL) {
54                 struct inode *inode = file->f_dentry->d_inode;
55
56                 down(&lli->lli_open_sem);
57                 /* Check to see if we lost the race */
58                 if (lli->lli_smd == NULL) {
59                         oa = obdo_alloc();
60                         if (!oa) {
61                                 up(&lli->lli_open_sem);
62                                 RETURN(-ENOMEM);
63                         }
64                         oa->o_mode = S_IFREG | 0600;
65                         oa->o_easize = ll_mds_easize(inode->i_sb);
66                         oa->o_id = inode->i_ino;
67                         oa->o_valid = OBD_MD_FLMODE | OBD_MD_FLEASIZE |
68                                         OBD_MD_FLID;
69                         rc = obd_create(ll_i2obdconn(inode), oa, &lli->lli_smd);
70
71                         if (rc) {
72                                 obdo_free(oa);
73                                 up(&lli->lli_open_sem);
74                                 RETURN(rc);
75                         }
76                 }
77                 if (lli->lli_smd->lsm_object_id == 0)
78                         LBUG();
79                 up(&lli->lli_open_sem);
80                 lsm = lli->lli_smd;
81         }
82
83         fd = kmem_cache_alloc(ll_file_data_slab, SLAB_KERNEL);
84         if (!fd)
85                 GOTO(out, rc = -ENOMEM);
86         memset(fd, 0, sizeof(*fd));
87
88         rc = mdc_open(&sbi->ll_mdc_conn, inode->i_ino, S_IFREG | inode->i_mode,
89                       file->f_flags, lsm, (__u64)(unsigned long)file,
90                       &fd->fd_mdshandle, &req);
91         fd->fd_req = req;
92         ptlrpc_req_finished(req);
93         if (rc)
94                 GOTO(out_req, -abs(rc));
95         if (!fd->fd_mdshandle) {
96                 CERROR("mdc_open didn't assign fd_mdshandle\n");
97                 /* XXX handle this how, abort or is it non-fatal? */
98         }
99
100         if (oa == NULL && (oa = obdo_alloc()) == NULL)
101                 GOTO(out_mdc, rc = -EINVAL);
102
103         oa->o_id = lsm->lsm_object_id;
104         oa->o_mode = S_IFREG | inode->i_mode;
105         oa->o_valid = OBD_MD_FLMODE | OBD_MD_FLID | OBD_MD_FLSIZE;
106         rc = obd_open(ll_i2obdconn(inode), oa, lsm);
107         obdo_free(oa);
108         oa = NULL;
109
110         if (rc)
111                 GOTO(out_mdc, rc = -abs(rc));
112
113         file->private_data = fd;
114
115         EXIT;
116
117         return 0;
118 out_mdc:
119         mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
120                   S_IFREG, fd->fd_mdshandle, &req);
121 out_req:
122         ptlrpc_free_req(req);
123 //out_fd:
124         obdo_free(oa);
125         kmem_cache_free(ll_file_data_slab, fd);
126         file->private_data = NULL;
127 out:
128         return rc;
129 }
130
131 int ll_size_lock(struct inode *inode, struct lov_stripe_md *lsm, __u64 start,
132                  int mode, struct lustre_handle **lockhs_p)
133 {
134         struct ll_sb_info *sbi = ll_i2sbi(inode);
135         struct ldlm_extent extent;
136         struct lustre_handle *lockhs = NULL;
137         int rc, flags = 0;
138
139         if (sbi->ll_flags & LL_SBI_NOLCK) {
140                 *lockhs_p = NULL;
141                 RETURN(0);
142         }
143
144         OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
145         if (lockhs == NULL)
146                 RETURN(-ENOMEM);
147
148         extent.start = start;
149         extent.end = ~0;
150
151         rc = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT, &extent,
152                          sizeof(extent), mode, &flags, ll_lock_callback,
153                          inode, sizeof(*inode), lockhs);
154         if (rc != ELDLM_OK) {
155                 CERROR("lock enqueue: %d\n", rc);
156                 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
157         } else
158                 *lockhs_p = lockhs;
159         RETURN(rc);
160 }
161
162 int ll_size_unlock(struct inode *inode, struct lov_stripe_md *lsm, int mode,
163                    struct lustre_handle *lockhs)
164 {
165         struct ll_sb_info *sbi = ll_i2sbi(inode);
166         int rc;
167
168         if (sbi->ll_flags & LL_SBI_NOLCK)
169                 RETURN(0);
170
171         if (lockhs == NULL) {
172                 LBUG();
173                 RETURN(-EINVAL);
174         }
175
176         rc = obd_cancel(&sbi->ll_osc_conn, lsm, mode, lockhs);
177         if (rc != ELDLM_OK) {
178                 CERROR("lock cancel: %d\n", rc);
179                 LBUG();
180         }
181
182         OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
183         RETURN(rc);
184 }
185
186 int ll_file_size(struct inode *inode, struct lov_stripe_md *lsm)
187 {
188         struct ll_sb_info *sbi = ll_i2sbi(inode);
189         struct lustre_handle *lockhs;
190         struct obdo oa;
191         int err, rc;
192
193         rc = ll_size_lock(inode, lsm, 0, LCK_PR, &lockhs);
194         if (rc != ELDLM_OK) {
195                 CERROR("lock enqueue: %d\n", rc);
196                 RETURN(rc);
197         }
198
199         oa.o_id = lsm->lsm_object_id;
200         oa.o_mode = S_IFREG;
201         oa.o_valid = OBD_MD_FLID|OBD_MD_FLMODE|OBD_MD_FLSIZE|OBD_MD_FLBLOCKS;
202         rc = obd_getattr(&sbi->ll_osc_conn, &oa, lsm);
203         if (!rc)
204                 obdo_to_inode(inode, &oa, oa.o_valid);
205
206         err = ll_size_unlock(inode, lsm, LCK_PR, lockhs);
207         if (err != ELDLM_OK) {
208                 CERROR("lock cancel: %d\n", err);
209                 LBUG();
210         }
211         RETURN(rc);
212 }
213
214 static int ll_file_release(struct inode *inode, struct file *file)
215 {
216         int rc;
217         struct ptlrpc_request *req = NULL;
218         struct ll_file_data *fd;
219         struct obdo oa;
220         struct ll_sb_info *sbi = ll_i2sbi(inode);
221         struct ll_inode_info *lli = ll_i2info(inode);
222
223         ENTRY;
224
225         fd = (struct ll_file_data *)file->private_data;
226         if (!fd || !fd->fd_mdshandle) {
227                 LBUG();
228                 GOTO(out, rc = -EINVAL);
229         }
230
231         memset(&oa, 0, sizeof(oa));
232         oa.o_id = lli->lli_smd->lsm_object_id;
233         oa.o_mode = S_IFREG;
234         oa.o_valid = (OBD_MD_FLMODE | OBD_MD_FLID);
235         rc = obd_close(ll_i2obdconn(inode), &oa, lli->lli_smd);
236         if (rc)
237                 GOTO(out_fd, abs(rc));
238
239         /* If this fails and we goto out_fd, the file size on the MDS is out of
240          * date.  Is that a big deal? */
241         if (file->f_mode & FMODE_WRITE) {
242                 struct lustre_handle *lockhs;
243
244                 rc = ll_size_lock(inode, lli->lli_smd, 0, LCK_PR, &lockhs);
245                 if (rc)
246                         GOTO(out_fd, abs(rc));
247
248                 oa.o_id = lli->lli_smd->lsm_object_id;
249                 oa.o_mode = S_IFREG;
250                 oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLSIZE |
251                         OBD_MD_FLBLOCKS;
252                 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lli->lli_smd);
253                 if (!rc) {
254                         struct iattr attr;
255                         attr.ia_valid = (ATTR_MTIME | ATTR_CTIME | ATTR_ATIME |
256                                          ATTR_SIZE);
257                         attr.ia_mtime = inode->i_mtime;
258                         attr.ia_ctime = inode->i_ctime;
259                         attr.ia_atime = inode->i_atime;
260                         attr.ia_size = oa.o_size;
261
262                         inode->i_blocks = oa.o_blocks;
263
264                         /* XXX: this introduces a small race that we should
265                          * evaluate */
266                         rc = ll_inode_setattr(inode, &attr, 0);
267                         if (rc) {
268                                 CERROR("failed - %d.\n", rc);
269                                 rc = -EIO; /* XXX - GOTO(out)? -phil */
270                         }
271                 }
272                 rc = ll_size_unlock(inode, lli->lli_smd, LCK_PR, lockhs);
273                 if (rc) {
274                         CERROR("lock cancel: %d\n", rc);
275                         LBUG();
276                 }
277         }
278
279         rc = mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
280                        S_IFREG, fd->fd_mdshandle, &req);
281         ptlrpc_req_finished(req);
282         if (rc) {
283                 if (rc > 0)
284                         rc = -rc;
285                 GOTO(out, rc);
286         }
287         ptlrpc_req_finished(fd->fd_req);
288
289         rc = obd_cancel_unused(ll_i2obdconn(inode), lli->lli_smd);
290         if (rc)
291                 CERROR("obd_cancel_unused: %d\n", rc);
292
293         EXIT;
294
295 out_fd:
296         kmem_cache_free(ll_file_data_slab, fd);
297         file->private_data = NULL;
298 out:
299         return rc;
300 }
301
302
303 static inline void ll_remove_suid(struct inode *inode)
304 {
305         unsigned int mode;
306
307         /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
308         mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
309
310         /* was any of the uid bits set? */
311         mode &= inode->i_mode;
312         if (mode && !capable(CAP_FSETID)) {
313                 inode->i_mode &= ~mode;
314                 // XXX careful here - we cannot change the size
315         }
316 }
317
318 static void ll_update_atime(struct inode *inode)
319 {
320         struct iattr attr;
321
322         attr.ia_atime = CURRENT_TIME;
323         attr.ia_valid = ATTR_ATIME;
324
325         if (inode->i_atime == attr.ia_atime) return;
326         if (IS_RDONLY(inode)) return;
327         if (IS_NOATIME(inode)) return;
328
329         /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
330         ll_inode_setattr(inode, &attr, 0);
331 }
332
333 int ll_lock_callback(struct ldlm_lock *lock, struct ldlm_lock_desc *new,
334                      void *data, __u32 data_len)
335 {
336         struct inode *inode = data;
337         struct lustre_handle lockh;
338         int rc;
339         ENTRY;
340
341         if (data_len != sizeof(struct inode))
342                 LBUG();
343
344         if (inode == NULL)
345                 LBUG();
346         down(&inode->i_sem);
347         CDEBUG(D_INODE, "invalidating obdo/inode %ld\n", inode->i_ino);
348         /* FIXME: do something better than throwing away everything */
349         invalidate_inode_pages(inode);
350         up(&inode->i_sem);
351
352         ldlm_lock2handle(lock, &lockh);
353         rc = ldlm_cli_cancel(&lockh);
354         if (rc != ELDLM_OK)
355                 CERROR("ldlm_cli_cancel failed: %d\n", rc);
356         RETURN(0);
357 }
358
359 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
360                             loff_t *ppos)
361 {
362         struct ll_file_data *fd = (struct ll_file_data *)filp->private_data;
363         struct inode *inode = filp->f_dentry->d_inode;
364         struct ll_sb_info *sbi = ll_i2sbi(inode);
365         struct ldlm_extent extent;
366         struct lustre_handle *lockhs = NULL;
367         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
368         int flags = 0;
369         ldlm_error_t err;
370         ssize_t retval;
371         ENTRY;
372
373         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
374             sbi->ll_flags & LL_SBI_NOLCK) {
375                 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
376                 if (!lockhs)
377                         RETURN(-ENOMEM);
378
379                 extent.start = *ppos;
380                 extent.end = *ppos + count;
381                 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
382                        inode->i_ino, extent.start, extent.end);
383
384                 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
385                                   &extent, sizeof(extent), LCK_PR, &flags,
386                                   ll_lock_callback, inode, sizeof(*inode),
387                                   lockhs);
388                 if (err != ELDLM_OK) {
389                         OBD_FREE(lockhs, lsm->lsm_stripe_count*sizeof(*lockhs));
390                         CERROR("lock enqueue: err: %d\n", err);
391                         RETURN(err);
392                 }
393         }
394
395         CDEBUG(D_INFO, "Reading inode %ld, %d bytes, offset %Ld\n",
396                inode->i_ino, count, *ppos);
397         retval = generic_file_read(filp, buf, count, ppos);
398
399         if (retval > 0)
400                 ll_update_atime(inode);
401
402         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
403             sbi->ll_flags & LL_SBI_NOLCK) {
404                 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PR, lockhs);
405                 if (err != ELDLM_OK) {
406                         CERROR("lock cancel: err: %d\n", err);
407                         retval = err;
408                 }
409         }
410
411         if (lockhs)
412                 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
413         RETURN(retval);
414 }
415
416 /*
417  * Write to a file (through the page cache).
418  */
419 static ssize_t
420 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
421 {
422         struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
423         struct inode *inode = file->f_dentry->d_inode;
424         struct ll_sb_info *sbi = ll_i2sbi(inode);
425         struct ldlm_extent extent;
426         struct lustre_handle *lockhs = NULL, *eof_lockhs = NULL;
427         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
428         int flags = 0;
429         ldlm_error_t err;
430         ssize_t retval;
431         ENTRY;
432
433         if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
434                 struct obdo oa;
435                 err = ll_size_lock(inode, lsm, 0, LCK_PW, &eof_lockhs);
436                 if (err)
437                         RETURN(err);
438
439                 oa.o_id = lsm->lsm_object_id;
440                 oa.o_mode = inode->i_mode;
441                 oa.o_valid = OBD_MD_FLID | OBD_MD_FLMODE | OBD_MD_FLSIZE |
442                         OBD_MD_FLBLOCKS;
443                 retval = obd_getattr(&sbi->ll_osc_conn, &oa, lsm);
444                 if (retval)
445                         GOTO(out_eof, retval);
446
447                 *ppos = oa.o_size;
448                 obdo_to_inode(inode, &oa, oa.o_valid);
449         }
450
451         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
452             sbi->ll_flags & LL_SBI_NOLCK) {
453                 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
454                 if (!lockhs)
455                         GOTO(out_eof, retval = -ENOMEM);
456                 extent.start = *ppos;
457                 extent.end = *ppos + count;
458                 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
459                        inode->i_ino, extent.start, extent.end);
460
461                 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
462                                   &extent, sizeof(extent), LCK_PW, &flags,
463                                   ll_lock_callback, inode, sizeof(*inode),
464                                   lockhs);
465                 if (err != ELDLM_OK) {
466                         CERROR("lock enqueue: err: %d\n", err);
467                         GOTO(out_free, retval = err);
468                 }
469         }
470
471         CDEBUG(D_INFO, "Writing inode %ld, %ld bytes, offset "LPD64"\n",
472                inode->i_ino, (long)count, *ppos);
473
474         retval = generic_file_write(file, buf, count, ppos);
475
476         if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
477             sbi->ll_flags & LL_SBI_NOLCK) {
478                 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PW, lockhs);
479                 if (err != ELDLM_OK) {
480                         CERROR("lock cancel: err: %d\n", err);
481                         GOTO(out_free, retval = err);
482                 }
483         }
484
485         EXIT;
486  out_free:
487         if (lockhs)
488                 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
489
490  out_eof:
491         if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
492                 err = ll_size_unlock(inode, lsm, LCK_PW, eof_lockhs);
493                 if (err && !retval)
494                         retval = err;
495         }
496
497         return retval;
498 }
499
500 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
501                   unsigned long arg)
502 {
503         struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
504         int flags;
505
506         switch(cmd) {
507         case LL_IOC_GETFLAGS:
508                 /* Get the current value of the file flags */
509                 return put_user(fd->fd_flags, (int *)arg);
510         case LL_IOC_SETFLAGS:
511         case LL_IOC_CLRFLAGS:
512                 /* Set or clear specific file flags */
513                 /* XXX This probably needs checks to ensure the flags are
514                  *     not abused, and to handle any flag side effects.
515                  */
516                 if (get_user(flags, (int *) arg))
517                         return -EFAULT;
518
519                 if (cmd == LL_IOC_SETFLAGS)
520                         fd->fd_flags |= flags;
521                 else
522                         fd->fd_flags &= ~flags;
523                 return 0;
524
525         /* We need to special case any other ioctls we want to handle,
526          * to send them to the MDS/OST as appropriate and to properly
527          * network encode the arg field.
528         case EXT2_IOC_GETFLAGS:
529         case EXT2_IOC_SETFLAGS:
530         case EXT2_IOC_GETVERSION_OLD:
531         case EXT2_IOC_GETVERSION_NEW:
532         case EXT2_IOC_SETVERSION_OLD:
533         case EXT2_IOC_SETVERSION_NEW:
534         */
535         default:
536                 return -ENOTTY;
537         }
538 }
539
540 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
541 {
542         struct inode *inode = file->f_dentry->d_inode;
543         long long retval;
544         ENTRY;
545
546         switch (origin) {
547         case 2: {
548                 struct ll_inode_info *lli = ll_i2info(inode);
549
550                 retval = ll_file_size(inode, lli->lli_smd);
551                 if (retval)
552                         RETURN(retval);
553
554                 offset += inode->i_size;
555                 break;
556         }
557         case 1:
558                 offset += file->f_pos;
559         }
560         retval = -EINVAL;
561         if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
562                 if (offset != file->f_pos) {
563                         file->f_pos = offset;
564                         file->f_reada = 0;
565                         file->f_version = ++event;
566                 }
567                 retval = offset;
568         }
569         RETURN(retval);
570 }
571
572 /* XXX this does not need to do anything for data, it _does_ need to
573    call setattr */
574 int ll_fsync(struct file *file, struct dentry *dentry, int data)
575 {
576         return 0;
577 }
578
579 struct file_operations ll_file_operations = {
580         read:           ll_file_read,
581         write:          ll_file_write,
582         ioctl:          ll_file_ioctl,
583         open:           ll_file_open,
584         release:        ll_file_release,
585         mmap:           generic_file_mmap,
586         llseek:         ll_file_seek,
587         fsync:          NULL
588 };
589
590 struct inode_operations ll_file_inode_operations = {
591         truncate: ll_truncate,
592         setattr: ll_setattr
593 };