1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5 * Author: Peter Braam <braam@clusterfs.com>
6 * Author: Phil Schwan <phil@clusterfs.com>
7 * Author: Andreas Dilger <adilger@clusterfs.com>
9 * This file is part of Lustre, http://www.lustre.org.
11 * Lustre is free software; you can redistribute it and/or
12 * modify it under the terms of version 2 of the GNU General Public
13 * License as published by the Free Software Foundation.
15 * Lustre is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with Lustre; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #define DEBUG_SUBSYSTEM S_LLITE
26 #include <linux/lustre_dlm.h>
27 #include <linux/lustre_lite.h>
28 #include <linux/obd_lov.h> /* for lov_mds_md_size() in lov_setstripe() */
29 #include <linux/random.h>
30 #include <linux/pagemap.h>
31 #include <linux/file.h>
32 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
33 #include <linux/lustre_compat25.h>
36 #include "llite_internal.h"
38 static int ll_mdc_close(struct obd_export *mdc_exp, struct inode *inode,
41 struct ll_file_data *fd = file->private_data;
42 struct ptlrpc_request *req = NULL;
43 struct obd_client_handle *och = &fd->fd_mds_och;
44 struct ll_inode_info *lli = ll_i2info(inode);
50 if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
51 valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
53 memset(&obdo, 0, sizeof(obdo));
54 obdo.o_id = inode->i_ino;
55 obdo.o_mode = inode->i_mode;
56 obdo.o_size = inode->i_size;
57 obdo.o_blocks = inode->i_blocks;
58 if (0 /* ll_is_inode_dirty(inode) */) {
59 obdo.o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
60 valid |= OBD_MD_FLFLAGS;
63 rc = mdc_close(mdc_exp, &obdo, och, &req);
65 /* We are the last writer, so the MDS has instructed us to get
66 * the file size and any write cookies, then close again. */
67 //ll_queue_done_writing(inode);
70 CERROR("inode %lu mdc close failed: rc = %d\n",
74 rc = ll_objects_destroy(req, file->f_dentry->d_inode);
76 CERROR("inode %lu ll_objects destroy: rc = %d\n",
80 mdc_clear_open_replay_data(och);
81 ptlrpc_req_finished(req);
82 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
83 file->private_data = NULL;
84 OBD_SLAB_FREE(fd, ll_file_data_slab, sizeof *fd);
89 /* While this returns an error code, fput() the caller does not, so we need
90 * to make every effort to clean up all of our state here. Also, applications
91 * rarely check close errors and even if an error is returned they will not
92 * re-try the close call.
94 int ll_file_release(struct inode *inode, struct file *file)
96 struct ll_file_data *fd;
97 struct ll_sb_info *sbi = ll_i2sbi(inode);
101 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
102 inode->i_generation, inode);
104 /* don't do anything for / */
105 if (inode->i_sb->s_root == file->f_dentry)
108 lprocfs_counter_incr(sbi->ll_stats, LPROC_LL_RELEASE);
109 fd = (struct ll_file_data *)file->private_data;
112 rc = ll_mdc_close(sbi->ll_mdc_exp, inode, file);
116 static int ll_intent_file_open(struct file *file, void *lmm,
117 int lmmsize, struct lookup_intent *itp)
119 struct ll_sb_info *sbi = ll_i2sbi(file->f_dentry->d_inode);
120 struct lustre_handle lockh;
121 struct mdc_op_data data;
122 struct dentry *parent = file->f_dentry->d_parent;
123 const char *name = file->f_dentry->d_name.name;
124 const int len = file->f_dentry->d_name.len;
130 ll_prepare_mdc_op_data(&data, parent->d_inode, NULL, name, len, O_RDWR);
132 rc = mdc_enqueue(sbi->ll_mdc_exp, LDLM_PLAIN, itp, LCK_PR, &data,
133 &lockh, lmm, lmmsize, ldlm_completion_ast,
134 ll_mdc_blocking_ast, parent->d_inode);
136 CERROR("lock enqueue: err: %d\n", rc);
140 static int ll_local_open(struct file *file, struct lookup_intent *it)
142 struct ptlrpc_request *req = it->d.lustre.it_data;
143 struct ll_inode_info *lli = ll_i2info(file->f_dentry->d_inode);
144 struct ll_file_data *fd;
145 struct mds_body *body;
148 body = lustre_msg_buf (req->rq_repmsg, 1, sizeof (*body));
149 LASSERT (body != NULL); /* reply already checked out */
150 LASSERT_REPSWABBED (req, 1); /* and swabbed down */
152 LASSERT(!file->private_data);
154 OBD_SLAB_ALLOC(fd, ll_file_data_slab, SLAB_KERNEL, sizeof *fd);
155 /* We can't handle this well without reorganizing ll_file_open and
156 * ll_mdc_close, so don't even try right now. */
159 memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
160 fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
161 file->private_data = fd;
163 lli->lli_io_epoch = body->io_epoch;
165 mdc_set_open_replay_data(&fd->fd_mds_och, it->d.lustre.it_data);
170 /* Open a file, and (for the very first open) create objects on the OSTs at
171 * this time. If opened with O_LOV_DELAY_CREATE, then we don't do the object
172 * creation or open until ll_lov_setstripe() ioctl is called. We grab
173 * lli_open_sem to ensure no other process will create objects, send the
174 * stripe MD to the MDS, or try to destroy the objects if that fails.
176 * If we already have the stripe MD locally then we don't request it in
177 * mdc_open(), by passing a lmm_size = 0.
179 * It is up to the application to ensure no other processes open this file
180 * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
181 * used. We might be able to avoid races of that sort by getting lli_open_sem
182 * before returning in the O_LOV_DELAY_CREATE case and dropping it here
183 * or in ll_file_release(), but I'm not sure that is desirable/necessary.
185 int ll_file_open(struct inode *inode, struct file *file)
187 struct ll_inode_info *lli = ll_i2info(inode);
188 struct lookup_intent *it;
189 struct lov_stripe_md *lsm;
190 struct ptlrpc_request *req;
194 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
195 inode->i_generation, inode);
197 /* don't do anything for / */
198 if (inode->i_sb->s_root == file->f_dentry)
203 if (!it->d.lustre.it_disposition) {
204 struct lookup_intent oit = { .it_op = IT_OPEN,
205 .it_flags = file->f_flags };
207 rc = ll_intent_file_open(file, NULL, 0, it);
212 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_OPEN);
213 rc = it_open_error(DISP_OPEN_OPEN, it);
217 rc = ll_local_open(file, it);
221 if (!S_ISREG(inode->i_mode))
226 if (file->f_flags & O_LOV_DELAY_CREATE ||
227 !(file->f_mode & FMODE_WRITE)) {
228 CDEBUG(D_INODE, "object creation was delayed\n");
232 file->f_flags &= ~O_LOV_DELAY_CREATE;
235 req = it->d.lustre.it_data;
236 ptlrpc_req_finished(req);
238 ll_open_complete(inode);
242 /* Fills the obdo with the attributes for the inode defined by lsm */
243 int ll_lsm_getattr(struct obd_export *exp, struct lov_stripe_md *lsm,
246 struct ptlrpc_request_set *set;
250 LASSERT(lsm != NULL);
252 memset(oa, 0, sizeof *oa);
253 oa->o_id = lsm->lsm_object_id;
254 oa->o_mode = S_IFREG;
255 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
256 OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
259 set = ptlrpc_prep_set();
261 CERROR ("ENOMEM allocing request set\n");
264 rc = obd_getattr_async(exp, oa, lsm, set);
266 rc = ptlrpc_set_wait(set);
267 ptlrpc_set_destroy(set);
272 oa->o_valid &= (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
273 OBD_MD_FLCTIME | OBD_MD_FLSIZE);
277 static inline void ll_remove_suid(struct inode *inode)
281 /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
282 mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
284 /* was any of the uid bits set? */
285 mode &= inode->i_mode;
286 if (mode && !capable(CAP_FSETID)) {
287 inode->i_mode &= ~mode;
288 // XXX careful here - we cannot change the size
292 /* Flush the page cache for an extent as its canceled. No one can dirty the
293 * extent until we've finished our work and they can enqueue another lock.
294 * The DLM protects us from ll_file_read/write here, but other kernel actors
295 * could have pages locked */
296 void ll_pgcache_remove_extent(struct inode *inode, struct lov_stripe_md *lsm,
297 struct ldlm_lock *lock)
299 struct ldlm_extent *extent = &lock->l_policy_data.l_extent;
300 struct obd_export *exp = ll_i2obdexp(inode);
301 struct ll_inode_info *lli = ll_i2info(inode);
302 unsigned long start, end, i;
304 int rc, discard = lock->l_flags & LDLM_FL_DISCARD_DATA;
307 CDEBUG(D_INODE, "obdo %lu inode %p ["LPU64"->"LPU64"] size: %llu\n",
308 inode->i_ino, inode, extent->start, extent->end, inode->i_size);
310 start = extent->start >> PAGE_CACHE_SHIFT;
311 end = (extent->end >> PAGE_CACHE_SHIFT) + 1;
312 if ((end << PAGE_CACHE_SHIFT) < extent->end)
315 i = (inode->i_size + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
317 clear_bit(LLI_F_HAVE_OST_SIZE_LOCK,
318 &(ll_i2info(inode)->lli_flags));
322 CDEBUG(D_INODE, "walking page indices start: %lu end: %lu\n", start,
325 for (i = start; i < end; i++) {
326 ll_pgcache_lock(inode->i_mapping);
327 if (list_empty(&inode->i_mapping->dirty_pages) &&
328 list_empty(&inode->i_mapping->clean_pages) &&
329 list_empty(&inode->i_mapping->locked_pages)) {
330 CDEBUG(D_INODE, "nothing left\n");
331 ll_pgcache_unlock(inode->i_mapping);
334 ll_pgcache_unlock(inode->i_mapping);
336 conditional_schedule();
338 page = find_get_page(inode->i_mapping, i);
342 LL_CDEBUG_PAGE(page, "locking\n");
345 /* page->mapping to check with racing against teardown */
346 if (page->mapping && PageDirty(page) && !discard) {
347 ClearPageDirty(page);
348 LL_CDEBUG_PAGE(page, "found dirty\n");
349 ll_pgcache_lock(inode->i_mapping);
350 list_del(&page->list);
351 list_add(&page->list, &inode->i_mapping->locked_pages);
352 ll_pgcache_unlock(inode->i_mapping);
354 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
355 rc = inode->i_mapping->a_ops->writepage(page);
357 rc = inode->i_mapping->a_ops->writepage(page, NULL);
360 CERROR("writepage of page %p failed: %d\n",
363 lock_page(page); /* wait for io to complete */
367 /* checking again to account for writeback's lock_page() */
368 if (page->mapping != NULL) {
369 LL_CDEBUG_PAGE(page, "truncating\n");
370 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
371 truncate_complete_page(page);
373 truncate_complete_page(page->mapping, page);
377 page_cache_release(page);
380 if (test_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags)) {
381 rc = obd_lock_contains(exp, lsm, lock, inode->i_size - 1);
384 CERROR("obd_lock_contains: rc = %d\n", rc);
385 clear_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags);
392 static int ll_extent_lock_callback(struct ldlm_lock *lock,
393 struct ldlm_lock_desc *new, void *data,
396 struct lustre_handle lockh = { 0 };
401 if ((unsigned long)data > 0 && (unsigned long)data < 0x1000) {
402 LDLM_ERROR(lock, "cancelling lock with bad data %p", data);
407 case LDLM_CB_BLOCKING:
408 ldlm_lock2handle(lock, &lockh);
409 rc = ldlm_cli_cancel(&lockh);
411 CERROR("ldlm_cli_cancel failed: %d\n", rc);
413 case LDLM_CB_CANCELING: {
414 struct inode *inode = ll_inode_from_lock(lock);
415 struct ll_inode_info *lli;
419 lli= ll_i2info(inode);
425 ll_pgcache_remove_extent(inode, lli->lli_smd, lock);
426 //ll_try_done_writing(inode);
438 * some callers, notably truncate, really don't want i_size set based
439 * on the the size returned by the getattr, or lock acquisition in
442 int ll_extent_lock_no_validate(struct ll_file_data *fd, struct inode *inode,
443 struct lov_stripe_md *lsm,
444 int mode, struct ldlm_extent *extent,
445 struct lustre_handle *lockh, int ast_flags)
447 struct ll_sb_info *sbi = ll_i2sbi(inode);
451 LASSERT(lockh->cookie == 0);
453 /* XXX phil: can we do this? won't it screw the file size up? */
454 if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
455 (sbi->ll_flags & LL_SBI_NOLCK))
458 CDEBUG(D_DLMTRACE, "Locking inode %lu, start "LPU64" end "LPU64"\n",
459 inode->i_ino, extent->start, extent->end);
461 rc = obd_enqueue(sbi->ll_osc_exp, lsm, NULL, LDLM_EXTENT, extent,
462 sizeof(extent), mode, &ast_flags,
463 ll_extent_lock_callback, inode, lockh);
470 * this grabs a lock and manually implements behaviour that makes it look like
471 * the OST is returning the file size with each lock acquisition.
473 int ll_extent_lock(struct ll_file_data *fd, struct inode *inode,
474 struct lov_stripe_md *lsm, int mode,
475 struct ldlm_extent *extent, struct lustre_handle *lockh)
477 struct ll_inode_info *lli = ll_i2info(inode);
478 struct obd_export *exp = ll_i2obdexp(inode);
479 struct ldlm_extent size_lock;
480 struct lustre_handle match_lockh = {0};
482 obd_flag refresh_valid;
483 int flags, rc, matched;
486 rc = ll_extent_lock_no_validate(fd, inode, lsm, mode, extent, lockh, 0);
490 if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
493 rc = ll_lsm_getattr(exp, lsm, &oa);
497 /* We set this flag in commit write as we extend the file size. When
498 * the bit is set and the lock is canceled that covers the file size,
499 * we clear the bit. This is enough to protect the window where our
500 * local size extension is needed for writeback. However, it relies on
501 * behaviour that won't be true in the near future. This assumes that
502 * all getattr callers get extent locks, which they currnetly do. It
503 * also assumes that we only send discarding asts for {0,eof} truncates
504 * as is currently the case. This will have to be replaced by the
505 * proper eoc communication between clients and the ost, which is on
507 refresh_valid = (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
508 OBD_MD_FLCTIME | OBD_MD_FLSIZE);
509 if (test_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags)) {
510 if (oa.o_size < inode->i_size)
511 refresh_valid &= ~OBD_MD_FLSIZE;
513 clear_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags);
515 obdo_refresh_inode(inode, &oa, refresh_valid);
517 CDEBUG(D_INODE, "objid "LPX64" size %Lu, blocks %lu, blksize %lu\n",
518 lsm->lsm_object_id, inode->i_size, inode->i_blocks,
521 size_lock.start = inode->i_size;
522 size_lock.end = OBD_OBJECT_EOF;
524 /* XXX I bet we should be checking the lock ignore flags.. */
525 flags = LDLM_FL_CBPENDING | LDLM_FL_BLOCK_GRANTED;
526 matched = obd_match(exp, lsm, LDLM_EXTENT, &size_lock,
527 sizeof(size_lock), LCK_PR, &flags, inode,
530 GOTO(out, rc = matched);
532 /* hey, alright, we hold a size lock that covers the size we
533 * just found, its not going to change for a while.. */
535 set_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags);
536 obd_cancel(exp, lsm, LCK_PR, &match_lockh);
542 ll_extent_unlock(fd, inode, lsm, mode, lockh);
546 int ll_extent_unlock(struct ll_file_data *fd, struct inode *inode,
547 struct lov_stripe_md *lsm, int mode,
548 struct lustre_handle *lockh)
550 struct ll_sb_info *sbi = ll_i2sbi(inode);
554 /* XXX phil: can we do this? won't it screw the file size up? */
555 if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
556 (sbi->ll_flags & LL_SBI_NOLCK))
559 rc = obd_cancel(sbi->ll_osc_exp, lsm, mode, lockh);
564 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
567 struct ll_file_data *fd = filp->private_data;
568 struct inode *inode = filp->f_dentry->d_inode;
569 struct ll_inode_info *lli = ll_i2info(inode);
570 struct lov_stripe_md *lsm = lli->lli_smd;
571 struct lustre_handle lockh = { 0 };
572 struct ldlm_extent extent;
576 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
577 inode->i_ino, inode->i_generation, inode, count, *ppos);
579 /* "If nbyte is 0, read() will return 0 and have no other results."
580 * -- Single Unix Spec */
584 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_READ_BYTES,
590 /* grab a -> eof extent to push extending writes out of node's caches
591 * so we can see them at the getattr after lock acquisition. this will
592 * turn into a seperate [*ppos + count, EOF] 'size intent' lock attempt
594 extent.start = *ppos;
595 extent.end = OBD_OBJECT_EOF;
597 err = ll_extent_lock(fd, inode, lsm, LCK_PR, &extent, &lockh);
601 CDEBUG(D_INFO, "Reading inode %lu, "LPSZ" bytes, offset %Ld\n",
602 inode->i_ino, count, *ppos);
603 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
604 filp->f_ramax = 0; /* turn off generic_file_readahead() */
606 filp->f_ra.ra_pages = 0;
608 retval = generic_file_read(filp, buf, count, ppos);
611 ll_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
616 * Write to a file (through the page cache).
618 static ssize_t ll_file_write(struct file *file, const char *buf, size_t count,
621 struct ll_file_data *fd = file->private_data;
622 struct inode *inode = file->f_dentry->d_inode;
623 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
624 struct lustre_handle lockh = { 0 };
625 struct ldlm_extent extent;
626 loff_t maxbytes = ll_file_maxbytes(inode);
629 char should_validate = 1;
631 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
632 inode->i_ino, inode->i_generation, inode, count, *ppos);
634 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
636 /* POSIX, but surprised the VFS doesn't check this already */
642 if (file->f_flags & O_APPEND) {
644 extent.end = OBD_OBJECT_EOF;
646 extent.start = *ppos;
647 extent.end = *ppos + count - 1;
648 /* we really don't care what i_size is if we're doing
649 * fully page aligned writes */
650 if ((*ppos & ~PAGE_CACHE_MASK) == 0 &&
651 (count & ~PAGE_CACHE_MASK) == 0)
656 err = ll_extent_lock(fd, inode, lsm, LCK_PW, &extent, &lockh);
658 err = ll_extent_lock_no_validate(fd, inode, lsm, LCK_PW,
663 /* this is ok, g_f_w will overwrite this under i_sem if it races
664 * with a local truncate, it just makes our maxbyte checking easier */
665 if (file->f_flags & O_APPEND)
666 *ppos = inode->i_size;
668 if (*ppos >= maxbytes) {
669 if (count || *ppos > maxbytes) {
670 send_sig(SIGXFSZ, current, 0);
671 GOTO(out, retval = -EFBIG);
674 if (*ppos + count > maxbytes)
675 count = maxbytes - *ppos;
677 CDEBUG(D_INFO, "Writing inode %lu, "LPSZ" bytes, offset %Lu\n",
678 inode->i_ino, count, *ppos);
680 /* generic_file_write handles O_APPEND after getting i_sem */
681 retval = generic_file_write(file, buf, count, ppos);
685 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_WRITE_BYTES,
687 ll_extent_unlock(fd, inode, lsm, LCK_PW, &lockh);
691 static int ll_lov_setstripe(struct inode *inode, struct file *file,
694 struct ll_inode_info *lli = ll_i2info(inode);
696 struct obd_export *exp = ll_i2obdexp(inode);
697 struct lov_stripe_md *lsm;
698 struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = FMODE_WRITE};
699 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
700 struct ptlrpc_request *req = NULL;
705 /* Bug 1152: copy properly when this is no longer true */
706 LASSERT(sizeof(lum) == sizeof(*lump));
707 LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
708 rc = copy_from_user(&lum, lump, sizeof(lum));
712 down(&lli->lli_open_sem);
715 up(&lli->lli_open_sem);
716 CDEBUG(D_IOCTL, "stripe already exists for ino %lu\n",
721 f = get_empty_filp();
725 f->f_dentry = file->f_dentry;
726 f->f_vfsmnt = file->f_vfsmnt;
728 rc = ll_intent_file_open(f, &lum, sizeof(lum), &oit);
731 if (it_disposition(&oit, DISP_LOOKUP_NEG))
733 req = oit.d.lustre.it_data;
734 rc = oit.d.lustre.it_status;
739 rc = mdc_req2lustre_md(req, 1, exp, &md);
742 ll_update_inode(f->f_dentry->d_inode, md.body, md.lsm);
744 rc = ll_local_open(f, &oit);
747 ll_intent_release(&oit);
749 rc = ll_file_release(f->f_dentry->d_inode, f);
754 up(&lli->lli_open_sem);
756 ptlrpc_req_finished(req);
760 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
762 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
767 return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2obdexp(inode), 0, lsm,
771 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
774 struct ll_file_data *fd = file->private_data;
778 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),cmd=%x\n", inode->i_ino,
779 inode->i_generation, inode, cmd);
781 if (_IOC_TYPE(cmd) == 'T') /* tty ioctls */
784 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
786 case LL_IOC_GETFLAGS:
787 /* Get the current value of the file flags */
788 return put_user(fd->fd_flags, (int *)arg);
789 case LL_IOC_SETFLAGS:
790 case LL_IOC_CLRFLAGS:
791 /* Set or clear specific file flags */
792 /* XXX This probably needs checks to ensure the flags are
793 * not abused, and to handle any flag side effects.
795 if (get_user(flags, (int *) arg))
798 if (cmd == LL_IOC_SETFLAGS)
799 fd->fd_flags |= flags;
801 fd->fd_flags &= ~flags;
803 case LL_IOC_LOV_SETSTRIPE:
804 RETURN(ll_lov_setstripe(inode, file, arg));
805 case LL_IOC_LOV_GETSTRIPE:
806 RETURN(ll_lov_getstripe(inode, arg));
807 case EXT3_IOC_GETFLAGS:
808 case EXT3_IOC_SETFLAGS:
809 RETURN( ll_iocontrol(inode, file, cmd, arg) );
810 /* We need to special case any other ioctls we want to handle,
811 * to send them to the MDS/OST as appropriate and to properly
812 * network encode the arg field.
813 case EXT2_IOC_GETVERSION_OLD:
814 case EXT2_IOC_GETVERSION_NEW:
815 case EXT2_IOC_SETVERSION_OLD:
816 case EXT2_IOC_SETVERSION_NEW:
819 RETURN( obd_iocontrol(cmd, ll_i2obdexp(inode), 0, NULL,
824 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
826 struct inode *inode = file->f_dentry->d_inode;
827 struct ll_file_data *fd = file->private_data;
828 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
829 struct lustre_handle lockh = {0};
832 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),to=%llu\n", inode->i_ino,
833 inode->i_generation, inode,
834 offset + ((origin==2) ? inode->i_size : file->f_pos));
836 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_LLSEEK);
837 if (origin == 2) { /* SEEK_END */
839 struct ldlm_extent extent = {0, OBD_OBJECT_EOF};
840 err = ll_extent_lock(fd, inode, lsm, LCK_PR, &extent, &lockh);
844 offset += inode->i_size;
845 } else if (origin == 1) { /* SEEK_CUR */
846 offset += file->f_pos;
850 if (offset >= 0 && offset <= ll_file_maxbytes(inode)) {
851 if (offset != file->f_pos) {
852 file->f_pos = offset;
853 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
855 file->f_version = ++event;
862 ll_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
866 int ll_fsync(struct file *file, struct dentry *dentry, int data)
868 struct inode *inode = dentry->d_inode;
869 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
871 struct ptlrpc_request *req;
874 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
875 inode->i_generation, inode);
877 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_FSYNC);
879 /* fsync's caller has already called _fdata{sync,write}, we want
880 * that IO to finish before calling the osc and mdc sync methods */
881 rc = filemap_fdatawait(inode->i_mapping);
883 ll_inode2fid(&fid, inode);
884 err = mdc_sync(ll_i2sbi(inode)->ll_mdc_exp, &fid, &req);
888 ptlrpc_req_finished(req);
891 struct obdo *oa = obdo_alloc();
894 RETURN(rc ? rc : -ENOMEM);
896 oa->o_id = lsm->lsm_object_id;
897 oa->o_valid = OBD_MD_FLID;
898 obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
899 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
901 err = obd_sync(ll_i2sbi(inode)->ll_osc_exp, oa, lsm,
911 int ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
913 struct inode *inode = file->f_dentry->d_inode;
914 struct ll_sb_info *sbi = ll_i2sbi(inode);
915 struct obd_device *obddev;
916 struct ldlm_res_id res_id =
917 { .name = {inode->i_ino, inode->i_generation, LDLM_FLOCK} };
918 struct lustre_handle lockh = {0};
919 struct ldlm_flock flock;
920 ldlm_mode_t mode = 0;
925 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu file_lock=%p\n",
926 inode->i_ino, file_lock);
928 flock.pid = file_lock->fl_pid;
929 flock.start = file_lock->fl_start;
930 flock.end = file_lock->fl_end;
932 switch (file_lock->fl_type) {
937 /* An unlock request may or may not have any relation to
938 * existing locks so we may not be able to pass a lock handle
939 * via a normal ldlm_lock_cancel() request. The request may even
940 * unlock a byte range in the middle of an existing lock. In
941 * order to process an unlock request we need all of the same
942 * information that is given with a normal read or write record
943 * lock request. To avoid creating another ldlm unlock (cancel)
944 * message we'll treat a LCK_NL flock request as an unlock. */
951 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
960 flags = LDLM_FL_BLOCK_NOWAIT;
963 flags = LDLM_FL_TEST_LOCK;
964 /* Save the old mode so that if the mode in the lock changes we
965 * can decrement the appropriate reader or writer refcount. */
966 file_lock->fl_type = mode;
969 CERROR("unknown fcntl lock command: %d\n", cmd);
973 CDEBUG(D_DLMTRACE, "inode=%lu, pid=%u, flags=%#x, mode=%u, "
974 "start="LPU64", end="LPU64"\n", inode->i_ino, flock.pid,
975 flags, mode, flock.start, flock.end);
977 obddev = sbi->ll_mdc_exp->exp_obd;
978 rc = ldlm_cli_enqueue(sbi->ll_mdc_exp, NULL, obddev->obd_namespace,
979 NULL, res_id, LDLM_FLOCK, &flock, sizeof(flock),
980 mode, &flags, ldlm_flock_completion_ast, NULL,
985 static int ll_have_md_lock(struct dentry *de)
987 struct ll_sb_info *sbi = ll_s2sbi(de->d_sb);
988 struct lustre_handle lockh;
989 struct ldlm_res_id res_id = { .name = {0} };
990 struct obd_device *obddev;
997 obddev = sbi->ll_mdc_exp->exp_obd;
998 res_id.name[0] = de->d_inode->i_ino;
999 res_id.name[1] = de->d_inode->i_generation;
1001 CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
1003 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
1004 if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_PLAIN,
1005 NULL, 0, LCK_PR, &lockh)) {
1006 ldlm_lock_decref(&lockh, LCK_PR);
1010 if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_PLAIN,
1011 NULL, 0, LCK_PW, &lockh)) {
1012 ldlm_lock_decref(&lockh, LCK_PW);
1018 int ll_inode_revalidate_it(struct dentry *dentry, struct lookup_intent *it)
1020 struct inode *inode = dentry->d_inode;
1021 struct lov_stripe_md *lsm;
1025 CERROR("REPORT THIS LINE TO PETER\n");
1028 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),name=%s\n",
1029 inode->i_ino, inode->i_generation, inode, dentry->d_name.name);
1030 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
1031 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_REVALIDATE);
1034 if (!ll_have_md_lock(dentry)) {
1035 struct ptlrpc_request *req = NULL;
1036 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
1038 unsigned long valid = 0;
1041 if (S_ISREG(inode->i_mode)) {
1042 ealen = obd_size_diskmd(sbi->ll_osc_exp, NULL);
1043 valid |= OBD_MD_FLEASIZE;
1045 ll_inode2fid(&fid, inode);
1046 rc = mdc_getattr(sbi->ll_mdc_exp, &fid, valid, ealen, &req);
1048 CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1051 rc = ll_prep_inode(sbi->ll_osc_exp, &inode, req, 0, NULL);
1053 ptlrpc_req_finished(req);
1056 ptlrpc_req_finished(req);
1060 if (ll_have_md_lock(dentry) &&
1061 test_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &ll_i2info(inode)->lli_flags))
1065 lsm = ll_i2info(inode)->lli_smd;
1066 if (!lsm) /* object not yet allocated, don't validate size */
1069 /* unfortunately stat comes in through revalidate and we don't
1070 * differentiate this use from initial instantiation. we're
1071 * also being wildly conservative and flushing write caches
1072 * so that stat really returns the proper size. */
1074 struct ldlm_extent extent = {0, OBD_OBJECT_EOF};
1075 struct lustre_handle lockh = {0};
1078 err = ll_extent_lock(NULL, inode, lsm, LCK_PR, &extent, &lockh);
1079 if (err != ELDLM_OK)
1082 ll_extent_unlock(NULL, inode, lsm, LCK_PR, &lockh);
1087 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1088 int ll_getattr(struct vfsmount *mnt, struct dentry *de,
1089 struct lookup_intent *it, struct kstat *stat)
1092 struct inode *inode = de->d_inode;
1094 res = ll_inode_revalidate_it(de, it);
1095 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_GETATTR);
1100 stat->dev = inode->i_sb->s_dev;
1101 stat->ino = inode->i_ino;
1102 stat->mode = inode->i_mode;
1103 stat->nlink = inode->i_nlink;
1104 stat->uid = inode->i_uid;
1105 stat->gid = inode->i_gid;
1106 stat->rdev = kdev_t_to_nr(inode->i_rdev);
1107 stat->atime = inode->i_atime;
1108 stat->mtime = inode->i_mtime;
1109 stat->ctime = inode->i_ctime;
1110 stat->size = inode->i_size;
1111 stat->blksize = inode->i_blksize;
1112 stat->blocks = inode->i_blocks;
1117 struct file_operations ll_file_operations = {
1119 write: ll_file_write,
1120 ioctl: ll_file_ioctl,
1122 release: ll_file_release,
1123 mmap: generic_file_mmap,
1124 llseek: ll_file_seek,
1126 //lock: ll_file_flock
1129 struct inode_operations ll_file_inode_operations = {
1130 setattr_raw: ll_setattr_raw,
1131 setattr: ll_setattr,
1132 truncate: ll_truncate,
1133 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1134 getattr_it: ll_getattr,
1136 revalidate_it: ll_inode_revalidate_it,
1140 struct inode_operations ll_special_inode_operations = {
1141 setattr_raw: ll_setattr_raw,
1142 setattr: ll_setattr,
1143 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1144 getattr_it: ll_getattr,
1146 revalidate_it: ll_inode_revalidate_it,