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 /* We don't call ptlrpc_req_finished here, because the request is
109 * preserved until we see a matching close, at which point it is
110 * released (and likely freed). (See ll_file_release.)
113 GOTO(out_req, -abs(rc));
114 if (!fd->fd_mdshandle.addr ||
115 fd->fd_mdshandle.addr == (__u64)(unsigned long)file) {
116 CERROR("hmm, mdc_open didn't assign fd_mdshandle?\n");
117 /* XXX handle this how, abort or is it non-fatal? */
122 GOTO(out_mdc, rc = -EINVAL);
124 oa->o_id = lsm->lsm_object_id;
125 oa->o_mode = S_IFREG;
126 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
128 rc = obd_open(ll_i2obdconn(inode), oa, lsm);
129 obdo_to_inode(inode, oa, oa->o_valid & (OBD_MD_FLSIZE|OBD_MD_FLBLOCKS));
131 obd_oa2handle(&fd->fd_osthandle, oa);
135 GOTO(out_mdc, rc = -abs(rc));
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));
277 /* If this fails and we goto out_fd, the file size on the MDS is out of
278 * date. Is that a big deal? */
279 #warning "FIXME: don't do this if the file is unlinked already"
280 if (file->f_mode & FMODE_WRITE) {
281 struct lustre_handle *lockhs;
283 rc = ll_size_lock(inode, lsm, 0, LCK_PR, &lockhs);
285 GOTO(out_mdc, -abs(rc));
287 oa.o_id = lsm->lsm_object_id;
289 oa.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
291 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lsm);
294 attr.ia_valid = (ATTR_MTIME | ATTR_CTIME | ATTR_ATIME |
296 attr.ia_mtime = inode->i_mtime;
297 attr.ia_ctime = inode->i_ctime;
298 attr.ia_atime = inode->i_atime;
299 attr.ia_size = oa.o_size;
301 inode->i_blocks = oa.o_blocks;
303 /* XXX: this introduces a small race that we should
305 rc = ll_inode_setattr(inode, &attr, 0);
307 rc2 = ll_size_unlock(inode, lli->lli_smd, LCK_PR, lockhs);
309 CERROR("lock cancel: %d\n", rc);
317 rc2 = mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
318 S_IFREG, &fd->fd_mdshandle, &req);
319 ptlrpc_req_finished(req);
325 CDEBUG(D_HA, "matched req %p xid "LPD64" transno "LPD64" op %d->%s:%d\n",
326 fd->fd_req, fd->fd_req->rq_xid, fd->fd_req->rq_repmsg->transno,
327 fd->fd_req->rq_reqmsg->opc,
328 fd->fd_req->rq_import->imp_connection->c_remote_uuid,
329 fd->fd_req->rq_import->imp_client->cli_request_portal);
330 ptlrpc_req_finished(fd->fd_req);
332 rc = obd_cancel_unused(ll_i2obdconn(inode), lsm, 0);
334 CERROR("obd_cancel_unused: %d\n", rc);
339 fd->fd_mdshandle.cookie = DEAD_HANDLE_MAGIC;
340 file->private_data = NULL;
341 kmem_cache_free(ll_file_data_slab, fd);
347 static inline void ll_remove_suid(struct inode *inode)
351 /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
352 mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
354 /* was any of the uid bits set? */
355 mode &= inode->i_mode;
356 if (mode && !capable(CAP_FSETID)) {
357 inode->i_mode &= ~mode;
358 // XXX careful here - we cannot change the size
362 static void ll_update_atime(struct inode *inode)
366 attr.ia_atime = CURRENT_TIME;
367 attr.ia_valid = ATTR_ATIME;
369 if (inode->i_atime == attr.ia_atime) return;
370 if (IS_RDONLY(inode)) return;
371 if (IS_NOATIME(inode)) return;
373 /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
374 ll_inode_setattr(inode, &attr, 0);
377 int ll_lock_callback(struct ldlm_lock *lock, struct ldlm_lock_desc *new,
378 void *data, __u32 data_len, int flag)
380 struct inode *inode = data;
381 struct lustre_handle lockh;
385 if (data_len != sizeof(struct inode))
392 case LDLM_CB_BLOCKING:
393 ldlm_lock2handle(lock, &lockh);
394 rc = ldlm_cli_cancel(&lockh);
396 CERROR("ldlm_cli_cancel failed: %d\n", rc);
398 case LDLM_CB_CANCELING:
399 CDEBUG(D_INODE, "invalidating obdo/inode %ld\n", inode->i_ino);
400 /* FIXME: do something better than throwing away everything */
401 //down(&inode->i_sem);
402 ll_invalidate_inode_pages(inode);
412 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
415 struct ll_file_data *fd = (struct ll_file_data *)filp->private_data;
416 struct inode *inode = filp->f_dentry->d_inode;
417 struct ll_sb_info *sbi = ll_i2sbi(inode);
418 struct ldlm_extent extent;
419 struct lustre_handle *lockhs = NULL;
420 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
426 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
427 !(sbi->ll_flags & LL_SBI_NOLCK)) {
428 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
432 extent.start = *ppos;
433 extent.end = *ppos + count;
434 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
435 inode->i_ino, extent.start, extent.end);
437 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
438 &extent, sizeof(extent), LCK_PR, &flags,
439 ll_lock_callback, inode, sizeof(*inode),
441 if (err != ELDLM_OK) {
442 OBD_FREE(lockhs, lsm->lsm_stripe_count*sizeof(*lockhs));
443 CERROR("lock enqueue: err: %d\n", err);
448 CDEBUG(D_INFO, "Reading inode %ld, %d bytes, offset %Ld\n",
449 inode->i_ino, count, *ppos);
450 retval = generic_file_read(filp, buf, count, ppos);
453 ll_update_atime(inode);
455 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
456 !(sbi->ll_flags & LL_SBI_NOLCK)) {
457 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PR, lockhs);
458 if (err != ELDLM_OK) {
459 CERROR("lock cancel: err: %d\n", err);
465 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
470 * Write to a file (through the page cache).
473 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
475 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
476 struct inode *inode = file->f_dentry->d_inode;
477 struct ll_sb_info *sbi = ll_i2sbi(inode);
478 struct ldlm_extent extent;
479 struct lustre_handle *lockhs = NULL, *eof_lockhs = NULL;
480 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
486 if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
493 err = ll_size_lock(inode, lsm, 0, LCK_PW, &eof_lockhs);
499 oa->o_id = lsm->lsm_object_id;
500 oa->o_mode = inode->i_mode;
501 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
503 obd_handle2oa(oa, &fd->fd_osthandle);
504 retval = obd_getattr(&sbi->ll_osc_conn, oa, lsm);
507 GOTO(out_eof, retval);
511 obdo_to_inode(inode, oa, oa->o_valid);
515 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
516 !(sbi->ll_flags & LL_SBI_NOLCK)) {
517 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
519 GOTO(out_eof, retval = -ENOMEM);
520 extent.start = *ppos;
521 extent.end = *ppos + count;
522 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
523 inode->i_ino, extent.start, extent.end);
525 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
526 &extent, sizeof(extent), LCK_PW, &flags,
527 ll_lock_callback, inode, sizeof(*inode),
529 if (err != ELDLM_OK) {
530 CERROR("lock enqueue: err: %d\n", err);
531 GOTO(out_free, retval = err);
535 CDEBUG(D_INFO, "Writing inode %ld, %ld bytes, offset "LPD64"\n",
536 inode->i_ino, (long)count, *ppos);
538 retval = generic_file_write(file, buf, count, ppos);
540 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
541 sbi->ll_flags & LL_SBI_NOLCK) {
542 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PW, lockhs);
543 if (err != ELDLM_OK) {
544 CERROR("lock cancel: err: %d\n", err);
545 GOTO(out_free, retval = err);
552 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
555 if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
556 err = ll_size_unlock(inode, lsm, LCK_PW, eof_lockhs);
564 static int ll_lov_setstripe(struct inode *inode, struct file *file,
565 struct lov_user_md *lum)
567 struct ll_inode_info *lli = ll_i2info(inode);
568 struct lov_stripe_md *lsm;
569 int size = ll_mds_easize(inode->i_sb);
572 rc = verify_area(VERIFY_READ, lum, sizeof(*lum));
576 down(&lli->lli_open_sem);
578 CERROR("striping data already set for %d\n", inode->i_ino);
579 GOTO(out_lov_up, rc = -EPERM);
582 OBD_ALLOC(lli->lli_smd, size);
584 GOTO(out_lov_up, rc = -ENOMEM);
587 lsm->lsm_magic = LOV_MAGIC;
588 lsm->lsm_stripe_size = lum->lum_stripe_size;
589 lsm->lsm_stripe_pattern = lum->lum_stripe_pattern;
590 lsm->lsm_stripe_offset = lum->lum_stripe_offset;
591 lsm->lsm_stripe_count = lum->lum_stripe_count;
592 lsm->lsm_mds_easize = size;
594 file->f_flags &= ~O_LOV_DELAY_CREATE;
595 rc = ll_create_objects(inode->i_sb, inode->i_ino, 0, 0, &lsm);
597 OBD_FREE(lli->lli_smd, size);
599 rc = ll_file_open(inode, file);
601 up(&lli->lli_open_sem);
605 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
608 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
612 case LL_IOC_GETFLAGS:
613 /* Get the current value of the file flags */
614 return put_user(fd->fd_flags, (int *)arg);
615 case LL_IOC_SETFLAGS:
616 case LL_IOC_CLRFLAGS:
617 /* Set or clear specific file flags */
618 /* XXX This probably needs checks to ensure the flags are
619 * not abused, and to handle any flag side effects.
621 if (get_user(flags, (int *) arg))
624 if (cmd == LL_IOC_SETFLAGS)
625 fd->fd_flags |= flags;
627 fd->fd_flags &= ~flags;
629 case LL_IOC_LOV_SETSTRIPE:
630 return ll_lov_setstripe(inode, file, (struct lov_user_md *)arg);
632 /* We need to special case any other ioctls we want to handle,
633 * to send them to the MDS/OST as appropriate and to properly
634 * network encode the arg field.
635 case EXT2_IOC_GETFLAGS:
636 case EXT2_IOC_SETFLAGS:
637 case EXT2_IOC_GETVERSION_OLD:
638 case EXT2_IOC_GETVERSION_NEW:
639 case EXT2_IOC_SETVERSION_OLD:
640 case EXT2_IOC_SETVERSION_NEW:
647 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
649 struct inode *inode = file->f_dentry->d_inode;
655 struct ll_inode_info *lli = ll_i2info(inode);
657 retval = ll_file_size(inode, lli->lli_smd);
661 offset += inode->i_size;
665 offset += file->f_pos;
668 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
669 if (offset != file->f_pos) {
670 file->f_pos = offset;
671 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
674 file->f_version = ++event;
681 /* XXX this does not need to do anything for data, it _does_ need to
683 int ll_fsync(struct file *file, struct dentry *dentry, int data)
690 static int ll_inode_revalidate(struct dentry *dentry)
692 struct inode *inode = dentry->d_inode;
693 struct lov_stripe_md *lsm;
699 lsm = ll_i2info(inode)->lli_smd;
700 if (!lsm) /* object not yet allocated, don't validate size */
703 RETURN(ll_file_size(inode, lsm));
706 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
707 static int ll_getattr(struct vfsmount *mnt, struct dentry *de,
710 return ll_inode_revalidate(de);
714 struct file_operations ll_file_operations = {
716 write: ll_file_write,
717 ioctl: ll_file_ioctl,
719 release: ll_file_release,
720 mmap: generic_file_mmap,
721 llseek: ll_file_seek,
725 struct inode_operations ll_file_inode_operations = {
727 truncate: ll_truncate,
728 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
731 revalidate: ll_inode_revalidate,