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_file_open(struct inode *inode, struct file *file)
37 struct ptlrpc_request *req = NULL;
38 struct ll_file_data *fd;
39 struct obdo *oa = NULL;
40 struct lov_stripe_md *lsm = NULL;
41 struct ll_sb_info *sbi = ll_i2sbi(inode);
42 struct ll_inode_info *lli = ll_i2info(inode);
46 LASSERT(!file->private_data);
48 CHECK_MOUNT_EPOCH(inode);
52 /* delayed create of object (intent created inode) */
53 /* XXX object needs to be cleaned up if mdc_open fails */
54 /* XXX error handling appropriate here? */
56 struct inode *inode = file->f_dentry->d_inode;
58 down(&lli->lli_open_sem);
59 /* Check to see if we lost the race */
60 if (lli->lli_smd == NULL) {
63 up(&lli->lli_open_sem);
66 oa->o_mode = S_IFREG | 0600;
67 oa->o_easize = ll_mds_easize(inode->i_sb);
68 oa->o_id = inode->i_ino;
69 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
70 OBD_MD_FLMODE | OBD_MD_FLEASIZE;
71 rc = obd_create(ll_i2obdconn(inode), oa, &lli->lli_smd);
75 up(&lli->lli_open_sem);
79 if (lli->lli_smd->lsm_object_id == 0)
81 up(&lli->lli_open_sem);
85 fd = kmem_cache_alloc(ll_file_data_slab, SLAB_KERNEL);
87 GOTO(out, rc = -ENOMEM);
88 memset(fd, 0, sizeof(*fd));
90 fd->fd_mdshandle.addr = (__u64)(unsigned long)file;
91 get_random_bytes(&fd->fd_mdshandle.cookie,
92 sizeof(fd->fd_mdshandle.cookie));
93 rc = mdc_open(&sbi->ll_mdc_conn, inode->i_ino, S_IFREG | inode->i_mode,
94 file->f_flags, lsm, &fd->fd_mdshandle, &req);
96 ptlrpc_req_finished(req);
98 GOTO(out_req, -abs(rc));
99 if (!fd->fd_mdshandle.addr ||
100 fd->fd_mdshandle.addr == (__u64)(unsigned long)file) {
101 CERROR("hmm, mdc_open didn't assign fd_mdshandle?\n");
102 /* XXX handle this how, abort or is it non-fatal? */
108 GOTO(out_mdc, rc = -EINVAL);
110 oa->o_id = lsm->lsm_object_id;
111 oa->o_mode = S_IFREG;
112 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE;
113 rc = obd_open(ll_i2obdconn(inode), oa, lsm);
114 obd_oa2handle(&fd->fd_osthandle, oa);
117 GOTO(out_mdc, rc = -abs(rc));
121 file->private_data = fd;
127 mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
128 S_IFREG, &fd->fd_mdshandle, &req);
130 ptlrpc_free_req(req);
133 kmem_cache_free(ll_file_data_slab, fd);
134 file->private_data = NULL;
139 int ll_size_lock(struct inode *inode, struct lov_stripe_md *lsm, __u64 start,
140 int mode, struct lustre_handle **lockhs_p)
142 struct ll_sb_info *sbi = ll_i2sbi(inode);
143 struct ldlm_extent extent;
144 struct lustre_handle *lockhs = NULL;
145 int rc, flags = 0, stripe_count;
148 if (sbi->ll_flags & LL_SBI_NOLCK) {
153 stripe_count = lsm->lsm_stripe_count;
157 OBD_ALLOC(lockhs, stripe_count * sizeof(*lockhs));
161 extent.start = start;
164 rc = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT, &extent,
165 sizeof(extent), mode, &flags, ll_lock_callback,
166 inode, sizeof(*inode), lockhs);
167 if (rc != ELDLM_OK) {
168 CERROR("lock enqueue: %d\n", rc);
169 OBD_FREE(lockhs, stripe_count * sizeof(*lockhs));
175 int ll_size_unlock(struct inode *inode, struct lov_stripe_md *lsm, int mode,
176 struct lustre_handle *lockhs)
178 struct ll_sb_info *sbi = ll_i2sbi(inode);
179 int rc, stripe_count;
182 if (sbi->ll_flags & LL_SBI_NOLCK)
185 if (lockhs == NULL) {
190 rc = obd_cancel(&sbi->ll_osc_conn, lsm, mode, lockhs);
191 if (rc != ELDLM_OK) {
192 CERROR("lock cancel: %d\n", rc);
196 stripe_count = lsm->lsm_stripe_count;
200 OBD_FREE(lockhs, stripe_count * sizeof(*lockhs));
204 int ll_file_size(struct inode *inode, struct lov_stripe_md *lsm)
206 struct ll_sb_info *sbi = ll_i2sbi(inode);
207 struct lustre_handle *lockhs;
215 rc = ll_size_lock(inode, lsm, 0, LCK_PR, &lockhs);
216 if (rc != ELDLM_OK) {
217 CERROR("lock enqueue: %d\n", rc);
221 oa.o_id = lsm->lsm_object_id;
223 oa.o_valid = OBD_MD_FLID|OBD_MD_FLTYPE|OBD_MD_FLSIZE|OBD_MD_FLBLOCKS;
224 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lsm);
226 obdo_to_inode(inode, &oa,
227 oa.o_valid & ~(OBD_MD_FLTYPE | OBD_MD_FLMODE));
229 err = ll_size_unlock(inode, lsm, LCK_PR, lockhs);
230 if (err != ELDLM_OK) {
231 CERROR("lock cancel: %d\n", err);
237 static int ll_file_release(struct inode *inode, struct file *file)
240 struct ptlrpc_request *req = NULL;
241 struct ll_file_data *fd;
243 struct ll_sb_info *sbi = ll_i2sbi(inode);
244 struct ll_inode_info *lli = ll_i2info(inode);
248 CHECK_MOUNT_EPOCH(inode);
250 fd = (struct ll_file_data *)file->private_data;
253 GOTO(out, rc = -EINVAL);
256 memset(&oa, 0, sizeof(oa));
257 oa.o_id = lli->lli_smd->lsm_object_id;
259 oa.o_valid = OBD_MD_FLTYPE | OBD_MD_FLID;
260 obd_handle2oa(&oa, &fd->fd_osthandle);
261 rc = obd_close(ll_i2obdconn(inode), &oa, lli->lli_smd);
263 GOTO(out_fd, abs(rc));
265 /* If this fails and we goto out_fd, the file size on the MDS is out of
266 * date. Is that a big deal? */
267 if (file->f_mode & FMODE_WRITE) {
268 struct lustre_handle *lockhs;
270 rc = ll_size_lock(inode, lli->lli_smd, 0, LCK_PR, &lockhs);
272 GOTO(out_fd, abs(rc));
274 oa.o_id = lli->lli_smd->lsm_object_id;
276 oa.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
278 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lli->lli_smd);
281 attr.ia_valid = (ATTR_MTIME | ATTR_CTIME | ATTR_ATIME |
283 attr.ia_mtime = inode->i_mtime;
284 attr.ia_ctime = inode->i_ctime;
285 attr.ia_atime = inode->i_atime;
286 attr.ia_size = oa.o_size;
288 inode->i_blocks = oa.o_blocks;
290 /* XXX: this introduces a small race that we should
292 rc = ll_inode_setattr(inode, &attr, 0);
294 CERROR("failed - %d.\n", rc);
295 rc = -EIO; /* XXX - GOTO(out)? -phil */
298 rc = ll_size_unlock(inode, lli->lli_smd, LCK_PR, lockhs);
300 CERROR("lock cancel: %d\n", rc);
305 rc = mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
306 S_IFREG, &fd->fd_mdshandle, &req);
307 ptlrpc_req_finished(req);
313 ptlrpc_req_finished(fd->fd_req);
315 rc = obd_cancel_unused(ll_i2obdconn(inode), lli->lli_smd, 0);
317 CERROR("obd_cancel_unused: %d\n", rc);
322 kmem_cache_free(ll_file_data_slab, fd);
323 file->private_data = NULL;
329 static inline void ll_remove_suid(struct inode *inode)
333 /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
334 mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
336 /* was any of the uid bits set? */
337 mode &= inode->i_mode;
338 if (mode && !capable(CAP_FSETID)) {
339 inode->i_mode &= ~mode;
340 // XXX careful here - we cannot change the size
344 static void ll_update_atime(struct inode *inode)
348 attr.ia_atime = CURRENT_TIME;
349 attr.ia_valid = ATTR_ATIME;
351 if (inode->i_atime == attr.ia_atime) return;
352 if (IS_RDONLY(inode)) return;
353 if (IS_NOATIME(inode)) return;
355 /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
356 ll_inode_setattr(inode, &attr, 0);
359 int ll_lock_callback(struct ldlm_lock *lock, struct ldlm_lock_desc *new,
360 void *data, __u32 data_len, int flag)
362 struct inode *inode = data;
363 struct lustre_handle lockh;
367 if (data_len != sizeof(struct inode))
374 case LDLM_CB_BLOCKING:
375 ldlm_lock2handle(lock, &lockh);
376 rc = ldlm_cli_cancel(&lockh);
378 CERROR("ldlm_cli_cancel failed: %d\n", rc);
380 case LDLM_CB_CANCELING:
381 CDEBUG(D_INODE, "invalidating obdo/inode %ld\n", inode->i_ino);
382 /* FIXME: do something better than throwing away everything */
383 //down(&inode->i_sem);
384 invalidate_inode_pages(inode);
394 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
397 struct ll_file_data *fd = (struct ll_file_data *)filp->private_data;
398 struct inode *inode = filp->f_dentry->d_inode;
399 struct ll_sb_info *sbi = ll_i2sbi(inode);
400 struct ldlm_extent extent;
401 struct lustre_handle *lockhs = NULL;
402 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
408 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
409 sbi->ll_flags & LL_SBI_NOLCK) {
410 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
414 extent.start = *ppos;
415 extent.end = *ppos + count;
416 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
417 inode->i_ino, extent.start, extent.end);
419 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
420 &extent, sizeof(extent), LCK_PR, &flags,
421 ll_lock_callback, inode, sizeof(*inode),
423 if (err != ELDLM_OK) {
424 OBD_FREE(lockhs, lsm->lsm_stripe_count*sizeof(*lockhs));
425 CERROR("lock enqueue: err: %d\n", err);
430 CDEBUG(D_INFO, "Reading inode %ld, %d bytes, offset %Ld\n",
431 inode->i_ino, count, *ppos);
432 retval = generic_file_read(filp, buf, count, ppos);
435 ll_update_atime(inode);
437 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
438 sbi->ll_flags & LL_SBI_NOLCK) {
439 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PR, lockhs);
440 if (err != ELDLM_OK) {
441 CERROR("lock cancel: err: %d\n", err);
447 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
452 * Write to a file (through the page cache).
455 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
457 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
458 struct inode *inode = file->f_dentry->d_inode;
459 struct ll_sb_info *sbi = ll_i2sbi(inode);
460 struct ldlm_extent extent;
461 struct lustre_handle *lockhs = NULL, *eof_lockhs = NULL;
462 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
468 if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
475 err = ll_size_lock(inode, lsm, 0, LCK_PW, &eof_lockhs);
481 oa->o_id = lsm->lsm_object_id;
482 oa->o_mode = inode->i_mode;
483 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
485 obd_handle2oa(oa, &fd->fd_osthandle);
486 retval = obd_getattr(&sbi->ll_osc_conn, oa, lsm);
489 GOTO(out_eof, retval);
493 obdo_to_inode(inode, oa, oa->o_valid);
497 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
498 sbi->ll_flags & LL_SBI_NOLCK) {
499 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
501 GOTO(out_eof, retval = -ENOMEM);
502 extent.start = *ppos;
503 extent.end = *ppos + count;
504 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
505 inode->i_ino, extent.start, extent.end);
507 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
508 &extent, sizeof(extent), LCK_PW, &flags,
509 ll_lock_callback, inode, sizeof(*inode),
511 if (err != ELDLM_OK) {
512 CERROR("lock enqueue: err: %d\n", err);
513 GOTO(out_free, retval = err);
517 CDEBUG(D_INFO, "Writing inode %ld, %ld bytes, offset "LPD64"\n",
518 inode->i_ino, (long)count, *ppos);
520 retval = generic_file_write(file, buf, count, ppos);
522 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
523 sbi->ll_flags & LL_SBI_NOLCK) {
524 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PW, lockhs);
525 if (err != ELDLM_OK) {
526 CERROR("lock cancel: err: %d\n", err);
527 GOTO(out_free, retval = err);
534 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
537 if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
538 err = ll_size_unlock(inode, lsm, LCK_PW, eof_lockhs);
546 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
549 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
553 case LL_IOC_GETFLAGS:
554 /* Get the current value of the file flags */
555 return put_user(fd->fd_flags, (int *)arg);
556 case LL_IOC_SETFLAGS:
557 case LL_IOC_CLRFLAGS:
558 /* Set or clear specific file flags */
559 /* XXX This probably needs checks to ensure the flags are
560 * not abused, and to handle any flag side effects.
562 if (get_user(flags, (int *) arg))
565 if (cmd == LL_IOC_SETFLAGS)
566 fd->fd_flags |= flags;
568 fd->fd_flags &= ~flags;
571 /* We need to special case any other ioctls we want to handle,
572 * to send them to the MDS/OST as appropriate and to properly
573 * network encode the arg field.
574 case EXT2_IOC_GETFLAGS:
575 case EXT2_IOC_SETFLAGS:
576 case EXT2_IOC_GETVERSION_OLD:
577 case EXT2_IOC_GETVERSION_NEW:
578 case EXT2_IOC_SETVERSION_OLD:
579 case EXT2_IOC_SETVERSION_NEW:
586 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
588 struct inode *inode = file->f_dentry->d_inode;
592 CHECK_MOUNT_EPOCH(inode);
596 struct ll_inode_info *lli = ll_i2info(inode);
598 retval = ll_file_size(inode, lli->lli_smd);
602 offset += inode->i_size;
606 offset += file->f_pos;
609 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
610 if (offset != file->f_pos) {
611 file->f_pos = offset;
613 file->f_version = ++event;
620 /* XXX this does not need to do anything for data, it _does_ need to
622 int ll_fsync(struct file *file, struct dentry *dentry, int data)
627 static int ll_inode_revalidate(struct dentry *dentry)
629 struct inode *inode = dentry->d_inode;
635 RETURN(ll_file_size(inode, ll_i2info(inode)->lli_smd));
638 struct file_operations ll_file_operations = {
640 write: ll_file_write,
641 ioctl: ll_file_ioctl,
643 release: ll_file_release,
644 mmap: generic_file_mmap,
645 llseek: ll_file_seek,
649 struct inode_operations ll_file_inode_operations = {
650 truncate: ll_truncate,
652 revalidate: ll_inode_revalidate