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;
125 mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
126 S_IFREG, &fd->fd_mdshandle, &req);
128 ptlrpc_free_req(req);
131 kmem_cache_free(ll_file_data_slab, fd);
132 file->private_data = NULL;
137 int ll_size_lock(struct inode *inode, struct lov_stripe_md *lsm, __u64 start,
138 int mode, struct lustre_handle **lockhs_p)
140 struct ll_sb_info *sbi = ll_i2sbi(inode);
141 struct ldlm_extent extent;
142 struct lustre_handle *lockhs = NULL;
143 int rc, flags = 0, stripe_count;
146 if (sbi->ll_flags & LL_SBI_NOLCK) {
151 stripe_count = lsm->lsm_stripe_count;
155 OBD_ALLOC(lockhs, stripe_count * sizeof(*lockhs));
159 extent.start = start;
162 rc = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT, &extent,
163 sizeof(extent), mode, &flags, ll_lock_callback,
164 inode, sizeof(*inode), lockhs);
165 if (rc != ELDLM_OK) {
166 CERROR("lock enqueue: %d\n", rc);
167 OBD_FREE(lockhs, stripe_count * sizeof(*lockhs));
173 int ll_size_unlock(struct inode *inode, struct lov_stripe_md *lsm, int mode,
174 struct lustre_handle *lockhs)
176 struct ll_sb_info *sbi = ll_i2sbi(inode);
177 int rc, stripe_count;
180 if (sbi->ll_flags & LL_SBI_NOLCK)
183 if (lockhs == NULL) {
188 rc = obd_cancel(&sbi->ll_osc_conn, lsm, mode, lockhs);
189 if (rc != ELDLM_OK) {
190 CERROR("lock cancel: %d\n", rc);
194 stripe_count = lsm->lsm_stripe_count;
198 OBD_FREE(lockhs, stripe_count * sizeof(*lockhs));
202 int ll_file_size(struct inode *inode, struct lov_stripe_md *lsm)
204 struct ll_sb_info *sbi = ll_i2sbi(inode);
205 struct lustre_handle *lockhs;
213 rc = ll_size_lock(inode, lsm, 0, LCK_PR, &lockhs);
214 if (rc != ELDLM_OK) {
215 CERROR("lock enqueue: %d\n", rc);
219 oa.o_id = lsm->lsm_object_id;
221 oa.o_valid = OBD_MD_FLID|OBD_MD_FLTYPE|OBD_MD_FLSIZE|OBD_MD_FLBLOCKS;
222 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lsm);
224 obdo_to_inode(inode, &oa,
225 oa.o_valid & ~(OBD_MD_FLTYPE | OBD_MD_FLMODE));
227 err = ll_size_unlock(inode, lsm, LCK_PR, lockhs);
228 if (err != ELDLM_OK) {
229 CERROR("lock cancel: %d\n", err);
235 static int ll_file_release(struct inode *inode, struct file *file)
238 struct ptlrpc_request *req = NULL;
239 struct ll_file_data *fd;
241 struct ll_sb_info *sbi = ll_i2sbi(inode);
242 struct ll_inode_info *lli = ll_i2info(inode);
246 CHECK_MOUNT_EPOCH(inode);
248 fd = (struct ll_file_data *)file->private_data;
251 GOTO(out, rc = -EINVAL);
254 memset(&oa, 0, sizeof(oa));
255 oa.o_id = lli->lli_smd->lsm_object_id;
257 oa.o_valid = OBD_MD_FLTYPE | OBD_MD_FLID;
258 obd_handle2oa(&oa, &fd->fd_osthandle);
259 rc = obd_close(ll_i2obdconn(inode), &oa, lli->lli_smd);
261 GOTO(out_fd, abs(rc));
263 /* If this fails and we goto out_fd, the file size on the MDS is out of
264 * date. Is that a big deal? */
265 if (file->f_mode & FMODE_WRITE) {
266 struct lustre_handle *lockhs;
268 rc = ll_size_lock(inode, lli->lli_smd, 0, LCK_PR, &lockhs);
270 GOTO(out_fd, abs(rc));
272 oa.o_id = lli->lli_smd->lsm_object_id;
274 oa.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
276 rc = obd_getattr(&sbi->ll_osc_conn, &oa, lli->lli_smd);
279 attr.ia_valid = (ATTR_MTIME | ATTR_CTIME | ATTR_ATIME |
281 attr.ia_mtime = inode->i_mtime;
282 attr.ia_ctime = inode->i_ctime;
283 attr.ia_atime = inode->i_atime;
284 attr.ia_size = oa.o_size;
286 inode->i_blocks = oa.o_blocks;
288 /* XXX: this introduces a small race that we should
290 rc = ll_inode_setattr(inode, &attr, 0);
292 CERROR("failed - %d.\n", rc);
293 rc = -EIO; /* XXX - GOTO(out)? -phil */
296 rc = ll_size_unlock(inode, lli->lli_smd, LCK_PR, lockhs);
298 CERROR("lock cancel: %d\n", rc);
303 rc = mdc_close(&sbi->ll_mdc_conn, inode->i_ino,
304 S_IFREG, &fd->fd_mdshandle, &req);
305 ptlrpc_req_finished(req);
311 ptlrpc_req_finished(fd->fd_req);
313 rc = obd_cancel_unused(ll_i2obdconn(inode), lli->lli_smd, 0);
315 CERROR("obd_cancel_unused: %d\n", rc);
320 kmem_cache_free(ll_file_data_slab, fd);
321 file->private_data = NULL;
327 static inline void ll_remove_suid(struct inode *inode)
331 /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
332 mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
334 /* was any of the uid bits set? */
335 mode &= inode->i_mode;
336 if (mode && !capable(CAP_FSETID)) {
337 inode->i_mode &= ~mode;
338 // XXX careful here - we cannot change the size
342 static void ll_update_atime(struct inode *inode)
346 attr.ia_atime = CURRENT_TIME;
347 attr.ia_valid = ATTR_ATIME;
349 if (inode->i_atime == attr.ia_atime) return;
350 if (IS_RDONLY(inode)) return;
351 if (IS_NOATIME(inode)) return;
353 /* ll_inode_setattr() sets inode->i_atime from attr.ia_atime */
354 ll_inode_setattr(inode, &attr, 0);
357 int ll_lock_callback(struct ldlm_lock *lock, struct ldlm_lock_desc *new,
358 void *data, __u32 data_len, int flag)
360 struct inode *inode = data;
361 struct lustre_handle lockh;
365 if (data_len != sizeof(struct inode))
372 case LDLM_CB_BLOCKING:
373 ldlm_lock2handle(lock, &lockh);
374 rc = ldlm_cli_cancel(&lockh);
376 CERROR("ldlm_cli_cancel failed: %d\n", rc);
378 case LDLM_CB_CANCELING:
379 CDEBUG(D_INODE, "invalidating obdo/inode %ld\n", inode->i_ino);
380 /* FIXME: do something better than throwing away everything */
381 //down(&inode->i_sem);
382 invalidate_inode_pages(inode);
392 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
395 struct ll_file_data *fd = (struct ll_file_data *)filp->private_data;
396 struct inode *inode = filp->f_dentry->d_inode;
397 struct ll_sb_info *sbi = ll_i2sbi(inode);
398 struct ldlm_extent extent;
399 struct lustre_handle *lockhs = NULL;
400 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
406 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
407 sbi->ll_flags & LL_SBI_NOLCK) {
408 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
412 extent.start = *ppos;
413 extent.end = *ppos + count;
414 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
415 inode->i_ino, extent.start, extent.end);
417 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
418 &extent, sizeof(extent), LCK_PR, &flags,
419 ll_lock_callback, inode, sizeof(*inode),
421 if (err != ELDLM_OK) {
422 OBD_FREE(lockhs, lsm->lsm_stripe_count*sizeof(*lockhs));
423 CERROR("lock enqueue: err: %d\n", err);
428 CDEBUG(D_INFO, "Reading inode %ld, %d bytes, offset %Ld\n",
429 inode->i_ino, count, *ppos);
430 retval = generic_file_read(filp, buf, count, ppos);
433 ll_update_atime(inode);
435 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
436 sbi->ll_flags & LL_SBI_NOLCK) {
437 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PR, lockhs);
438 if (err != ELDLM_OK) {
439 CERROR("lock cancel: err: %d\n", err);
445 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
450 * Write to a file (through the page cache).
453 ll_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
455 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
456 struct inode *inode = file->f_dentry->d_inode;
457 struct ll_sb_info *sbi = ll_i2sbi(inode);
458 struct ldlm_extent extent;
459 struct lustre_handle *lockhs = NULL, *eof_lockhs = NULL;
460 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
466 if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
473 err = ll_size_lock(inode, lsm, 0, LCK_PW, &eof_lockhs);
479 oa->o_id = lsm->lsm_object_id;
480 oa->o_mode = inode->i_mode;
481 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
483 obd_handle2oa(oa, &fd->fd_osthandle);
484 retval = obd_getattr(&sbi->ll_osc_conn, oa, lsm);
487 GOTO(out_eof, retval);
491 obdo_to_inode(inode, oa, oa->o_valid);
495 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
496 sbi->ll_flags & LL_SBI_NOLCK) {
497 OBD_ALLOC(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
499 GOTO(out_eof, retval = -ENOMEM);
500 extent.start = *ppos;
501 extent.end = *ppos + count;
502 CDEBUG(D_INFO, "Locking inode %ld, start "LPU64" end "LPU64"\n",
503 inode->i_ino, extent.start, extent.end);
505 err = obd_enqueue(&sbi->ll_osc_conn, lsm, NULL, LDLM_EXTENT,
506 &extent, sizeof(extent), LCK_PW, &flags,
507 ll_lock_callback, inode, sizeof(*inode),
509 if (err != ELDLM_OK) {
510 CERROR("lock enqueue: err: %d\n", err);
511 GOTO(out_free, retval = err);
515 CDEBUG(D_INFO, "Writing inode %ld, %ld bytes, offset "LPD64"\n",
516 inode->i_ino, (long)count, *ppos);
518 retval = generic_file_write(file, buf, count, ppos);
520 if (!(fd->fd_flags & LL_FILE_IGNORE_LOCK) ||
521 sbi->ll_flags & LL_SBI_NOLCK) {
522 err = obd_cancel(&sbi->ll_osc_conn, lsm, LCK_PW, lockhs);
523 if (err != ELDLM_OK) {
524 CERROR("lock cancel: err: %d\n", err);
525 GOTO(out_free, retval = err);
532 OBD_FREE(lockhs, lsm->lsm_stripe_count * sizeof(*lockhs));
535 if (!S_ISBLK(inode->i_mode) && file->f_flags & O_APPEND) {
536 err = ll_size_unlock(inode, lsm, LCK_PW, eof_lockhs);
544 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
547 struct ll_file_data *fd = (struct ll_file_data *)file->private_data;
551 case LL_IOC_GETFLAGS:
552 /* Get the current value of the file flags */
553 return put_user(fd->fd_flags, (int *)arg);
554 case LL_IOC_SETFLAGS:
555 case LL_IOC_CLRFLAGS:
556 /* Set or clear specific file flags */
557 /* XXX This probably needs checks to ensure the flags are
558 * not abused, and to handle any flag side effects.
560 if (get_user(flags, (int *) arg))
563 if (cmd == LL_IOC_SETFLAGS)
564 fd->fd_flags |= flags;
566 fd->fd_flags &= ~flags;
569 /* We need to special case any other ioctls we want to handle,
570 * to send them to the MDS/OST as appropriate and to properly
571 * network encode the arg field.
572 case EXT2_IOC_GETFLAGS:
573 case EXT2_IOC_SETFLAGS:
574 case EXT2_IOC_GETVERSION_OLD:
575 case EXT2_IOC_GETVERSION_NEW:
576 case EXT2_IOC_SETVERSION_OLD:
577 case EXT2_IOC_SETVERSION_NEW:
584 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
586 struct inode *inode = file->f_dentry->d_inode;
590 CHECK_MOUNT_EPOCH(inode);
594 struct ll_inode_info *lli = ll_i2info(inode);
596 retval = ll_file_size(inode, lli->lli_smd);
600 offset += inode->i_size;
604 offset += file->f_pos;
607 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
608 if (offset != file->f_pos) {
609 file->f_pos = offset;
611 file->f_version = ++event;
618 /* XXX this does not need to do anything for data, it _does_ need to
620 int ll_fsync(struct file *file, struct dentry *dentry, int data)
625 static int ll_inode_revalidate(struct dentry *dentry)
627 struct inode *inode = dentry->d_inode;
633 RETURN(ll_file_size(inode, ll_i2info(inode)->lli_smd));
636 struct file_operations ll_file_operations = {
638 write: ll_file_write,
639 ioctl: ll_file_ioctl,
641 release: ll_file_release,
642 mmap: generic_file_mmap,
643 llseek: ll_file_seek,
647 struct inode_operations ll_file_inode_operations = {
648 truncate: ll_truncate,
650 revalidate: ll_inode_revalidate