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);
74 CHECK_MOUNT_EPOCH(inode);
78 /* delayed create of object (intent created inode) */
79 /* XXX object needs to be cleaned up if mdc_open fails */
80 /* XXX error handling appropriate here? */
82 if (file->f_flags & O_LOV_DELAY_CREATE) {
83 CDEBUG(D_INODE, "delaying object creation\n");
86 down(&lli->lli_open_sem);
87 /* Check to see if we lost the race */
89 rc = ll_create_objects(inode->i_sb, inode->i_ino, 0, 0,
91 up(&lli->lli_open_sem);
98 fd = kmem_cache_alloc(ll_file_data_slab, SLAB_KERNEL);
100 GOTO(out, rc = -ENOMEM);
101 memset(fd, 0, sizeof(*fd));
103 fd->fd_mdshandle.addr = (__u64)(unsigned long)file;
104 get_random_bytes(&fd->fd_mdshandle.cookie,
105 sizeof(fd->fd_mdshandle.cookie));
106 rc = mdc_open(&sbi->ll_mdc_conn, inode->i_ino, S_IFREG | inode->i_mode,
107 file->f_flags, lsm, &fd->fd_mdshandle, &req);
110 /* We don't call ptlrpc_req_finished here, because the request is
111 * preserved until we see a matching close, at which point it is
112 * released (and likely freed). (See ll_file_release.)
115 GOTO(out_req, -abs(rc));
116 if (!fd->fd_mdshandle.addr ||
117 fd->fd_mdshandle.addr == (__u64)(unsigned long)file) {
118 CERROR("hmm, mdc_open didn't assign fd_mdshandle?\n");
119 /* XXX handle this how, abort or is it non-fatal? */
124 GOTO(out_mdc, rc = -EINVAL);
126 oa->o_id = lsm->lsm_object_id;
127 oa->o_mode = S_IFREG;
128 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
130 rc = obd_open(ll_i2obdconn(inode), oa, lsm);
131 obdo_to_inode(inode, oa, oa->o_valid & (OBD_MD_FLSIZE|OBD_MD_FLBLOCKS));
133 obd_oa2handle(&fd->fd_osthandle, oa);
137 GOTO(out_mdc, rc = -abs(rc));
139 file->private_data = fd;
143 mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
144 S_IFREG, &fd->fd_mdshandle, &req);
146 ptlrpc_free_req(req);
148 fd->fd_mdshandle.cookie = DEAD_HANDLE_MAGIC;
149 kmem_cache_free(ll_file_data_slab, fd);
154 int ll_size_lock(struct inode *inode, struct lov_stripe_md *lsm, obd_off start,
155 int mode, struct lustre_handle **lockhs_p)
157 struct ll_sb_info *sbi = ll_i2sbi(inode);
158 struct ldlm_extent extent;
159 struct lustre_handle *lockhs = NULL;
160 int rc, flags = 0, stripe_count;
163 if (sbi->ll_flags & LL_SBI_NOLCK) {
168 stripe_count = lsm->lsm_stripe_count;
172 OBD_ALLOC(lockhs, stripe_count * sizeof(*lockhs));
176 extent.start = start;
177 extent.end = OBD_OBJECT_EOF;
179 rc = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT, &extent,
180 sizeof(extent), mode, &flags, ll_lock_callback,
181 inode, sizeof(*inode), lockhs);
182 if (rc != ELDLM_OK) {
183 CERROR("lock enqueue: %d\n", rc);
184 OBD_FREE(lockhs, stripe_count * sizeof(*lockhs));
190 int ll_size_unlock(struct inode *inode, struct lov_stripe_md *lsm, int mode,
191 struct lustre_handle *lockhs)
193 struct ll_sb_info *sbi = ll_i2sbi(inode);
194 int rc, stripe_count;
197 if (sbi->ll_flags & LL_SBI_NOLCK)
200 if (lockhs == NULL) {
205 rc = obd_cancel(&sbi->ll_osc_conn, lsm, mode, lockhs);
206 if (rc != ELDLM_OK) {
207 CERROR("lock cancel: %d\n", rc);
211 stripe_count = lsm->lsm_stripe_count;
215 OBD_FREE(lockhs, stripe_count * sizeof(*lockhs));
219 int ll_file_size(struct inode *inode, struct lov_stripe_md *lsm)
221 struct ll_sb_info *sbi = ll_i2sbi(inode);
222 struct lustre_handle *lockhs;
230 rc = ll_size_lock(inode, lsm, 0, LCK_PR, &lockhs);
231 if (rc != ELDLM_OK) {
232 CERROR("lock enqueue: %d\n", rc);
236 oa.o_id = lsm->lsm_object_id;
238 oa.o_valid = OBD_MD_FLID|OBD_MD_FLTYPE|OBD_MD_FLSIZE|OBD_MD_FLBLOCKS;
239 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lsm);
241 obdo_to_inode(inode, &oa,
242 oa.o_valid & ~(OBD_MD_FLTYPE | OBD_MD_FLMODE));
244 err = ll_size_unlock(inode, lsm, LCK_PR, lockhs);
245 if (err != ELDLM_OK) {
246 CERROR("lock cancel: %d\n", err);
252 static int ll_file_release(struct inode *inode, struct file *file)
254 struct ptlrpc_request *req = NULL;
255 struct ll_file_data *fd;
257 struct ll_sb_info *sbi = ll_i2sbi(inode);
258 struct ll_inode_info *lli = ll_i2info(inode);
259 struct lov_stripe_md *lsm = lli->lli_smd;
264 CHECK_MOUNT_EPOCH(inode);
266 fd = (struct ll_file_data *)file->private_data;
269 GOTO(out, rc = -EINVAL);
272 memset(&oa, 0, sizeof(oa));
273 oa.o_id = lsm->lsm_object_id;
275 oa.o_valid = OBD_MD_FLTYPE | OBD_MD_FLID;
276 obd_handle2oa(&oa, &fd->fd_osthandle);
277 rc = obd_close(ll_i2obdconn(inode), &oa, lsm);
279 GOTO(out_mdc, rc = -abs(rc));
281 /* If this fails and we goto out_fd, the file size on the MDS is out of
282 * date. Is that a big deal? */
283 if (file->f_mode & FMODE_WRITE) {
284 struct lustre_handle *lockhs;
286 rc = ll_size_lock(inode, lsm, 0, LCK_PR, &lockhs);
288 GOTO(out_mdc, -abs(rc));
290 oa.o_id = lsm->lsm_object_id;
292 oa.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
294 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lsm);
297 attr.ia_valid = (ATTR_MTIME | ATTR_CTIME | ATTR_ATIME |
299 attr.ia_mtime = inode->i_mtime;
300 attr.ia_ctime = inode->i_ctime;
301 attr.ia_atime = inode->i_atime;
302 attr.ia_size = oa.o_size;
304 inode->i_blocks = oa.o_blocks;
306 /* XXX: this introduces a small race that we should
308 rc = ll_inode_setattr(inode, &attr, 0);
310 rc2 = ll_size_unlock(inode, lli->lli_smd, LCK_PR, lockhs);
312 CERROR("lock cancel: %d\n", rc);
320 rc2 = mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
321 S_IFREG, &fd->fd_mdshandle, &req);
322 ptlrpc_req_finished(req);
328 CDEBUG(D_HA, "matched req %p xid "LPD64" transno "LPD64" op %d->%s:%d\n",
329 fd->fd_req, fd->fd_req->rq_xid, fd->fd_req->rq_repmsg->transno,
330 fd->fd_req->rq_reqmsg->opc,
331 fd->fd_req->rq_import->imp_connection->c_remote_uuid,
332 fd->fd_req->rq_import->imp_client->cli_request_portal);
333 ptlrpc_req_finished(fd->fd_req);
335 rc = obd_cancel_unused(ll_i2obdconn(inode), lsm, 0);
337 CERROR("obd_cancel_unused: %d\n", rc);
342 fd->fd_mdshandle.cookie = DEAD_HANDLE_MAGIC;
343 file->private_data = NULL;
344 kmem_cache_free(ll_file_data_slab, fd);
350 static inline void ll_remove_suid(struct inode *inode)
354 /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
355 mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
357 /* was any of the uid bits set? */
358 mode &= inode->i_mode;
359 if (mode && !capable(CAP_FSETID)) {
360 inode->i_mode &= ~mode;
361 // XXX careful here - we cannot change the size
365 static void ll_update_atime(struct inode *inode)
369 attr.ia_atime = CURRENT_TIME;
370 attr.ia_valid = ATTR_ATIME;
372 if (inode->i_atime == attr.ia_atime) return;
373 if (IS_RDONLY(inode)) return;
374 if (IS_NOATIME(inode)) return;
376 /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
377 ll_inode_setattr(inode, &attr, 0);
380 int ll_lock_callback(struct ldlm_lock *lock, struct ldlm_lock_desc *new,
381 void *data, __u32 data_len, int flag)
383 struct inode *inode = data;
384 struct lustre_handle lockh;
388 if (data_len != sizeof(struct inode))
395 case LDLM_CB_BLOCKING:
396 ldlm_lock2handle(lock, &lockh);
397 rc = ldlm_cli_cancel(&lockh);
399 CERROR("ldlm_cli_cancel failed: %d\n", rc);
401 case LDLM_CB_CANCELING:
402 CDEBUG(D_INODE, "invalidating obdo/inode %ld\n", inode->i_ino);
403 /* FIXME: do something better than throwing away everything */
404 //down(&inode->i_sem);
405 invalidate_inode_pages(inode);
415 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
418 struct ll_file_data *fd = (struct ll_file_data *)filp->private_data;
419 struct inode *inode = filp->f_dentry->d_inode;
420 struct ll_sb_info *sbi = ll_i2sbi(inode);
421 struct ldlm_extent extent;
422 struct lustre_handle *lockhs = NULL;
423 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
429 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
430 !(sbi->ll_flags & LL_SBI_NOLCK)) {
431 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
435 extent.start = *ppos;
436 extent.end = *ppos + count;
437 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
438 inode->i_ino, extent.start, extent.end);
440 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
441 &extent, sizeof(extent), LCK_PR, &flags,
442 ll_lock_callback, inode, sizeof(*inode),
444 if (err != ELDLM_OK) {
445 OBD_FREE(lockhs, lsm->lsm_stripe_count*sizeof(*lockhs));
446 CERROR("lock enqueue: err: %d\n", err);
451 CDEBUG(D_INFO, "Reading inode %ld, %d bytes, offset %Ld\n",
452 inode->i_ino, count, *ppos);
453 retval = generic_file_read(filp, buf, count, ppos);
456 ll_update_atime(inode);
458 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
459 !(sbi->ll_flags & LL_SBI_NOLCK)) {
460 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PR, lockhs);
461 if (err != ELDLM_OK) {
462 CERROR("lock cancel: err: %d\n", err);
468 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
473 * Write to a file (through the page cache).
476 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
478 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
479 struct inode *inode = file->f_dentry->d_inode;
480 struct ll_sb_info *sbi = ll_i2sbi(inode);
481 struct ldlm_extent extent;
482 struct lustre_handle *lockhs = NULL, *eof_lockhs = NULL;
483 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
489 if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
496 err = ll_size_lock(inode, lsm, 0, LCK_PW, &eof_lockhs);
502 oa->o_id = lsm->lsm_object_id;
503 oa->o_mode = inode->i_mode;
504 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
506 obd_handle2oa(oa, &fd->fd_osthandle);
507 retval = obd_getattr(&sbi->ll_osc_conn, oa, lsm);
510 GOTO(out_eof, retval);
514 obdo_to_inode(inode, oa, oa->o_valid);
518 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) &&
519 !(sbi->ll_flags & LL_SBI_NOLCK)) {
520 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
522 GOTO(out_eof, retval = -ENOMEM);
523 extent.start = *ppos;
524 extent.end = *ppos + count;
525 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
526 inode->i_ino, extent.start, extent.end);
528 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
529 &extent, sizeof(extent), LCK_PW, &flags,
530 ll_lock_callback, inode, sizeof(*inode),
532 if (err != ELDLM_OK) {
533 CERROR("lock enqueue: err: %d\n", err);
534 GOTO(out_free, retval = err);
538 CDEBUG(D_INFO, "Writing inode %ld, %ld bytes, offset "LPD64"\n",
539 inode->i_ino, (long)count, *ppos);
541 retval = generic_file_write(file, buf, count, ppos);
543 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
544 sbi->ll_flags & LL_SBI_NOLCK) {
545 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PW, lockhs);
546 if (err != ELDLM_OK) {
547 CERROR("lock cancel: err: %d\n", err);
548 GOTO(out_free, retval = err);
555 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
558 if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
559 err = ll_size_unlock(inode, lsm, LCK_PW, eof_lockhs);
567 static int ll_lov_setstripe(struct inode *inode, struct file *file,
568 struct lov_user_md *lum)
570 struct ll_inode_info *lli = ll_i2info(inode);
571 struct lov_stripe_md *lsm;
572 int size = ll_mds_easize(inode->i_sb);
575 rc = verify_area(VERIFY_READ, lum, sizeof(*lum));
579 down(&lli->lli_open_sem);
581 CERROR("striping data already set for %d\n", inode->i_ino);
582 GOTO(out_lov_up, rc = -EPERM);
585 OBD_ALLOC(lli->lli_smd, size);
587 GOTO(out_lov_up, rc = -ENOMEM);
590 lsm->lsm_magic = LOV_MAGIC;
591 lsm->lsm_stripe_size = lum->lum_stripe_size;
592 lsm->lsm_stripe_pattern = lum->lum_stripe_pattern;
593 lsm->lsm_stripe_offset = lum->lum_stripe_offset;
594 lsm->lsm_stripe_count = lum->lum_stripe_count;
595 lsm->lsm_mds_easize = size;
597 file->f_flags &= ~O_LOV_DELAY_CREATE;
598 rc = ll_create_objects(inode->i_sb, inode->i_ino, 0, 0, &lsm);
600 OBD_FREE(lli->lli_smd, size);
602 rc = ll_file_open(inode, file);
604 up(&lli->lli_open_sem);
608 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
611 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
615 case LL_IOC_GETFLAGS:
616 /* Get the current value of the file flags */
617 return put_user(fd->fd_flags, (int *)arg);
618 case LL_IOC_SETFLAGS:
619 case LL_IOC_CLRFLAGS:
620 /* Set or clear specific file flags */
621 /* XXX This probably needs checks to ensure the flags are
622 * not abused, and to handle any flag side effects.
624 if (get_user(flags, (int *) arg))
627 if (cmd == LL_IOC_SETFLAGS)
628 fd->fd_flags |= flags;
630 fd->fd_flags &= ~flags;
632 case LL_IOC_LOV_SETSTRIPE:
633 return ll_lov_setstripe(inode, file, (struct lov_user_md *)arg);
635 /* We need to special case any other ioctls we want to handle,
636 * to send them to the MDS/OST as appropriate and to properly
637 * network encode the arg field.
638 case EXT2_IOC_GETFLAGS:
639 case EXT2_IOC_SETFLAGS:
640 case EXT2_IOC_GETVERSION_OLD:
641 case EXT2_IOC_GETVERSION_NEW:
642 case EXT2_IOC_SETVERSION_OLD:
643 case EXT2_IOC_SETVERSION_NEW:
650 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
652 struct inode *inode = file->f_dentry->d_inode;
656 CHECK_MOUNT_EPOCH(inode);
660 struct ll_inode_info *lli = ll_i2info(inode);
662 retval = ll_file_size(inode, lli->lli_smd);
666 offset += inode->i_size;
670 offset += file->f_pos;
673 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
674 if (offset != file->f_pos) {
675 file->f_pos = offset;
677 file->f_version = ++event;
684 /* XXX this does not need to do anything for data, it _does_ need to
686 int ll_fsync(struct file *file, struct dentry *dentry, int data)
691 static int ll_inode_revalidate(struct dentry *dentry)
693 struct inode *inode = dentry->d_inode;
694 struct lov_stripe_md *lsm;
700 lsm = ll_i2info(inode)->lli_smd;
701 if (!lsm) /* object not yet allocated, don't validate size */
704 RETURN(ll_file_size(inode, lsm));
707 struct file_operations ll_file_operations = {
709 write: ll_file_write,
710 ioctl: ll_file_ioctl,
712 release: ll_file_release,
713 mmap: generic_file_mmap,
714 llseek: ll_file_seek,
718 struct inode_operations ll_file_inode_operations = {
719 truncate: ll_truncate,
721 revalidate: ll_inode_revalidate