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