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 file->private_data = fd;
139 mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
140 S_IFREG, &fd->fd_mdshandle, &req);
142 ptlrpc_free_req(req);
144 fd->fd_mdshandle.cookie = DEAD_HANDLE_MAGIC;
145 kmem_cache_free(ll_file_data_slab, fd);
150 int ll_size_lock(struct inode *inode, struct lov_stripe_md *lsm, obd_off start,
151 int mode, struct lustre_handle **lockhs_p)
153 struct ll_sb_info *sbi = ll_i2sbi(inode);
154 struct ldlm_extent extent;
155 struct lustre_handle *lockhs = NULL;
156 int rc, flags = 0, stripe_count;
159 if (sbi->ll_flags & LL_SBI_NOLCK) {
164 stripe_count = lsm->lsm_stripe_count;
168 OBD_ALLOC(lockhs, stripe_count * sizeof(*lockhs));
172 extent.start = start;
173 extent.end = OBD_OBJECT_EOF;
175 rc = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT, &extent,
176 sizeof(extent), mode, &flags, ll_lock_callback,
177 inode, sizeof(*inode), lockhs);
178 if (rc != ELDLM_OK) {
179 CERROR("lock enqueue: %d\n", rc);
180 OBD_FREE(lockhs, stripe_count * sizeof(*lockhs));
186 int ll_size_unlock(struct inode *inode, struct lov_stripe_md *lsm, int mode,
187 struct lustre_handle *lockhs)
189 struct ll_sb_info *sbi = ll_i2sbi(inode);
190 int rc, stripe_count;
193 if (sbi->ll_flags & LL_SBI_NOLCK)
196 if (lockhs == NULL) {
201 rc = obd_cancel(&sbi->ll_osc_conn, lsm, mode, lockhs);
202 if (rc != ELDLM_OK) {
203 CERROR("lock cancel: %d\n", rc);
207 stripe_count = lsm->lsm_stripe_count;
211 OBD_FREE(lockhs, stripe_count * sizeof(*lockhs));
215 int ll_file_size(struct inode *inode, struct lov_stripe_md *lsm)
217 struct ll_sb_info *sbi = ll_i2sbi(inode);
218 struct lustre_handle *lockhs;
226 rc = ll_size_lock(inode, lsm, 0, LCK_PR, &lockhs);
227 if (rc != ELDLM_OK) {
228 CERROR("lock enqueue: %d\n", rc);
232 oa.o_id = lsm->lsm_object_id;
234 oa.o_valid = OBD_MD_FLID|OBD_MD_FLTYPE|OBD_MD_FLSIZE|OBD_MD_FLBLOCKS;
235 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lsm);
237 obdo_to_inode(inode, &oa,
238 oa.o_valid & ~(OBD_MD_FLTYPE | OBD_MD_FLMODE));
240 err = ll_size_unlock(inode, lsm, LCK_PR, lockhs);
241 if (err != ELDLM_OK) {
242 CERROR("lock cancel: %d\n", err);
248 static int ll_file_release(struct inode *inode, struct file *file)
250 struct ptlrpc_request *req = NULL;
251 struct ll_file_data *fd;
253 struct ll_sb_info *sbi = ll_i2sbi(inode);
254 struct ll_inode_info *lli = ll_i2info(inode);
255 struct lov_stripe_md *lsm = lli->lli_smd;
260 fd = (struct ll_file_data *)file->private_data;
263 GOTO(out, rc = -EINVAL);
266 memset(&oa, 0, sizeof(oa));
267 oa.o_id = lsm->lsm_object_id;
269 oa.o_valid = OBD_MD_FLTYPE | OBD_MD_FLID;
270 obd_handle2oa(&oa, &fd->fd_osthandle);
271 rc = obd_close(ll_i2obdconn(inode), &oa, lsm);
273 GOTO(out_mdc, rc = -abs(rc));
275 /* If this fails and we goto out_fd, the file size on the MDS is out of
276 * date. Is that a big deal? */
277 #warning "FIXME: don't do this if the file is unlinked already"
278 if (file->f_mode & FMODE_WRITE) {
279 struct lustre_handle *lockhs;
281 rc = ll_size_lock(inode, lsm, 0, LCK_PR, &lockhs);
283 GOTO(out_mdc, -abs(rc));
285 oa.o_id = lsm->lsm_object_id;
287 oa.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
289 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lsm);
292 attr.ia_valid = (ATTR_MTIME | ATTR_CTIME | ATTR_ATIME |
294 attr.ia_mtime = inode->i_mtime;
295 attr.ia_ctime = inode->i_ctime;
296 attr.ia_atime = inode->i_atime;
297 attr.ia_size = oa.o_size;
299 inode->i_blocks = oa.o_blocks;
301 /* XXX: this introduces a small race that we should
303 rc = ll_inode_setattr(inode, &attr, 0);
305 rc2 = ll_size_unlock(inode, lli->lli_smd, LCK_PR, lockhs);
307 CERROR("lock cancel: %d\n", rc);
315 rc2 = mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
316 S_IFREG, &fd->fd_mdshandle, &req);
317 ptlrpc_req_finished(req);
323 CDEBUG(D_HA, "matched req %p xid "LPD64" transno "LPD64" op %d->%s:%d\n",
324 fd->fd_req, fd->fd_req->rq_xid, fd->fd_req->rq_repmsg->transno,
325 fd->fd_req->rq_reqmsg->opc,
326 fd->fd_req->rq_import->imp_connection->c_remote_uuid,
327 fd->fd_req->rq_import->imp_client->cli_request_portal);
328 ptlrpc_req_finished(fd->fd_req);
330 rc = obd_cancel_unused(ll_i2obdconn(inode), lsm, 0);
332 CERROR("obd_cancel_unused: %d\n", rc);
337 fd->fd_mdshandle.cookie = DEAD_HANDLE_MAGIC;
338 file->private_data = NULL;
339 kmem_cache_free(ll_file_data_slab, fd);
345 static inline void ll_remove_suid(struct inode *inode)
349 /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
350 mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
352 /* was any of the uid bits set? */
353 mode &= inode->i_mode;
354 if (mode && !capable(CAP_FSETID)) {
355 inode->i_mode &= ~mode;
356 // XXX careful here - we cannot change the size
360 static void ll_update_atime(struct inode *inode)
364 attr.ia_atime = CURRENT_TIME;
365 attr.ia_valid = ATTR_ATIME;
367 if (inode->i_atime == attr.ia_atime) return;
368 if (IS_RDONLY(inode)) return;
369 if (IS_NOATIME(inode)) return;
371 /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
372 ll_inode_setattr(inode, &attr, 0);
375 int ll_lock_callback(struct ldlm_lock *lock, struct ldlm_lock_desc *new,
376 void *data, __u32 data_len, int flag)
378 struct inode *inode = data;
379 struct lustre_handle lockh;
383 if (data_len != sizeof(struct inode))
390 case LDLM_CB_BLOCKING:
391 ldlm_lock2handle(lock, &lockh);
392 rc = ldlm_cli_cancel(&lockh);
394 CERROR("ldlm_cli_cancel failed: %d\n", rc);
396 case LDLM_CB_CANCELING:
397 CDEBUG(D_INODE, "invalidating obdo/inode %ld\n", inode->i_ino);
398 /* FIXME: do something better than throwing away everything */
399 //down(&inode->i_sem);
400 ll_invalidate_inode_pages(inode);
410 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
413 struct ll_file_data *fd = (struct ll_file_data *)filp->private_data;
414 struct inode *inode = filp->f_dentry->d_inode;
415 struct ll_sb_info *sbi = ll_i2sbi(inode);
416 struct lustre_handle *lockhs = NULL;
417 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
423 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
424 !(sbi->ll_flags & LL_SBI_NOLCK)) {
425 struct ldlm_extent extent;
426 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
430 extent.start = *ppos;
431 extent.end = *ppos + count;
432 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
433 inode->i_ino, extent.start, extent.end);
435 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
436 &extent, sizeof(extent), LCK_PR, &flags,
437 ll_lock_callback, inode, sizeof(*inode),
439 if (err != ELDLM_OK) {
440 OBD_FREE(lockhs, lsm->lsm_stripe_count*sizeof(*lockhs));
441 CERROR("lock enqueue: err: %d\n", err);
446 CDEBUG(D_INFO, "Reading inode %ld, %d bytes, offset %Ld\n",
447 inode->i_ino, count, *ppos);
448 retval = generic_file_read(filp, buf, count, ppos);
451 ll_update_atime(inode);
453 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
454 !(sbi->ll_flags & LL_SBI_NOLCK)) {
455 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PR, lockhs);
456 if (err != ELDLM_OK) {
457 CERROR("lock cancel: err: %d\n", err);
463 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
468 * Write to a file (through the page cache).
471 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
473 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
474 struct inode *inode = file->f_dentry->d_inode;
475 struct ll_sb_info *sbi = ll_i2sbi(inode);
476 struct lustre_handle *lockhs = NULL, *eof_lockhs = NULL;
477 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
483 if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
490 err = ll_size_lock(inode, lsm, 0, LCK_PW, &eof_lockhs);
496 oa->o_id = lsm->lsm_object_id;
497 oa->o_mode = inode->i_mode;
498 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
500 obd_handle2oa(oa, &fd->fd_osthandle);
501 retval = obd_getattr(&sbi->ll_osc_conn, oa, lsm);
504 GOTO(out_eof, retval);
508 obdo_to_inode(inode, oa, oa->o_valid);
512 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
513 !(sbi->ll_flags & LL_SBI_NOLCK)) {
514 struct ldlm_extent extent;
515 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
517 GOTO(out_eof, retval = -ENOMEM);
518 extent.start = *ppos;
519 extent.end = *ppos + count;
520 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
521 inode->i_ino, extent.start, extent.end);
523 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
524 &extent, sizeof(extent), LCK_PW, &flags,
525 ll_lock_callback, inode, sizeof(*inode),
527 if (err != ELDLM_OK) {
528 CERROR("lock enqueue: err: %d\n", err);
529 GOTO(out_free, retval = err);
533 CDEBUG(D_INFO, "Writing inode %ld, %ld bytes, offset "LPD64"\n",
534 inode->i_ino, (long)count, *ppos);
536 retval = generic_file_write(file, buf, count, ppos);
538 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
539 sbi->ll_flags & LL_SBI_NOLCK) {
540 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PW, lockhs);
541 if (err != ELDLM_OK) {
542 CERROR("lock cancel: err: %d\n", err);
543 GOTO(out_free, retval = err);
550 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
553 if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
554 err = ll_size_unlock(inode, lsm, LCK_PW, eof_lockhs);
562 static int ll_lov_setstripe(struct inode *inode, struct file *file,
563 struct lov_user_md *lum)
565 struct ll_inode_info *lli = ll_i2info(inode);
566 struct lov_stripe_md *lsm;
567 int size = ll_mds_easize(inode->i_sb);
570 rc = verify_area(VERIFY_READ, lum, sizeof(*lum));
574 down(&lli->lli_open_sem);
576 CERROR("striping data already set for %d\n", inode->i_ino);
577 GOTO(out_lov_up, rc = -EPERM);
580 OBD_ALLOC(lli->lli_smd, size);
582 GOTO(out_lov_up, rc = -ENOMEM);
585 lsm->lsm_magic = LOV_MAGIC;
586 lsm->lsm_stripe_size = lum->lum_stripe_size;
587 lsm->lsm_stripe_pattern = lum->lum_stripe_pattern;
588 lsm->lsm_stripe_offset = lum->lum_stripe_offset;
589 lsm->lsm_stripe_count = lum->lum_stripe_count;
590 lsm->lsm_mds_easize = size;
592 file->f_flags &= ~O_LOV_DELAY_CREATE;
593 rc = ll_create_objects(inode->i_sb, inode->i_ino, 0, 0, &lsm);
595 OBD_FREE(lli->lli_smd, size);
597 rc = ll_file_open(inode, file);
599 up(&lli->lli_open_sem);
603 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
606 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
610 case LL_IOC_GETFLAGS:
611 /* Get the current value of the file flags */
612 return put_user(fd->fd_flags, (int *)arg);
613 case LL_IOC_SETFLAGS:
614 case LL_IOC_CLRFLAGS:
615 /* Set or clear specific file flags */
616 /* XXX This probably needs checks to ensure the flags are
617 * not abused, and to handle any flag side effects.
619 if (get_user(flags, (int *) arg))
622 if (cmd == LL_IOC_SETFLAGS)
623 fd->fd_flags |= flags;
625 fd->fd_flags &= ~flags;
627 case LL_IOC_LOV_SETSTRIPE:
628 return ll_lov_setstripe(inode, file, (struct lov_user_md *)arg);
630 /* We need to special case any other ioctls we want to handle,
631 * to send them to the MDS/OST as appropriate and to properly
632 * network encode the arg field.
633 case EXT2_IOC_GETFLAGS:
634 case EXT2_IOC_SETFLAGS:
635 case EXT2_IOC_GETVERSION_OLD:
636 case EXT2_IOC_GETVERSION_NEW:
637 case EXT2_IOC_SETVERSION_OLD:
638 case EXT2_IOC_SETVERSION_NEW:
645 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
647 struct inode *inode = file->f_dentry->d_inode;
653 struct ll_inode_info *lli = ll_i2info(inode);
655 retval = ll_file_size(inode, lli->lli_smd);
659 offset += inode->i_size;
663 offset += file->f_pos;
666 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
667 if (offset != file->f_pos) {
668 file->f_pos = offset;
669 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
672 file->f_version = ++event;
679 /* XXX this does not need to do anything for data, it _does_ need to
681 int ll_fsync(struct file *file, struct dentry *dentry, int data)
688 static int ll_inode_revalidate(struct dentry *dentry)
690 struct inode *inode = dentry->d_inode;
691 struct lov_stripe_md *lsm;
697 lsm = ll_i2info(inode)->lli_smd;
698 if (!lsm) /* object not yet allocated, don't validate size */
701 RETURN(ll_file_size(inode, lsm));
704 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
705 static int ll_getattr(struct vfsmount *mnt, struct dentry *de,
708 return ll_inode_revalidate(de);
712 struct file_operations ll_file_operations = {
714 write: ll_file_write,
715 ioctl: ll_file_ioctl,
717 release: ll_file_release,
718 mmap: generic_file_mmap,
719 llseek: ll_file_seek,
723 struct inode_operations ll_file_inode_operations = {
725 truncate: ll_truncate,
726 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
729 revalidate: ll_inode_revalidate,