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 static int ll_create_objects(struct inode *inode, struct ll_inode_info *lli)
45 oa->o_mode = S_IFREG | 0600;
46 oa->o_easize = ll_mds_easize(inode->i_sb);
47 oa->o_id = inode->i_ino;
48 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
49 OBD_MD_FLMODE | OBD_MD_FLEASIZE;
50 rc = obd_create(ll_i2obdconn(inode), oa, &lli->lli_smd);
54 LASSERT(lli->lli_smd->lsm_object_id);
58 static int ll_file_open(struct inode *inode, struct file *file)
60 struct ptlrpc_request *req = NULL;
61 struct ll_file_data *fd;
63 struct lov_stripe_md *lsm = NULL;
64 struct ll_sb_info *sbi = ll_i2sbi(inode);
65 struct ll_inode_info *lli = ll_i2info(inode);
69 LASSERT(!file->private_data);
71 CHECK_MOUNT_EPOCH(inode);
75 /* delayed create of object (intent created inode) */
76 /* XXX object needs to be cleaned up if mdc_open fails */
77 /* XXX error handling appropriate here? */
79 if (file->f_flags & O_LOV_DELAY_CREATE) {
80 CDEBUG(D_INODE, "delaying object creation\n");
83 down(&lli->lli_open_sem);
84 /* Check to see if we lost the race */
86 rc = ll_create_objects(inode, lli);
87 up(&lli->lli_open_sem);
94 fd = kmem_cache_alloc(ll_file_data_slab, SLAB_KERNEL);
96 GOTO(out, rc = -ENOMEM);
97 memset(fd, 0, sizeof(*fd));
99 fd->fd_mdshandle.addr = (__u64)(unsigned long)file;
100 get_random_bytes(&fd->fd_mdshandle.cookie,
101 sizeof(fd->fd_mdshandle.cookie));
102 rc = mdc_open(&sbi->ll_mdc_conn, inode->i_ino, S_IFREG | inode->i_mode,
103 file->f_flags, lsm, &fd->fd_mdshandle, &req);
106 /* We don't call ptlrpc_req_finished here, because the request is
107 * preserved until we see a matching close, at which point it is
108 * released (and likely freed). (See ll_file_release.)
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;
125 rc = obd_open(ll_i2obdconn(inode), oa, lsm);
127 obd_oa2handle(&fd->fd_osthandle, oa);
131 GOTO(out_mdc, rc = -abs(rc));
133 file->private_data = fd;
137 mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
138 S_IFREG, &fd->fd_mdshandle, &req);
140 ptlrpc_free_req(req);
142 fd->fd_mdshandle.cookie = DEAD_HANDLE_MAGIC;
143 kmem_cache_free(ll_file_data_slab, fd);
148 int ll_size_lock(struct inode *inode, struct lov_stripe_md *lsm, obd_off start,
149 int mode, struct lustre_handle **lockhs_p)
151 struct ll_sb_info *sbi = ll_i2sbi(inode);
152 struct ldlm_extent extent;
153 struct lustre_handle *lockhs = NULL;
154 int rc, flags = 0, stripe_count;
157 if (sbi->ll_flags & LL_SBI_NOLCK) {
162 stripe_count = lsm->lsm_stripe_count;
166 OBD_ALLOC(lockhs, stripe_count * sizeof(*lockhs));
170 extent.start = start;
171 extent.end = OBD_OBJECT_EOF;
173 rc = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT, &extent,
174 sizeof(extent), mode, &flags, ll_lock_callback,
175 inode, sizeof(*inode), lockhs);
176 if (rc != ELDLM_OK) {
177 CERROR("lock enqueue: %d\n", rc);
178 OBD_FREE(lockhs, stripe_count * sizeof(*lockhs));
184 int ll_size_unlock(struct inode *inode, struct lov_stripe_md *lsm, int mode,
185 struct lustre_handle *lockhs)
187 struct ll_sb_info *sbi = ll_i2sbi(inode);
188 int rc, stripe_count;
191 if (sbi->ll_flags & LL_SBI_NOLCK)
194 if (lockhs == NULL) {
199 rc = obd_cancel(&sbi->ll_osc_conn, lsm, mode, lockhs);
200 if (rc != ELDLM_OK) {
201 CERROR("lock cancel: %d\n", rc);
205 stripe_count = lsm->lsm_stripe_count;
209 OBD_FREE(lockhs, stripe_count * sizeof(*lockhs));
213 int ll_file_size(struct inode *inode, struct lov_stripe_md *lsm)
215 struct ll_sb_info *sbi = ll_i2sbi(inode);
216 struct lustre_handle *lockhs;
224 rc = ll_size_lock(inode, lsm, 0, LCK_PR, &lockhs);
225 if (rc != ELDLM_OK) {
226 CERROR("lock enqueue: %d\n", rc);
230 oa.o_id = lsm->lsm_object_id;
232 oa.o_valid = OBD_MD_FLID|OBD_MD_FLTYPE|OBD_MD_FLSIZE|OBD_MD_FLBLOCKS;
233 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lsm);
235 obdo_to_inode(inode, &oa,
236 oa.o_valid & ~(OBD_MD_FLTYPE | OBD_MD_FLMODE));
238 err = ll_size_unlock(inode, lsm, LCK_PR, lockhs);
239 if (err != ELDLM_OK) {
240 CERROR("lock cancel: %d\n", err);
246 static int ll_file_release(struct inode *inode, struct file *file)
248 struct ptlrpc_request *req = NULL;
249 struct ll_file_data *fd;
251 struct ll_sb_info *sbi = ll_i2sbi(inode);
252 struct ll_inode_info *lli = ll_i2info(inode);
253 struct lov_stripe_md *lsm = lli->lli_smd;
258 CHECK_MOUNT_EPOCH(inode);
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 if (file->f_mode & FMODE_WRITE) {
278 struct lustre_handle *lockhs;
280 rc = ll_size_lock(inode, lsm, 0, LCK_PR, &lockhs);
282 GOTO(out_mdc, -abs(rc));
284 oa.o_id = lsm->lsm_object_id;
286 oa.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
288 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lsm);
291 attr.ia_valid = (ATTR_MTIME | ATTR_CTIME | ATTR_ATIME |
293 attr.ia_mtime = inode->i_mtime;
294 attr.ia_ctime = inode->i_ctime;
295 attr.ia_atime = inode->i_atime;
296 attr.ia_size = oa.o_size;
298 inode->i_blocks = oa.o_blocks;
300 /* XXX: this introduces a small race that we should
302 rc = ll_inode_setattr(inode, &attr, 0);
304 rc2 = ll_size_unlock(inode, lli->lli_smd, LCK_PR, lockhs);
306 CERROR("lock cancel: %d\n", rc);
314 rc2 = mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
315 S_IFREG, &fd->fd_mdshandle, &req);
316 ptlrpc_req_finished(req);
322 CDEBUG(D_HA, "matched req %p xid "LPD64" transno "LPD64" op %d->%s:%d\n",
323 fd->fd_req, fd->fd_req->rq_xid, fd->fd_req->rq_repmsg->transno,
324 fd->fd_req->rq_reqmsg->opc,
325 fd->fd_req->rq_import->imp_connection->c_remote_uuid,
326 fd->fd_req->rq_import->imp_client->cli_request_portal);
327 ptlrpc_req_finished(fd->fd_req);
329 rc = obd_cancel_unused(ll_i2obdconn(inode), lsm, 0);
331 CERROR("obd_cancel_unused: %d\n", rc);
336 fd->fd_mdshandle.cookie = DEAD_HANDLE_MAGIC;
337 file->private_data = NULL;
338 kmem_cache_free(ll_file_data_slab, fd);
344 static inline void ll_remove_suid(struct inode *inode)
348 /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
349 mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
351 /* was any of the uid bits set? */
352 mode &= inode->i_mode;
353 if (mode && !capable(CAP_FSETID)) {
354 inode->i_mode &= ~mode;
355 // XXX careful here - we cannot change the size
359 static void ll_update_atime(struct inode *inode)
363 attr.ia_atime = CURRENT_TIME;
364 attr.ia_valid = ATTR_ATIME;
366 if (inode->i_atime == attr.ia_atime) return;
367 if (IS_RDONLY(inode)) return;
368 if (IS_NOATIME(inode)) return;
370 /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
371 ll_inode_setattr(inode, &attr, 0);
374 int ll_lock_callback(struct ldlm_lock *lock, struct ldlm_lock_desc *new,
375 void *data, __u32 data_len, int flag)
377 struct inode *inode = data;
378 struct lustre_handle lockh;
382 if (data_len != sizeof(struct inode))
389 case LDLM_CB_BLOCKING:
390 ldlm_lock2handle(lock, &lockh);
391 rc = ldlm_cli_cancel(&lockh);
393 CERROR("ldlm_cli_cancel failed: %d\n", rc);
395 case LDLM_CB_CANCELING:
396 CDEBUG(D_INODE, "invalidating obdo/inode %ld\n", inode->i_ino);
397 /* FIXME: do something better than throwing away everything */
398 //down(&inode->i_sem);
399 invalidate_inode_pages(inode);
409 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
412 struct ll_file_data *fd = (struct ll_file_data *)filp->private_data;
413 struct inode *inode = filp->f_dentry->d_inode;
414 struct ll_sb_info *sbi = ll_i2sbi(inode);
415 struct ldlm_extent extent;
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 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
429 extent.start = *ppos;
430 extent.end = *ppos + count;
431 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
432 inode->i_ino, extent.start, extent.end);
434 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
435 &extent, sizeof(extent), LCK_PR, &flags,
436 ll_lock_callback, inode, sizeof(*inode),
438 if (err != ELDLM_OK) {
439 OBD_FREE(lockhs, lsm->lsm_stripe_count*sizeof(*lockhs));
440 CERROR("lock enqueue: err: %d\n", err);
445 CDEBUG(D_INFO, "Reading inode %ld, %d bytes, offset %Ld\n",
446 inode->i_ino, count, *ppos);
447 retval = generic_file_read(filp, buf, count, ppos);
450 ll_update_atime(inode);
452 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
453 !(sbi->ll_flags & LL_SBI_NOLCK)) {
454 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PR, lockhs);
455 if (err != ELDLM_OK) {
456 CERROR("lock cancel: err: %d\n", err);
462 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
467 * Write to a file (through the page cache).
470 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
472 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
473 struct inode *inode = file->f_dentry->d_inode;
474 struct ll_sb_info *sbi = ll_i2sbi(inode);
475 struct ldlm_extent extent;
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 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
516 GOTO(out_eof, retval = -ENOMEM);
517 extent.start = *ppos;
518 extent.end = *ppos + count;
519 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
520 inode->i_ino, extent.start, extent.end);
522 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
523 &extent, sizeof(extent), LCK_PW, &flags,
524 ll_lock_callback, inode, sizeof(*inode),
526 if (err != ELDLM_OK) {
527 CERROR("lock enqueue: err: %d\n", err);
528 GOTO(out_free, retval = err);
532 CDEBUG(D_INFO, "Writing inode %ld, %ld bytes, offset "LPD64"\n",
533 inode->i_ino, (long)count, *ppos);
535 retval = generic_file_write(file, buf, count, ppos);
537 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
538 sbi->ll_flags & LL_SBI_NOLCK) {
539 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PW, lockhs);
540 if (err != ELDLM_OK) {
541 CERROR("lock cancel: err: %d\n", err);
542 GOTO(out_free, retval = err);
549 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
552 if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
553 err = ll_size_unlock(inode, lsm, LCK_PW, eof_lockhs);
561 static int ll_lov_setstripe(struct inode *inode, struct file *file,
562 struct lov_user_md *lum)
564 struct ll_inode_info *lli = ll_i2info(inode);
565 struct lov_stripe_md *lsm;
566 int size = ll_mds_easize(inode->i_sb);
569 rc = verify_area(VERIFY_READ, lum, sizeof(*lum));
573 down(&lli->lli_open_sem);
575 CERROR("striping data already set for %d\n", inode->i_ino);
576 GOTO(out_lov_up, rc = -EPERM);
579 OBD_ALLOC(lli->lli_smd, size);
581 GOTO(out_lov_up, rc = -ENOMEM);
584 lsm->lsm_magic = LOV_MAGIC;
585 lsm->lsm_stripe_size = lum->lum_stripe_size;
586 lsm->lsm_stripe_pattern = lum->lum_stripe_pattern;
587 lsm->lsm_stripe_offset = lum->lum_stripe_offset;
588 lsm->lsm_stripe_count = lum->lum_stripe_count;
589 lsm->lsm_mds_easize = size;
591 file->f_flags &= ~O_LOV_DELAY_CREATE;
592 rc = ll_create_objects(inode, lli);
594 OBD_FREE(lli->lli_smd, size);
596 rc = ll_file_open(inode, file);
598 up(&lli->lli_open_sem);
602 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
605 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
609 case LL_IOC_GETFLAGS:
610 /* Get the current value of the file flags */
611 return put_user(fd->fd_flags, (int *)arg);
612 case LL_IOC_SETFLAGS:
613 case LL_IOC_CLRFLAGS:
614 /* Set or clear specific file flags */
615 /* XXX This probably needs checks to ensure the flags are
616 * not abused, and to handle any flag side effects.
618 if (get_user(flags, (int *) arg))
621 if (cmd == LL_IOC_SETFLAGS)
622 fd->fd_flags |= flags;
624 fd->fd_flags &= ~flags;
626 case LL_IOC_LOV_SETSTRIPE:
627 return ll_lov_setstripe(inode, file, (struct lov_user_md *)arg);
629 /* We need to special case any other ioctls we want to handle,
630 * to send them to the MDS/OST as appropriate and to properly
631 * network encode the arg field.
632 case EXT2_IOC_GETFLAGS:
633 case EXT2_IOC_SETFLAGS:
634 case EXT2_IOC_GETVERSION_OLD:
635 case EXT2_IOC_GETVERSION_NEW:
636 case EXT2_IOC_SETVERSION_OLD:
637 case EXT2_IOC_SETVERSION_NEW:
644 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
646 struct inode *inode = file->f_dentry->d_inode;
650 CHECK_MOUNT_EPOCH(inode);
654 struct ll_inode_info *lli = ll_i2info(inode);
656 retval = ll_file_size(inode, lli->lli_smd);
660 offset += inode->i_size;
664 offset += file->f_pos;
667 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
668 if (offset != file->f_pos) {
669 file->f_pos = offset;
671 file->f_version = ++event;
678 /* XXX this does not need to do anything for data, it _does_ need to
680 int ll_fsync(struct file *file, struct dentry *dentry, int data)
685 static int ll_inode_revalidate(struct dentry *dentry)
687 struct inode *inode = dentry->d_inode;
688 struct lov_stripe_md *lsm;
694 lsm = ll_i2info(inode)->lli_smd;
695 if (!lsm) /* object not yet allocated, don't validate size */
698 RETURN(ll_file_size(inode, lsm));
701 struct file_operations ll_file_operations = {
703 write: ll_file_write,
704 ioctl: ll_file_ioctl,
706 release: ll_file_release,
707 mmap: generic_file_mmap,
708 llseek: ll_file_seek,
712 struct inode_operations ll_file_inode_operations = {
713 truncate: ll_truncate,
715 revalidate: ll_inode_revalidate