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);
105 ptlrpc_req_finished(req);
107 GOTO(out_req, -abs(rc));
108 if (!fd->fd_mdshandle.addr ||
109 fd->fd_mdshandle.addr == (__u64)(unsigned long)file) {
110 CERROR("hmm, mdc_open didn't assign fd_mdshandle?\n");
111 /* XXX handle this how, abort or is it non-fatal? */
116 GOTO(out_mdc, rc = -EINVAL);
118 oa->o_id = lsm->lsm_object_id;
119 oa->o_mode = S_IFREG;
120 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE;
121 rc = obd_open(ll_i2obdconn(inode), oa, lsm);
123 obd_oa2handle(&fd->fd_osthandle, oa);
127 GOTO(out_mdc, rc = -abs(rc));
129 file->private_data = fd;
133 mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
134 S_IFREG, &fd->fd_mdshandle, &req);
136 ptlrpc_free_req(req);
138 fd->fd_mdshandle.cookie = DEAD_HANDLE_MAGIC;
139 kmem_cache_free(ll_file_data_slab, fd);
144 int ll_size_lock(struct inode *inode, struct lov_stripe_md *lsm, __u64 start,
145 int mode, struct lustre_handle **lockhs_p)
147 struct ll_sb_info *sbi = ll_i2sbi(inode);
148 struct ldlm_extent extent;
149 struct lustre_handle *lockhs = NULL;
150 int rc, flags = 0, stripe_count;
153 if (sbi->ll_flags & LL_SBI_NOLCK) {
158 stripe_count = lsm->lsm_stripe_count;
162 OBD_ALLOC(lockhs, stripe_count * sizeof(*lockhs));
166 extent.start = start;
167 extent.end = OBD_OBJECT_EOF;
169 rc = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT, &extent,
170 sizeof(extent), mode, &flags, ll_lock_callback,
171 inode, sizeof(*inode), lockhs);
172 if (rc != ELDLM_OK) {
173 CERROR("lock enqueue: %d\n", rc);
174 OBD_FREE(lockhs, stripe_count * sizeof(*lockhs));
180 int ll_size_unlock(struct inode *inode, struct lov_stripe_md *lsm, int mode,
181 struct lustre_handle *lockhs)
183 struct ll_sb_info *sbi = ll_i2sbi(inode);
184 int rc, stripe_count;
187 if (sbi->ll_flags & LL_SBI_NOLCK)
190 if (lockhs == NULL) {
195 rc = obd_cancel(&sbi->ll_osc_conn, lsm, mode, lockhs);
196 if (rc != ELDLM_OK) {
197 CERROR("lock cancel: %d\n", rc);
201 stripe_count = lsm->lsm_stripe_count;
205 OBD_FREE(lockhs, stripe_count * sizeof(*lockhs));
209 int ll_file_size(struct inode *inode, struct lov_stripe_md *lsm)
211 struct ll_sb_info *sbi = ll_i2sbi(inode);
212 struct lustre_handle *lockhs;
220 rc = ll_size_lock(inode, lsm, 0, LCK_PR, &lockhs);
221 if (rc != ELDLM_OK) {
222 CERROR("lock enqueue: %d\n", rc);
226 oa.o_id = lsm->lsm_object_id;
228 oa.o_valid = OBD_MD_FLID|OBD_MD_FLTYPE|OBD_MD_FLSIZE|OBD_MD_FLBLOCKS;
229 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lsm);
231 obdo_to_inode(inode, &oa,
232 oa.o_valid & ~(OBD_MD_FLTYPE | OBD_MD_FLMODE));
234 err = ll_size_unlock(inode, lsm, LCK_PR, lockhs);
235 if (err != ELDLM_OK) {
236 CERROR("lock cancel: %d\n", err);
242 static int ll_file_release(struct inode *inode, struct file *file)
244 struct ptlrpc_request *req = NULL;
245 struct ll_file_data *fd;
247 struct ll_sb_info *sbi = ll_i2sbi(inode);
248 struct ll_inode_info *lli = ll_i2info(inode);
249 struct lov_stripe_md *lsm = lli->lli_smd;
254 CHECK_MOUNT_EPOCH(inode);
256 fd = (struct ll_file_data *)file->private_data;
259 GOTO(out, rc = -EINVAL);
262 memset(&oa, 0, sizeof(oa));
263 oa.o_id = lsm->lsm_object_id;
265 oa.o_valid = OBD_MD_FLTYPE | OBD_MD_FLID;
266 obd_handle2oa(&oa, &fd->fd_osthandle);
267 rc = obd_close(ll_i2obdconn(inode), &oa, lsm);
269 GOTO(out_mdc, rc = -abs(rc));
271 /* If this fails and we goto out_fd, the file size on the MDS is out of
272 * date. Is that a big deal? */
273 if (file->f_mode & FMODE_WRITE) {
274 struct lustre_handle *lockhs;
276 rc = ll_size_lock(inode, lsm, 0, LCK_PR, &lockhs);
278 GOTO(out_mdc, -abs(rc));
280 oa.o_id = lsm->lsm_object_id;
282 oa.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
284 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lsm);
287 attr.ia_valid = (ATTR_MTIME | ATTR_CTIME | ATTR_ATIME |
289 attr.ia_mtime = inode->i_mtime;
290 attr.ia_ctime = inode->i_ctime;
291 attr.ia_atime = inode->i_atime;
292 attr.ia_size = oa.o_size;
294 inode->i_blocks = oa.o_blocks;
296 /* XXX: this introduces a small race that we should
298 rc = ll_inode_setattr(inode, &attr, 0);
300 rc2 = ll_size_unlock(inode, lli->lli_smd, LCK_PR, lockhs);
302 CERROR("lock cancel: %d\n", rc);
310 rc2 = mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
311 S_IFREG, &fd->fd_mdshandle, &req);
312 ptlrpc_req_finished(req);
318 /* XXX Mike, we have also done this in ll_file_open? */
319 ptlrpc_req_finished(fd->fd_req);
321 rc = obd_cancel_unused(ll_i2obdconn(inode), lsm, 0);
323 CERROR("obd_cancel_unused: %d\n", rc);
328 fd->fd_mdshandle.cookie = DEAD_HANDLE_MAGIC;
329 file->private_data = NULL;
330 kmem_cache_free(ll_file_data_slab, fd);
336 static inline void ll_remove_suid(struct inode *inode)
340 /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
341 mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
343 /* was any of the uid bits set? */
344 mode &= inode->i_mode;
345 if (mode && !capable(CAP_FSETID)) {
346 inode->i_mode &= ~mode;
347 // XXX careful here - we cannot change the size
351 static void ll_update_atime(struct inode *inode)
355 attr.ia_atime = CURRENT_TIME;
356 attr.ia_valid = ATTR_ATIME;
358 if (inode->i_atime == attr.ia_atime) return;
359 if (IS_RDONLY(inode)) return;
360 if (IS_NOATIME(inode)) return;
362 /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
363 ll_inode_setattr(inode, &attr, 0);
366 int ll_lock_callback(struct ldlm_lock *lock, struct ldlm_lock_desc *new,
367 void *data, __u32 data_len, int flag)
369 struct inode *inode = data;
370 struct lustre_handle lockh;
374 if (data_len != sizeof(struct inode))
381 case LDLM_CB_BLOCKING:
382 ldlm_lock2handle(lock, &lockh);
383 rc = ldlm_cli_cancel(&lockh);
385 CERROR("ldlm_cli_cancel failed: %d\n", rc);
387 case LDLM_CB_CANCELING:
388 CDEBUG(D_INODE, "invalidating obdo/inode %ld\n", inode->i_ino);
389 /* FIXME: do something better than throwing away everything */
390 //down(&inode->i_sem);
391 invalidate_inode_pages(inode);
401 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
404 struct ll_file_data *fd = (struct ll_file_data *)filp->private_data;
405 struct inode *inode = filp->f_dentry->d_inode;
406 struct ll_sb_info *sbi = ll_i2sbi(inode);
407 struct ldlm_extent extent;
408 struct lustre_handle *lockhs = NULL;
409 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
415 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
416 !(sbi->ll_flags & LL_SBI_NOLCK)) {
417 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
421 extent.start = *ppos;
422 extent.end = *ppos + count;
423 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
424 inode->i_ino, extent.start, extent.end);
426 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
427 &extent, sizeof(extent), LCK_PR, &flags,
428 ll_lock_callback, inode, sizeof(*inode),
430 if (err != ELDLM_OK) {
431 OBD_FREE(lockhs, lsm->lsm_stripe_count*sizeof(*lockhs));
432 CERROR("lock enqueue: err: %d\n", err);
437 CDEBUG(D_INFO, "Reading inode %ld, %d bytes, offset %Ld\n",
438 inode->i_ino, count, *ppos);
439 retval = generic_file_read(filp, buf, count, ppos);
442 ll_update_atime(inode);
444 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
445 !(sbi->ll_flags & LL_SBI_NOLCK)) {
446 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PR, lockhs);
447 if (err != ELDLM_OK) {
448 CERROR("lock cancel: err: %d\n", err);
454 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
459 * Write to a file (through the page cache).
462 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
464 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
465 struct inode *inode = file->f_dentry->d_inode;
466 struct ll_sb_info *sbi = ll_i2sbi(inode);
467 struct ldlm_extent extent;
468 struct lustre_handle *lockhs = NULL, *eof_lockhs = NULL;
469 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
475 if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
482 err = ll_size_lock(inode, lsm, 0, LCK_PW, &eof_lockhs);
488 oa->o_id = lsm->lsm_object_id;
489 oa->o_mode = inode->i_mode;
490 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
492 obd_handle2oa(oa, &fd->fd_osthandle);
493 retval = obd_getattr(&sbi->ll_osc_conn, oa, lsm);
496 GOTO(out_eof, retval);
500 obdo_to_inode(inode, oa, oa->o_valid);
504 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
505 sbi->ll_flags & LL_SBI_NOLCK) {
506 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
508 GOTO(out_eof, retval = -ENOMEM);
509 extent.start = *ppos;
510 extent.end = *ppos + count;
511 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
512 inode->i_ino, extent.start, extent.end);
514 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
515 &extent, sizeof(extent), LCK_PW, &flags,
516 ll_lock_callback, inode, sizeof(*inode),
518 if (err != ELDLM_OK) {
519 CERROR("lock enqueue: err: %d\n", err);
520 GOTO(out_free, retval = err);
524 CDEBUG(D_INFO, "Writing inode %ld, %ld bytes, offset "LPD64"\n",
525 inode->i_ino, (long)count, *ppos);
527 retval = generic_file_write(file, buf, count, ppos);
529 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
530 sbi->ll_flags & LL_SBI_NOLCK) {
531 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PW, lockhs);
532 if (err != ELDLM_OK) {
533 CERROR("lock cancel: err: %d\n", err);
534 GOTO(out_free, retval = err);
541 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
544 if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
545 err = ll_size_unlock(inode, lsm, LCK_PW, eof_lockhs);
553 static int ll_lov_setstripe(struct inode *inode, struct file *file,
554 struct lov_user_md *lum)
556 struct ll_inode_info *lli = ll_i2info(inode);
557 struct lov_stripe_md *lsm;
558 int size = ll_mds_easize(inode->i_sb);
561 rc = verify_area(VERIFY_READ, lum, sizeof(*lum));
565 down(&lli->lli_open_sem);
567 CERROR("striping data already set for %d\n", inode->i_ino);
568 GOTO(out_lov_up, rc = -EPERM);
571 OBD_ALLOC(lli->lli_smd, size);
573 GOTO(out_lov_up, rc = -ENOMEM);
576 lsm->lsm_magic = LOV_MAGIC;
577 lsm->lsm_stripe_size = lum->lum_stripe_size;
578 lsm->lsm_stripe_pattern = lum->lum_stripe_pattern;
579 lsm->lsm_stripe_offset = lum->lum_stripe_offset;
580 lsm->lsm_stripe_count = lum->lum_stripe_count;
581 lsm->lsm_mds_easize = size;
583 file->f_flags &= ~O_LOV_DELAY_CREATE;
584 rc = ll_create_objects(inode, lli);
586 OBD_FREE(lli->lli_smd, size);
588 rc = ll_file_open(inode, file);
590 up(&lli->lli_open_sem);
594 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
597 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
601 case LL_IOC_GETFLAGS:
602 /* Get the current value of the file flags */
603 return put_user(fd->fd_flags, (int *)arg);
604 case LL_IOC_SETFLAGS:
605 case LL_IOC_CLRFLAGS:
606 /* Set or clear specific file flags */
607 /* XXX This probably needs checks to ensure the flags are
608 * not abused, and to handle any flag side effects.
610 if (get_user(flags, (int *) arg))
613 if (cmd == LL_IOC_SETFLAGS)
614 fd->fd_flags |= flags;
616 fd->fd_flags &= ~flags;
618 case LL_IOC_LOV_SETSTRIPE:
619 return ll_lov_setstripe(inode, file, (struct lov_user_md *)arg);
621 /* We need to special case any other ioctls we want to handle,
622 * to send them to the MDS/OST as appropriate and to properly
623 * network encode the arg field.
624 case EXT2_IOC_GETFLAGS:
625 case EXT2_IOC_SETFLAGS:
626 case EXT2_IOC_GETVERSION_OLD:
627 case EXT2_IOC_GETVERSION_NEW:
628 case EXT2_IOC_SETVERSION_OLD:
629 case EXT2_IOC_SETVERSION_NEW:
636 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
638 struct inode *inode = file->f_dentry->d_inode;
642 CHECK_MOUNT_EPOCH(inode);
646 struct ll_inode_info *lli = ll_i2info(inode);
648 retval = ll_file_size(inode, lli->lli_smd);
652 offset += inode->i_size;
656 offset += file->f_pos;
659 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
660 if (offset != file->f_pos) {
661 file->f_pos = offset;
663 file->f_version = ++event;
670 /* XXX this does not need to do anything for data, it _does_ need to
672 int ll_fsync(struct file *file, struct dentry *dentry, int data)
677 static int ll_inode_revalidate(struct dentry *dentry)
679 struct inode *inode = dentry->d_inode;
680 struct lov_stripe_md *lsm;
686 lsm = ll_i2info(inode)->lli_smd;
687 if (!lsm) /* object not yet allocated, don't validate size */
690 RETURN(ll_file_size(inode, lsm));
693 struct file_operations ll_file_operations = {
695 write: ll_file_write,
696 ioctl: ll_file_ioctl,
698 release: ll_file_release,
699 mmap: generic_file_mmap,
700 llseek: ll_file_seek,
704 struct inode_operations ll_file_inode_operations = {
705 truncate: ll_truncate,
707 revalidate: ll_inode_revalidate