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