1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * This code is issued under the GNU General Public License.
7 * See the file COPYING in this distribution
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)
16 * linux/fs/minix/file.c
18 * Copyright (C) 1991, 1992 Linus Torvalds
20 * ext2 fs regular file handling primitives
22 * 64-bit file support on 64-bit platforms by Jakub Jelinek
23 * (jj@sunsite.ms.mff.cuni.cz)
26 #define DEBUG_SUBSYSTEM S_LLITE
28 #include <linux/lustre_dlm.h>
29 #include <linux/lustre_lite.h>
30 #include <linux/random.h>
32 int ll_inode_setattr(struct inode *inode, struct iattr *attr, int do_trunc);
33 extern int ll_setattr(struct dentry *de, struct iattr *attr);
35 int ll_create_objects(struct super_block *sb, obd_id id, uid_t uid, gid_t gid,
36 struct lov_stripe_md **lsmp)
46 oa->o_mode = S_IFREG | 0600;
47 oa->o_easize = ll_mds_easize(sb);
51 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE |
52 OBD_MD_FLEASIZE | OBD_MD_FLUID | OBD_MD_FLGID;
53 rc = obd_create(ll_s2obdconn(sb), oa, lsmp);
57 LASSERT(*lsmp && (*lsmp)->lsm_object_id);
61 static int ll_file_open(struct inode *inode, struct file *file)
63 struct ptlrpc_request *req = NULL;
64 struct ll_file_data *fd;
66 struct lov_stripe_md *lsm = NULL;
67 struct ll_sb_info *sbi = ll_i2sbi(inode);
68 struct ll_inode_info *lli = ll_i2info(inode);
72 LASSERT(!file->private_data);
76 /* delayed create of object (intent created inode) */
77 /* XXX object needs to be cleaned up if mdc_open fails */
78 /* XXX error handling appropriate here? */
80 if (file->f_flags & O_LOV_DELAY_CREATE) {
81 CDEBUG(D_INODE, "delaying object creation\n");
84 down(&lli->lli_open_sem);
85 /* Check to see if we lost the race */
87 rc = ll_create_objects(inode->i_sb, inode->i_ino, 0, 0,
89 up(&lli->lli_open_sem);
96 fd = kmem_cache_alloc(ll_file_data_slab, SLAB_KERNEL);
98 GOTO(out, rc = -ENOMEM);
99 memset(fd, 0, sizeof(*fd));
101 fd->fd_mdshandle.addr = (__u64)(unsigned long)file;
102 get_random_bytes(&fd->fd_mdshandle.cookie,
103 sizeof(fd->fd_mdshandle.cookie));
104 rc = mdc_open(&sbi->ll_mdc_conn, inode->i_ino, S_IFREG | inode->i_mode,
105 file->f_flags, lsm, &fd->fd_mdshandle, &req);
108 /* This is the "reply" refcount. */
109 ptlrpc_req_finished(req);
111 GOTO(out_req, -abs(rc));
112 if (!fd->fd_mdshandle.addr ||
113 fd->fd_mdshandle.addr == (__u64)(unsigned long)file) {
114 CERROR("hmm, mdc_open didn't assign fd_mdshandle?\n");
115 /* XXX handle this how, abort or is it non-fatal? */
120 GOTO(out_mdc, rc = -EINVAL);
122 oa->o_id = lsm->lsm_object_id;
123 oa->o_mode = S_IFREG;
124 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
126 rc = obd_open(ll_i2obdconn(inode), oa, lsm);
127 obdo_to_inode(inode, oa, oa->o_valid & (OBD_MD_FLSIZE|OBD_MD_FLBLOCKS));
129 obd_oa2handle(&fd->fd_osthandle, oa);
133 GOTO(out_mdc, rc = -abs(rc));
135 atomic_inc(&lli->lli_open_count);
137 file->private_data = fd;
141 mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
142 S_IFREG, &fd->fd_mdshandle, &req);
144 ptlrpc_free_req(req);
146 fd->fd_mdshandle.cookie = DEAD_HANDLE_MAGIC;
147 kmem_cache_free(ll_file_data_slab, fd);
152 int ll_size_lock(struct inode *inode, struct lov_stripe_md *lsm, obd_off start,
153 int mode, struct lustre_handle **lockhs_p)
155 struct ll_sb_info *sbi = ll_i2sbi(inode);
156 struct ldlm_extent extent;
157 struct lustre_handle *lockhs = NULL;
158 int rc, flags = 0, stripe_count;
161 if (sbi->ll_flags & LL_SBI_NOLCK) {
166 stripe_count = lsm->lsm_stripe_count;
170 OBD_ALLOC(lockhs, stripe_count * sizeof(*lockhs));
174 extent.start = start;
175 extent.end = OBD_OBJECT_EOF;
177 rc = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT, &extent,
178 sizeof(extent), mode, &flags, ll_lock_callback,
179 inode, sizeof(*inode), lockhs);
180 if (rc != ELDLM_OK) {
181 CERROR("lock enqueue: %d\n", rc);
182 OBD_FREE(lockhs, stripe_count * sizeof(*lockhs));
188 int ll_size_unlock(struct inode *inode, struct lov_stripe_md *lsm, int mode,
189 struct lustre_handle *lockhs)
191 struct ll_sb_info *sbi = ll_i2sbi(inode);
192 int rc, stripe_count;
195 if (sbi->ll_flags & LL_SBI_NOLCK)
198 if (lockhs == NULL) {
203 rc = obd_cancel(&sbi->ll_osc_conn, lsm, mode, lockhs);
204 if (rc != ELDLM_OK) {
205 CERROR("lock cancel: %d\n", rc);
209 stripe_count = lsm->lsm_stripe_count;
213 OBD_FREE(lockhs, stripe_count * sizeof(*lockhs));
217 int ll_file_size(struct inode *inode, struct lov_stripe_md *lsm)
219 struct ll_sb_info *sbi = ll_i2sbi(inode);
220 struct lustre_handle *lockhs;
228 rc = ll_size_lock(inode, lsm, 0, LCK_PR, &lockhs);
229 if (rc != ELDLM_OK) {
230 CERROR("lock enqueue: %d\n", rc);
234 oa.o_id = lsm->lsm_object_id;
236 oa.o_valid = OBD_MD_FLID|OBD_MD_FLTYPE|OBD_MD_FLSIZE|OBD_MD_FLBLOCKS;
237 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lsm);
239 obdo_to_inode(inode, &oa,
240 oa.o_valid & ~(OBD_MD_FLTYPE | OBD_MD_FLMODE));
242 err = ll_size_unlock(inode, lsm, LCK_PR, lockhs);
243 if (err != ELDLM_OK) {
244 CERROR("lock cancel: %d\n", err);
250 static int ll_file_release(struct inode *inode, struct file *file)
252 struct ptlrpc_request *req = NULL;
253 struct ll_file_data *fd;
255 struct ll_sb_info *sbi = ll_i2sbi(inode);
256 struct ll_inode_info *lli = ll_i2info(inode);
257 struct lov_stripe_md *lsm = lli->lli_smd;
262 fd = (struct ll_file_data *)file->private_data;
265 GOTO(out, rc = -EINVAL);
268 memset(&oa, 0, sizeof(oa));
269 oa.o_id = lsm->lsm_object_id;
271 oa.o_valid = OBD_MD_FLTYPE | OBD_MD_FLID;
272 obd_handle2oa(&oa, &fd->fd_osthandle);
273 rc = obd_close(ll_i2obdconn(inode), &oa, lsm);
275 GOTO(out_mdc, rc = -abs(rc));
278 #error "This should only be done on the node that already has the EOF lock"
279 #error "and only in the case where the file size actually changed. For now"
280 #error "we don't care about the size on the MDS, since we never use it (the"
281 #error "OST always has the authoritative size and we don't even use the MDS."
282 /* If this fails and we goto out_fd, the file size on the MDS is out of
283 * date. Is that a big deal? */
284 if (file->f_mode & FMODE_WRITE) {
285 struct lustre_handle *lockhs;
287 rc = ll_size_lock(inode, lsm, 0, LCK_PR, &lockhs);
289 GOTO(out_mdc, -abs(rc));
291 oa.o_id = lsm->lsm_object_id;
293 oa.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
295 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lsm);
298 attr.ia_valid = (ATTR_MTIME | ATTR_CTIME | ATTR_ATIME |
300 attr.ia_mtime = inode->i_mtime;
301 attr.ia_ctime = inode->i_ctime;
302 attr.ia_atime = inode->i_atime;
303 attr.ia_size = oa.o_size;
305 inode->i_blocks = oa.o_blocks;
307 /* XXX: this introduces a small race that we should
309 rc = ll_inode_setattr(inode, &attr, 0);
311 rc2 = ll_size_unlock(inode, lli->lli_smd, LCK_PR, lockhs);
313 CERROR("lock cancel: %d\n", rc);
322 rc2 = mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
323 S_IFREG, &fd->fd_mdshandle, &req);
324 ptlrpc_req_finished(req);
330 CDEBUG(D_HA, "matched req %p xid "LPD64" transno "LPD64" op "
331 "%d->%s:%d\n", fd->fd_req, fd->fd_req->rq_xid,
332 fd->fd_req->rq_repmsg->transno, fd->fd_req->rq_reqmsg->opc,
333 fd->fd_req->rq_import->imp_connection->c_remote_uuid,
334 fd->fd_req->rq_import->imp_client->cli_request_portal);
335 ptlrpc_req_finished(fd->fd_req);
337 if (atomic_dec_and_test(&lli->lli_open_count)) {
338 CDEBUG(D_INFO, "last close, cancelling unused locks\n");
339 rc = obd_cancel_unused(ll_i2obdconn(inode), lsm, 0);
341 CERROR("obd_cancel_unused: %d\n", rc);
343 CDEBUG(D_INFO, "not last close, not cancelling unused locks\n");
349 fd->fd_mdshandle.cookie = DEAD_HANDLE_MAGIC;
350 file->private_data = NULL;
351 kmem_cache_free(ll_file_data_slab, fd);
356 static inline void ll_remove_suid(struct inode *inode)
360 /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
361 mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
363 /* was any of the uid bits set? */
364 mode &= inode->i_mode;
365 if (mode && !capable(CAP_FSETID)) {
366 inode->i_mode &= ~mode;
367 // XXX careful here - we cannot change the size
371 static void ll_update_atime(struct inode *inode)
375 attr.ia_atime = CURRENT_TIME;
376 attr.ia_valid = ATTR_ATIME;
378 if (inode->i_atime == attr.ia_atime) return;
379 if (IS_RDONLY(inode)) return;
380 if (IS_NOATIME(inode)) return;
382 /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
383 ll_inode_setattr(inode, &attr, 0);
386 int ll_lock_callback(struct ldlm_lock *lock, struct ldlm_lock_desc *new,
387 void *data, __u32 data_len, int flag)
389 struct inode *inode = data;
390 struct lustre_handle lockh;
394 if (data_len != sizeof(struct inode))
401 case LDLM_CB_BLOCKING:
402 ldlm_lock2handle(lock, &lockh);
403 rc = ldlm_cli_cancel(&lockh);
405 CERROR("ldlm_cli_cancel failed: %d\n", rc);
407 case LDLM_CB_CANCELING:
408 CDEBUG(D_INODE, "invalidating obdo/inode %ld\n", inode->i_ino);
409 /* FIXME: do something better than throwing away everything */
410 //down(&inode->i_sem);
411 ll_invalidate_inode_pages(inode);
421 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
424 struct ll_file_data *fd = (struct ll_file_data *)filp->private_data;
425 struct inode *inode = filp->f_dentry->d_inode;
426 struct ll_sb_info *sbi = ll_i2sbi(inode);
427 struct lustre_handle *lockhs = NULL;
428 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
434 /* If we don't refresh the file size, generic_file_read may not even
436 retval = ll_file_size(inode, lsm);
438 CERROR("ll_file_size: %d\n", retval);
442 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
443 !(sbi->ll_flags & LL_SBI_NOLCK)) {
444 struct ldlm_extent extent;
445 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
449 extent.start = *ppos;
450 extent.end = *ppos + count;
451 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
452 inode->i_ino, extent.start, extent.end);
454 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
455 &extent, sizeof(extent), LCK_PR, &flags,
456 ll_lock_callback, inode, sizeof(*inode),
458 if (err != ELDLM_OK) {
459 OBD_FREE(lockhs, lsm->lsm_stripe_count*sizeof(*lockhs));
460 CERROR("lock enqueue: err: %d\n", err);
465 CDEBUG(D_INFO, "Reading inode %ld, %d bytes, offset %Ld\n",
466 inode->i_ino, count, *ppos);
467 retval = generic_file_read(filp, buf, count, ppos);
470 ll_update_atime(inode);
472 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
473 !(sbi->ll_flags & LL_SBI_NOLCK)) {
474 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PR, lockhs);
475 if (err != ELDLM_OK) {
476 CERROR("lock cancel: err: %d\n", err);
482 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
487 * Write to a file (through the page cache).
490 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
492 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
493 struct inode *inode = file->f_dentry->d_inode;
494 struct ll_sb_info *sbi = ll_i2sbi(inode);
495 struct lustre_handle *lockhs = NULL, *eof_lockhs = NULL;
496 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
502 if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
509 err = ll_size_lock(inode, lsm, 0, LCK_PW, &eof_lockhs);
515 oa->o_id = lsm->lsm_object_id;
516 oa->o_mode = inode->i_mode;
517 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
519 obd_handle2oa(oa, &fd->fd_osthandle);
520 retval = obd_getattr(&sbi->ll_osc_conn, oa, lsm);
523 GOTO(out_eof, retval);
527 obdo_to_inode(inode, oa, oa->o_valid);
531 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
532 !(sbi->ll_flags & LL_SBI_NOLCK)) {
533 struct ldlm_extent extent;
534 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
536 GOTO(out_eof, retval = -ENOMEM);
537 extent.start = *ppos;
538 extent.end = *ppos + count;
539 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
540 inode->i_ino, extent.start, extent.end);
542 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
543 &extent, sizeof(extent), LCK_PW, &flags,
544 ll_lock_callback, inode, sizeof(*inode),
546 if (err != ELDLM_OK) {
547 CERROR("lock enqueue: err: %d\n", err);
548 GOTO(out_free, retval = err);
552 CDEBUG(D_INFO, "Writing inode %ld, %ld bytes, offset "LPD64"\n",
553 inode->i_ino, (long)count, *ppos);
555 retval = generic_file_write(file, buf, count, ppos);
557 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
558 sbi->ll_flags & LL_SBI_NOLCK) {
559 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PW, lockhs);
560 if (err != ELDLM_OK) {
561 CERROR("lock cancel: err: %d\n", err);
562 GOTO(out_free, retval = err);
569 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
572 if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
573 err = ll_size_unlock(inode, lsm, LCK_PW, eof_lockhs);
581 static int ll_lov_setstripe(struct inode *inode, struct file *file,
582 struct lov_user_md *lum)
584 struct ll_inode_info *lli = ll_i2info(inode);
585 struct lov_stripe_md *lsm;
586 int size = ll_mds_easize(inode->i_sb);
589 rc = verify_area(VERIFY_READ, lum, sizeof(*lum));
593 down(&lli->lli_open_sem);
595 CERROR("striping data already set for %d\n", inode->i_ino);
596 GOTO(out_lov_up, rc = -EPERM);
599 OBD_ALLOC(lli->lli_smd, size);
601 GOTO(out_lov_up, rc = -ENOMEM);
604 lsm->lsm_magic = LOV_MAGIC;
605 lsm->lsm_stripe_size = lum->lum_stripe_size;
606 lsm->lsm_stripe_pattern = lum->lum_stripe_pattern;
607 lsm->lsm_stripe_offset = lum->lum_stripe_offset;
608 lsm->lsm_stripe_count = lum->lum_stripe_count;
609 lsm->lsm_mds_easize = size;
611 file->f_flags &= ~O_LOV_DELAY_CREATE;
612 rc = ll_create_objects(inode->i_sb, inode->i_ino, 0, 0, &lsm);
614 OBD_FREE(lli->lli_smd, size);
616 rc = ll_file_open(inode, file);
618 up(&lli->lli_open_sem);
622 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
624 struct lov_user_md lum;
625 struct lov_user_md *lump;
626 struct ll_inode_info *lli = ll_i2info(inode);
627 struct lov_stripe_md *lsm = lli->lli_smd;
628 struct lov_user_oinfo *luoip;
629 struct lov_oinfo *loip;
630 int count, len, i, rc;
632 rc = copy_from_user(&lum, (void *)arg, sizeof(lum));
636 if ((count = lsm->lsm_stripe_count) == 0)
639 if (lum.lum_stripe_count < count)
642 len = sizeof(*lump) + count * sizeof(*luoip);
644 rc = verify_area(VERIFY_WRITE, (void *)arg, len);
648 lump = (struct lov_user_md *)arg;
649 lump->lum_stripe_count = count;
650 luoip = lump->lum_luoinfo;
652 if (lsm->lsm_stripe_count == 0) {
653 lump->lum_stripe_size = 0;
654 lump->lum_stripe_pattern = 0;
655 lump->lum_stripe_offset = 0;
657 luoip->luo_id = lsm->lsm_object_id;
659 lump->lum_stripe_size = lsm->lsm_stripe_size;
660 lump->lum_stripe_pattern = lsm->lsm_stripe_pattern;
661 lump->lum_stripe_offset = lsm->lsm_stripe_offset;
663 loip = lsm->lsm_oinfo;
664 for (i = 0; i < count; i++, luoip++, loip++) {
665 luoip->luo_idx = loip->loi_ost_idx;
666 luoip->luo_id = loip->loi_id;
673 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
676 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
677 struct lustre_handle *conn;
681 case LL_IOC_GETFLAGS:
682 /* Get the current value of the file flags */
683 return put_user(fd->fd_flags, (int *)arg);
684 case LL_IOC_SETFLAGS:
685 case LL_IOC_CLRFLAGS:
686 /* Set or clear specific file flags */
687 /* XXX This probably needs checks to ensure the flags are
688 * not abused, and to handle any flag side effects.
690 if (get_user(flags, (int *) arg))
693 if (cmd == LL_IOC_SETFLAGS)
694 fd->fd_flags |= flags;
696 fd->fd_flags &= ~flags;
698 case LL_IOC_LOV_SETSTRIPE:
699 return ll_lov_setstripe(inode, file, (struct lov_user_md *)arg);
700 case LL_IOC_LOV_GETSTRIPE:
701 return ll_lov_getstripe(inode, arg);
703 /* We need to special case any other ioctls we want to handle,
704 * to send them to the MDS/OST as appropriate and to properly
705 * network encode the arg field.
706 case EXT2_IOC_GETFLAGS:
707 case EXT2_IOC_SETFLAGS:
708 case EXT2_IOC_GETVERSION_OLD:
709 case EXT2_IOC_GETVERSION_NEW:
710 case EXT2_IOC_SETVERSION_OLD:
711 case EXT2_IOC_SETVERSION_NEW:
714 conn = ll_i2obdconn(inode);
715 return obd_iocontrol(cmd, conn, 0, NULL, (void *)arg);
719 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
721 struct inode *inode = file->f_dentry->d_inode;
727 struct ll_inode_info *lli = ll_i2info(inode);
729 retval = ll_file_size(inode, lli->lli_smd);
733 offset += inode->i_size;
737 offset += file->f_pos;
740 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
741 if (offset != file->f_pos) {
742 file->f_pos = offset;
743 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
746 file->f_version = ++event;
753 /* XXX this does not need to do anything for data, it _does_ need to
755 int ll_fsync(struct file *file, struct dentry *dentry, int data)
762 static int ll_inode_revalidate(struct dentry *dentry)
764 struct inode *inode = dentry->d_inode;
765 struct lov_stripe_md *lsm;
771 lsm = ll_i2info(inode)->lli_smd;
772 if (!lsm) /* object not yet allocated, don't validate size */
775 RETURN(ll_file_size(inode, lsm));
778 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
779 static int ll_getattr(struct vfsmount *mnt, struct dentry *de,
782 return ll_inode_revalidate(de);
786 struct file_operations ll_file_operations = {
788 write: ll_file_write,
789 ioctl: ll_file_ioctl,
791 release: ll_file_release,
792 mmap: generic_file_mmap,
793 llseek: ll_file_seek,
797 struct inode_operations ll_file_inode_operations = {
799 truncate: ll_truncate,
800 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
803 revalidate: ll_inode_revalidate,