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 /* XXX Mike, we have also done this in ll_file_open? */
323 ptlrpc_req_finished(fd->fd_req);
325 rc = obd_cancel_unused(ll_i2obdconn(inode), lsm, 0);
327 CERROR("obd_cancel_unused: %d\n", rc);
332 fd->fd_mdshandle.cookie = DEAD_HANDLE_MAGIC;
333 file->private_data = NULL;
334 kmem_cache_free(ll_file_data_slab, fd);
340 static inline void ll_remove_suid(struct inode *inode)
344 /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
345 mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
347 /* was any of the uid bits set? */
348 mode &= inode->i_mode;
349 if (mode && !capable(CAP_FSETID)) {
350 inode->i_mode &= ~mode;
351 // XXX careful here - we cannot change the size
355 static void ll_update_atime(struct inode *inode)
359 attr.ia_atime = CURRENT_TIME;
360 attr.ia_valid = ATTR_ATIME;
362 if (inode->i_atime == attr.ia_atime) return;
363 if (IS_RDONLY(inode)) return;
364 if (IS_NOATIME(inode)) return;
366 /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
367 ll_inode_setattr(inode, &attr, 0);
370 int ll_lock_callback(struct ldlm_lock *lock, struct ldlm_lock_desc *new,
371 void *data, __u32 data_len, int flag)
373 struct inode *inode = data;
374 struct lustre_handle lockh;
378 if (data_len != sizeof(struct inode))
385 case LDLM_CB_BLOCKING:
386 ldlm_lock2handle(lock, &lockh);
387 rc = ldlm_cli_cancel(&lockh);
389 CERROR("ldlm_cli_cancel failed: %d\n", rc);
391 case LDLM_CB_CANCELING:
392 CDEBUG(D_INODE, "invalidating obdo/inode %ld\n", inode->i_ino);
393 /* FIXME: do something better than throwing away everything */
394 //down(&inode->i_sem);
395 invalidate_inode_pages(inode);
405 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
408 struct ll_file_data *fd = (struct ll_file_data *)filp->private_data;
409 struct inode *inode = filp->f_dentry->d_inode;
410 struct ll_sb_info *sbi = ll_i2sbi(inode);
411 struct ldlm_extent extent;
412 struct lustre_handle *lockhs = NULL;
413 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
419 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
420 !(sbi->ll_flags & LL_SBI_NOLCK)) {
421 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
425 extent.start = *ppos;
426 extent.end = *ppos + count;
427 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
428 inode->i_ino, extent.start, extent.end);
430 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
431 &extent, sizeof(extent), LCK_PR, &flags,
432 ll_lock_callback, inode, sizeof(*inode),
434 if (err != ELDLM_OK) {
435 OBD_FREE(lockhs, lsm->lsm_stripe_count*sizeof(*lockhs));
436 CERROR("lock enqueue: err: %d\n", err);
441 CDEBUG(D_INFO, "Reading inode %ld, %d bytes, offset %Ld\n",
442 inode->i_ino, count, *ppos);
443 retval = generic_file_read(filp, buf, count, ppos);
446 ll_update_atime(inode);
448 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
449 !(sbi->ll_flags & LL_SBI_NOLCK)) {
450 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PR, lockhs);
451 if (err != ELDLM_OK) {
452 CERROR("lock cancel: err: %d\n", err);
458 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
463 * Write to a file (through the page cache).
466 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
468 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
469 struct inode *inode = file->f_dentry->d_inode;
470 struct ll_sb_info *sbi = ll_i2sbi(inode);
471 struct ldlm_extent extent;
472 struct lustre_handle *lockhs = NULL, *eof_lockhs = NULL;
473 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
479 if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
486 err = ll_size_lock(inode, lsm, 0, LCK_PW, &eof_lockhs);
492 oa->o_id = lsm->lsm_object_id;
493 oa->o_mode = inode->i_mode;
494 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
496 obd_handle2oa(oa, &fd->fd_osthandle);
497 retval = obd_getattr(&sbi->ll_osc_conn, oa, lsm);
500 GOTO(out_eof, retval);
504 obdo_to_inode(inode, oa, oa->o_valid);
508 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
509 sbi->ll_flags & LL_SBI_NOLCK) {
510 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
512 GOTO(out_eof, retval = -ENOMEM);
513 extent.start = *ppos;
514 extent.end = *ppos + count;
515 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
516 inode->i_ino, extent.start, extent.end);
518 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
519 &extent, sizeof(extent), LCK_PW, &flags,
520 ll_lock_callback, inode, sizeof(*inode),
522 if (err != ELDLM_OK) {
523 CERROR("lock enqueue: err: %d\n", err);
524 GOTO(out_free, retval = err);
528 CDEBUG(D_INFO, "Writing inode %ld, %ld bytes, offset "LPD64"\n",
529 inode->i_ino, (long)count, *ppos);
531 retval = generic_file_write(file, buf, count, ppos);
533 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
534 sbi->ll_flags & LL_SBI_NOLCK) {
535 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PW, lockhs);
536 if (err != ELDLM_OK) {
537 CERROR("lock cancel: err: %d\n", err);
538 GOTO(out_free, retval = err);
545 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
548 if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
549 err = ll_size_unlock(inode, lsm, LCK_PW, eof_lockhs);
557 static int ll_lov_setstripe(struct inode *inode, struct file *file,
558 struct lov_user_md *lum)
560 struct ll_inode_info *lli = ll_i2info(inode);
561 struct lov_stripe_md *lsm;
562 int size = ll_mds_easize(inode->i_sb);
565 rc = verify_area(VERIFY_READ, lum, sizeof(*lum));
569 down(&lli->lli_open_sem);
571 CERROR("striping data already set for %d\n", inode->i_ino);
572 GOTO(out_lov_up, rc = -EPERM);
575 OBD_ALLOC(lli->lli_smd, size);
577 GOTO(out_lov_up, rc = -ENOMEM);
580 lsm->lsm_magic = LOV_MAGIC;
581 lsm->lsm_stripe_size = lum->lum_stripe_size;
582 lsm->lsm_stripe_pattern = lum->lum_stripe_pattern;
583 lsm->lsm_stripe_offset = lum->lum_stripe_offset;
584 lsm->lsm_stripe_count = lum->lum_stripe_count;
585 lsm->lsm_mds_easize = size;
587 file->f_flags &= ~O_LOV_DELAY_CREATE;
588 rc = ll_create_objects(inode, lli);
590 OBD_FREE(lli->lli_smd, size);
592 rc = ll_file_open(inode, file);
594 up(&lli->lli_open_sem);
598 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
601 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
605 case LL_IOC_GETFLAGS:
606 /* Get the current value of the file flags */
607 return put_user(fd->fd_flags, (int *)arg);
608 case LL_IOC_SETFLAGS:
609 case LL_IOC_CLRFLAGS:
610 /* Set or clear specific file flags */
611 /* XXX This probably needs checks to ensure the flags are
612 * not abused, and to handle any flag side effects.
614 if (get_user(flags, (int *) arg))
617 if (cmd == LL_IOC_SETFLAGS)
618 fd->fd_flags |= flags;
620 fd->fd_flags &= ~flags;
622 case LL_IOC_LOV_SETSTRIPE:
623 return ll_lov_setstripe(inode, file, (struct lov_user_md *)arg);
625 /* We need to special case any other ioctls we want to handle,
626 * to send them to the MDS/OST as appropriate and to properly
627 * network encode the arg field.
628 case EXT2_IOC_GETFLAGS:
629 case EXT2_IOC_SETFLAGS:
630 case EXT2_IOC_GETVERSION_OLD:
631 case EXT2_IOC_GETVERSION_NEW:
632 case EXT2_IOC_SETVERSION_OLD:
633 case EXT2_IOC_SETVERSION_NEW:
640 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
642 struct inode *inode = file->f_dentry->d_inode;
646 CHECK_MOUNT_EPOCH(inode);
650 struct ll_inode_info *lli = ll_i2info(inode);
652 retval = ll_file_size(inode, lli->lli_smd);
656 offset += inode->i_size;
660 offset += file->f_pos;
663 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
664 if (offset != file->f_pos) {
665 file->f_pos = offset;
667 file->f_version = ++event;
674 /* XXX this does not need to do anything for data, it _does_ need to
676 int ll_fsync(struct file *file, struct dentry *dentry, int data)
681 static int ll_inode_revalidate(struct dentry *dentry)
683 struct inode *inode = dentry->d_inode;
684 struct lov_stripe_md *lsm;
690 lsm = ll_i2info(inode)->lli_smd;
691 if (!lsm) /* object not yet allocated, don't validate size */
694 RETURN(ll_file_size(inode, lsm));
697 struct file_operations ll_file_operations = {
699 write: ll_file_write,
700 ioctl: ll_file_ioctl,
702 release: ll_file_release,
703 mmap: generic_file_mmap,
704 llseek: ll_file_seek,
708 struct inode_operations ll_file_inode_operations = {
709 truncate: ll_truncate,
711 revalidate: ll_inode_revalidate