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/pagemap.h>
29 #include <linux/file.h>
30 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
31 #include <linux/lustre_compat25.h>
34 #include "llite_internal.h"
36 static int ll_mdc_close(struct obd_export *mdc_exp, struct inode *inode,
39 struct ll_file_data *fd = file->private_data;
40 struct ptlrpc_request *req = NULL;
41 struct obd_client_handle *och = &fd->fd_mds_och;
42 struct ll_inode_info *lli = ll_i2info(inode);
48 if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
49 valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
51 memset(&obdo, 0, sizeof(obdo));
52 obdo.o_id = inode->i_ino;
53 obdo.o_mode = inode->i_mode;
54 obdo.o_size = inode->i_size;
55 obdo.o_blocks = inode->i_blocks;
56 if (0 /* ll_is_inode_dirty(inode) */) {
57 obdo.o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
58 valid |= OBD_MD_FLFLAGS;
61 rc = mdc_close(mdc_exp, &obdo, och, &req);
63 /* We are the last writer, so the MDS has instructed us to get
64 * the file size and any write cookies, then close again. */
65 //ll_queue_done_writing(inode);
68 CERROR("inode %lu mdc close failed: rc = %d\n",
72 rc = ll_objects_destroy(req, file->f_dentry->d_inode);
74 CERROR("inode %lu ll_objects destroy: rc = %d\n",
78 mdc_clear_open_replay_data(och);
79 ptlrpc_req_finished(req);
80 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
81 file->private_data = NULL;
82 OBD_SLAB_FREE(fd, ll_file_data_slab, sizeof *fd);
87 /* While this returns an error code, fput() the caller does not, so we need
88 * to make every effort to clean up all of our state here. Also, applications
89 * rarely check close errors and even if an error is returned they will not
90 * re-try the close call.
92 int ll_file_release(struct inode *inode, struct file *file)
94 struct ll_file_data *fd;
95 struct ll_sb_info *sbi = ll_i2sbi(inode);
99 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
100 inode->i_generation, inode);
102 /* don't do anything for / */
103 if (inode->i_sb->s_root == file->f_dentry)
106 lprocfs_counter_incr(sbi->ll_stats, LPROC_LL_RELEASE);
107 fd = (struct ll_file_data *)file->private_data;
110 rc = ll_mdc_close(sbi->ll_mdc_exp, inode, file);
114 static int ll_intent_file_open(struct file *file, void *lmm,
115 int lmmsize, struct lookup_intent *itp)
117 struct ll_sb_info *sbi = ll_i2sbi(file->f_dentry->d_inode);
118 struct lustre_handle lockh;
119 struct mdc_op_data data;
120 struct dentry *parent = file->f_dentry->d_parent;
121 const char *name = file->f_dentry->d_name.name;
122 const int len = file->f_dentry->d_name.len;
128 ll_prepare_mdc_op_data(&data, parent->d_inode, NULL, name, len, O_RDWR);
130 rc = mdc_enqueue(sbi->ll_mdc_exp, LDLM_PLAIN, itp, LCK_PR, &data,
131 &lockh, lmm, lmmsize, ldlm_completion_ast,
132 ll_mdc_blocking_ast, parent->d_inode);
134 CERROR("lock enqueue: err: %d\n", rc);
138 static int ll_local_open(struct file *file, struct lookup_intent *it)
140 struct ptlrpc_request *req = it->d.lustre.it_data;
141 struct ll_inode_info *lli = ll_i2info(file->f_dentry->d_inode);
142 struct ll_file_data *fd;
143 struct mds_body *body;
146 body = lustre_msg_buf (req->rq_repmsg, 1, sizeof (*body));
147 LASSERT (body != NULL); /* reply already checked out */
148 LASSERT_REPSWABBED (req, 1); /* and swabbed down */
150 LASSERT(!file->private_data);
152 OBD_SLAB_ALLOC(fd, ll_file_data_slab, SLAB_KERNEL, sizeof *fd);
153 /* We can't handle this well without reorganizing ll_file_open and
154 * ll_mdc_close, so don't even try right now. */
157 memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
158 fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
159 file->private_data = fd;
160 ll_readahead_init(&fd->fd_ras);
162 lli->lli_io_epoch = body->io_epoch;
164 mdc_set_open_replay_data(&fd->fd_mds_och, it->d.lustre.it_data);
169 /* Open a file, and (for the very first open) create objects on the OSTs at
170 * this time. If opened with O_LOV_DELAY_CREATE, then we don't do the object
171 * creation or open until ll_lov_setstripe() ioctl is called. We grab
172 * lli_open_sem to ensure no other process will create objects, send the
173 * stripe MD to the MDS, or try to destroy the objects if that fails.
175 * If we already have the stripe MD locally then we don't request it in
176 * mdc_open(), by passing a lmm_size = 0.
178 * It is up to the application to ensure no other processes open this file
179 * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
180 * used. We might be able to avoid races of that sort by getting lli_open_sem
181 * before returning in the O_LOV_DELAY_CREATE case and dropping it here
182 * or in ll_file_release(), but I'm not sure that is desirable/necessary.
184 int ll_file_open(struct inode *inode, struct file *file)
186 struct ll_inode_info *lli = ll_i2info(inode);
187 struct lookup_intent *it;
188 struct lov_stripe_md *lsm;
189 struct ptlrpc_request *req;
193 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
194 inode->i_generation, inode);
196 /* don't do anything for / */
197 if (inode->i_sb->s_root == file->f_dentry)
202 if (!it->d.lustre.it_disposition) {
203 struct lookup_intent oit = { .it_op = IT_OPEN,
204 .it_flags = file->f_flags };
206 rc = ll_intent_file_open(file, NULL, 0, it);
211 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_OPEN);
212 rc = it_open_error(DISP_OPEN_OPEN, it);
216 rc = ll_local_open(file, it);
220 if (!S_ISREG(inode->i_mode))
225 if (file->f_flags & O_LOV_DELAY_CREATE ||
226 !(file->f_mode & FMODE_WRITE)) {
227 CDEBUG(D_INODE, "object creation was delayed\n");
231 file->f_flags &= ~O_LOV_DELAY_CREATE;
234 req = it->d.lustre.it_data;
235 ptlrpc_req_finished(req);
237 ll_open_complete(inode);
241 /* Fills the obdo with the attributes for the inode defined by lsm */
242 int ll_lsm_getattr(struct obd_export *exp, struct lov_stripe_md *lsm,
245 struct ptlrpc_request_set *set;
249 LASSERT(lsm != NULL);
251 memset(oa, 0, sizeof *oa);
252 oa->o_id = lsm->lsm_object_id;
253 oa->o_mode = S_IFREG;
254 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
255 OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
258 set = ptlrpc_prep_set();
260 CERROR ("ENOMEM allocing request set\n");
263 rc = obd_getattr_async(exp, oa, lsm, set);
265 rc = ptlrpc_set_wait(set);
266 ptlrpc_set_destroy(set);
271 oa->o_valid &= (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
272 OBD_MD_FLCTIME | OBD_MD_FLSIZE);
276 static inline void ll_remove_suid(struct inode *inode)
280 /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
281 mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
283 /* was any of the uid bits set? */
284 mode &= inode->i_mode;
285 if (mode && !capable(CAP_FSETID)) {
286 inode->i_mode &= ~mode;
287 // XXX careful here - we cannot change the size
291 /* Flush the page cache for an extent as its canceled. No one can dirty the
292 * extent until we've finished our work and they can enqueue another lock.
293 * The DLM protects us from ll_file_read/write here, but other kernel actors
294 * could have pages locked */
295 void ll_pgcache_remove_extent(struct inode *inode, struct lov_stripe_md *lsm,
296 struct ldlm_lock *lock)
298 struct ldlm_extent *extent = &lock->l_policy_data.l_extent;
299 struct obd_export *exp = ll_i2obdexp(inode);
300 struct ll_inode_info *lli = ll_i2info(inode);
301 unsigned long start, end, i;
303 int rc, discard = lock->l_flags & LDLM_FL_DISCARD_DATA;
306 CDEBUG(D_INODE, "obdo %lu inode %p ["LPU64"->"LPU64"] size: %llu\n",
307 inode->i_ino, inode, extent->start, extent->end, inode->i_size);
309 start = extent->start >> PAGE_CACHE_SHIFT;
310 end = (extent->end >> PAGE_CACHE_SHIFT) + 1;
311 if ((end << PAGE_CACHE_SHIFT) < extent->end)
314 i = (inode->i_size + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
316 clear_bit(LLI_F_HAVE_OST_SIZE_LOCK,
317 &(ll_i2info(inode)->lli_flags));
321 CDEBUG(D_INODE, "walking page indices start: %lu end: %lu\n", start,
324 for (i = start; i < end; i++) {
325 ll_pgcache_lock(inode->i_mapping);
326 if (list_empty(&inode->i_mapping->dirty_pages) &&
327 list_empty(&inode->i_mapping->clean_pages) &&
328 list_empty(&inode->i_mapping->locked_pages)) {
329 CDEBUG(D_INODE, "nothing left\n");
330 ll_pgcache_unlock(inode->i_mapping);
333 ll_pgcache_unlock(inode->i_mapping);
335 conditional_schedule();
337 page = find_get_page(inode->i_mapping, i);
341 LL_CDEBUG_PAGE(page, "locking\n");
344 /* page->mapping to check with racing against teardown */
345 if (page->mapping && PageDirty(page) && !discard) {
346 ClearPageDirty(page);
347 LL_CDEBUG_PAGE(page, "found dirty\n");
348 ll_pgcache_lock(inode->i_mapping);
349 list_del(&page->list);
350 list_add(&page->list, &inode->i_mapping->locked_pages);
351 ll_pgcache_unlock(inode->i_mapping);
353 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
354 rc = inode->i_mapping->a_ops->writepage(page);
356 rc = inode->i_mapping->a_ops->writepage(page, NULL);
359 CERROR("writepage of page %p failed: %d\n",
362 lock_page(page); /* wait for io to complete */
366 /* checking again to account for writeback's lock_page() */
367 if (page->mapping != NULL) {
368 LL_CDEBUG_PAGE(page, "truncating\n");
369 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
370 truncate_complete_page(page);
372 truncate_complete_page(page->mapping, page);
376 page_cache_release(page);
379 if (test_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags)) {
380 rc = obd_lock_contains(exp, lsm, lock, inode->i_size - 1);
383 CERROR("obd_lock_contains: rc = %d\n", rc);
384 clear_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags);
391 static int ll_extent_lock_callback(struct ldlm_lock *lock,
392 struct ldlm_lock_desc *new, void *data,
395 struct lustre_handle lockh = { 0 };
400 if ((unsigned long)data > 0 && (unsigned long)data < 0x1000) {
401 LDLM_ERROR(lock, "cancelling lock with bad data %p", data);
406 case LDLM_CB_BLOCKING:
407 ldlm_lock2handle(lock, &lockh);
408 rc = ldlm_cli_cancel(&lockh);
410 CERROR("ldlm_cli_cancel failed: %d\n", rc);
412 case LDLM_CB_CANCELING: {
413 struct inode *inode = ll_inode_from_lock(lock);
414 struct ll_inode_info *lli;
418 lli= ll_i2info(inode);
424 ll_pgcache_remove_extent(inode, lli->lli_smd, lock);
425 //ll_try_done_writing(inode);
437 * some callers, notably truncate, really don't want i_size set based
438 * on the the size returned by the getattr, or lock acquisition in
441 int ll_extent_lock_no_validate(struct ll_file_data *fd, struct inode *inode,
442 struct lov_stripe_md *lsm,
443 int mode, struct ldlm_extent *extent,
444 struct lustre_handle *lockh, int ast_flags)
446 struct ll_sb_info *sbi = ll_i2sbi(inode);
450 LASSERT(lockh->cookie == 0);
452 /* XXX phil: can we do this? won't it screw the file size up? */
453 if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
454 (sbi->ll_flags & LL_SBI_NOLCK))
457 CDEBUG(D_DLMTRACE, "Locking inode %lu, start "LPU64" end "LPU64"\n",
458 inode->i_ino, extent->start, extent->end);
460 rc = obd_enqueue(sbi->ll_osc_exp, lsm, NULL, LDLM_EXTENT, extent,
461 sizeof(extent), mode, &ast_flags,
462 ll_extent_lock_callback, inode, lockh);
469 * this grabs a lock and manually implements behaviour that makes it look like
470 * the OST is returning the file size with each lock acquisition.
472 int ll_extent_lock(struct ll_file_data *fd, struct inode *inode,
473 struct lov_stripe_md *lsm, int mode,
474 struct ldlm_extent *extent, struct lustre_handle *lockh)
476 struct ll_inode_info *lli = ll_i2info(inode);
477 struct obd_export *exp = ll_i2obdexp(inode);
478 struct ldlm_extent size_lock;
479 struct lustre_handle match_lockh = {0};
481 obd_flag refresh_valid;
482 int flags, rc, matched;
485 rc = ll_extent_lock_no_validate(fd, inode, lsm, mode, extent, lockh, 0);
489 if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
492 rc = ll_lsm_getattr(exp, lsm, &oa);
496 /* We set this flag in commit write as we extend the file size. When
497 * the bit is set and the lock is canceled that covers the file size,
498 * we clear the bit. This is enough to protect the window where our
499 * local size extension is needed for writeback. However, it relies on
500 * behaviour that won't be true in the near future. This assumes that
501 * all getattr callers get extent locks, which they currnetly do. It
502 * also assumes that we only send discarding asts for {0,eof} truncates
503 * as is currently the case. This will have to be replaced by the
504 * proper eoc communication between clients and the ost, which is on
506 refresh_valid = (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
507 OBD_MD_FLCTIME | OBD_MD_FLSIZE);
508 if (test_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags)) {
509 if (oa.o_size < inode->i_size)
510 refresh_valid &= ~OBD_MD_FLSIZE;
512 clear_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags);
514 obdo_refresh_inode(inode, &oa, refresh_valid);
516 CDEBUG(D_INODE, "objid "LPX64" size %Lu, blocks %lu, blksize %lu\n",
517 lsm->lsm_object_id, inode->i_size, inode->i_blocks,
520 size_lock.start = inode->i_size;
521 size_lock.end = OBD_OBJECT_EOF;
523 /* XXX I bet we should be checking the lock ignore flags.. */
524 flags = LDLM_FL_CBPENDING | LDLM_FL_BLOCK_GRANTED;
525 matched = obd_match(exp, lsm, LDLM_EXTENT, &size_lock,
526 sizeof(size_lock), LCK_PR, &flags, inode,
529 GOTO(out, rc = matched);
531 /* hey, alright, we hold a size lock that covers the size we
532 * just found, its not going to change for a while.. */
534 set_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags);
535 obd_cancel(exp, lsm, LCK_PR, &match_lockh);
541 ll_extent_unlock(fd, inode, lsm, mode, lockh);
545 int ll_extent_unlock(struct ll_file_data *fd, struct inode *inode,
546 struct lov_stripe_md *lsm, int mode,
547 struct lustre_handle *lockh)
549 struct ll_sb_info *sbi = ll_i2sbi(inode);
553 /* XXX phil: can we do this? won't it screw the file size up? */
554 if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
555 (sbi->ll_flags & LL_SBI_NOLCK))
558 rc = obd_cancel(sbi->ll_osc_exp, lsm, mode, lockh);
563 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
566 struct ll_file_data *fd = filp->private_data;
567 struct inode *inode = filp->f_dentry->d_inode;
568 struct ll_inode_info *lli = ll_i2info(inode);
569 struct lov_stripe_md *lsm = lli->lli_smd;
570 struct lustre_handle lockh = { 0 };
571 struct ldlm_extent extent;
575 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
576 inode->i_ino, inode->i_generation, inode, count, *ppos);
578 /* "If nbyte is 0, read() will return 0 and have no other results."
579 * -- Single Unix Spec */
583 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_READ_BYTES,
589 /* grab a -> eof extent to push extending writes out of node's caches
590 * so we can see them at the getattr after lock acquisition. this will
591 * turn into a seperate [*ppos + count, EOF] 'size intent' lock attempt
593 extent.start = *ppos;
594 extent.end = OBD_OBJECT_EOF;
596 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);
604 /* turn off the kernel's read-ahead */
605 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
608 filp->f_ra.ra_pages = 0;
610 retval = generic_file_read(filp, buf, count, ppos);
613 ll_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
618 * Write to a file (through the page cache).
620 static ssize_t ll_file_write(struct file *file, const char *buf, size_t count,
623 struct ll_file_data *fd = file->private_data;
624 struct inode *inode = file->f_dentry->d_inode;
625 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
626 struct lustre_handle lockh = { 0 };
627 struct ldlm_extent extent;
628 loff_t maxbytes = ll_file_maxbytes(inode);
631 char should_validate = 1;
633 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
634 inode->i_ino, inode->i_generation, inode, count, *ppos);
636 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
638 /* POSIX, but surprised the VFS doesn't check this already */
644 if (file->f_flags & O_APPEND) {
646 extent.end = OBD_OBJECT_EOF;
648 extent.start = *ppos;
649 extent.end = *ppos + count - 1;
650 /* we really don't care what i_size is if we're doing
651 * fully page aligned writes */
652 if ((*ppos & ~PAGE_CACHE_MASK) == 0 &&
653 (count & ~PAGE_CACHE_MASK) == 0)
658 err = ll_extent_lock(fd, inode, lsm, LCK_PW, &extent, &lockh);
660 err = ll_extent_lock_no_validate(fd, inode, lsm, LCK_PW,
665 /* this is ok, g_f_w will overwrite this under i_sem if it races
666 * with a local truncate, it just makes our maxbyte checking easier */
667 if (file->f_flags & O_APPEND)
668 *ppos = inode->i_size;
670 if (*ppos >= maxbytes) {
671 if (count || *ppos > maxbytes) {
672 send_sig(SIGXFSZ, current, 0);
673 GOTO(out, retval = -EFBIG);
676 if (*ppos + count > maxbytes)
677 count = maxbytes - *ppos;
679 CDEBUG(D_INFO, "Writing inode %lu, "LPSZ" bytes, offset %Lu\n",
680 inode->i_ino, count, *ppos);
682 /* generic_file_write handles O_APPEND after getting i_sem */
683 retval = generic_file_write(file, buf, count, ppos);
687 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_WRITE_BYTES,
689 ll_extent_unlock(fd, inode, lsm, LCK_PW, &lockh);
693 static int ll_lov_setstripe(struct inode *inode, struct file *file,
696 struct ll_inode_info *lli = ll_i2info(inode);
698 struct obd_export *exp = ll_i2obdexp(inode);
699 struct lov_stripe_md *lsm;
700 struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = FMODE_WRITE};
701 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
702 struct ptlrpc_request *req = NULL;
707 /* Bug 1152: copy properly when this is no longer true */
708 LASSERT(sizeof(lum) == sizeof(*lump));
709 LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
710 rc = copy_from_user(&lum, lump, sizeof(lum));
714 down(&lli->lli_open_sem);
717 up(&lli->lli_open_sem);
718 CDEBUG(D_IOCTL, "stripe already exists for ino %lu\n",
723 f = get_empty_filp();
727 f->f_dentry = file->f_dentry;
728 f->f_vfsmnt = file->f_vfsmnt;
730 rc = ll_intent_file_open(f, &lum, sizeof(lum), &oit);
733 if (it_disposition(&oit, DISP_LOOKUP_NEG))
735 req = oit.d.lustre.it_data;
736 rc = oit.d.lustre.it_status;
741 rc = mdc_req2lustre_md(req, 1, exp, &md);
744 ll_update_inode(f->f_dentry->d_inode, md.body, md.lsm);
746 rc = ll_local_open(f, &oit);
749 ll_intent_release(&oit);
751 rc = ll_file_release(f->f_dentry->d_inode, f);
756 up(&lli->lli_open_sem);
758 ptlrpc_req_finished(req);
762 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
764 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
769 return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2obdexp(inode), 0, lsm,
773 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
776 struct ll_file_data *fd = file->private_data;
780 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),cmd=%x\n", inode->i_ino,
781 inode->i_generation, inode, cmd);
783 if (_IOC_TYPE(cmd) == 'T') /* tty ioctls */
786 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
788 case LL_IOC_GETFLAGS:
789 /* Get the current value of the file flags */
790 return put_user(fd->fd_flags, (int *)arg);
791 case LL_IOC_SETFLAGS:
792 case LL_IOC_CLRFLAGS:
793 /* Set or clear specific file flags */
794 /* XXX This probably needs checks to ensure the flags are
795 * not abused, and to handle any flag side effects.
797 if (get_user(flags, (int *) arg))
800 if (cmd == LL_IOC_SETFLAGS)
801 fd->fd_flags |= flags;
803 fd->fd_flags &= ~flags;
805 case LL_IOC_LOV_SETSTRIPE:
806 RETURN(ll_lov_setstripe(inode, file, arg));
807 case LL_IOC_LOV_GETSTRIPE:
808 RETURN(ll_lov_getstripe(inode, arg));
809 case EXT3_IOC_GETFLAGS:
810 case EXT3_IOC_SETFLAGS:
811 RETURN( ll_iocontrol(inode, file, cmd, arg) );
812 /* We need to special case any other ioctls we want to handle,
813 * to send them to the MDS/OST as appropriate and to properly
814 * network encode the arg field.
815 case EXT2_IOC_GETVERSION_OLD:
816 case EXT2_IOC_GETVERSION_NEW:
817 case EXT2_IOC_SETVERSION_OLD:
818 case EXT2_IOC_SETVERSION_NEW:
821 RETURN( obd_iocontrol(cmd, ll_i2obdexp(inode), 0, NULL,
826 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
828 struct inode *inode = file->f_dentry->d_inode;
829 struct ll_file_data *fd = file->private_data;
830 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
831 struct lustre_handle lockh = {0};
834 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),to=%llu\n", inode->i_ino,
835 inode->i_generation, inode,
836 offset + ((origin==2) ? inode->i_size : file->f_pos));
838 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_LLSEEK);
839 if (origin == 2) { /* SEEK_END */
841 struct ldlm_extent extent = {0, OBD_OBJECT_EOF};
842 err = ll_extent_lock(fd, inode, lsm, LCK_PR, &extent, &lockh);
846 offset += inode->i_size;
847 } else if (origin == 1) { /* SEEK_CUR */
848 offset += file->f_pos;
852 if (offset >= 0 && offset <= ll_file_maxbytes(inode)) {
853 if (offset != file->f_pos) {
854 file->f_pos = offset;
855 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
857 file->f_version = ++event;
864 ll_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
868 int ll_fsync(struct file *file, struct dentry *dentry, int data)
870 struct inode *inode = dentry->d_inode;
871 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
873 struct ptlrpc_request *req;
876 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
877 inode->i_generation, inode);
879 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_FSYNC);
881 /* fsync's caller has already called _fdata{sync,write}, we want
882 * that IO to finish before calling the osc and mdc sync methods */
883 rc = filemap_fdatawait(inode->i_mapping);
885 ll_inode2fid(&fid, inode);
886 err = mdc_sync(ll_i2sbi(inode)->ll_mdc_exp, &fid, &req);
890 ptlrpc_req_finished(req);
893 struct obdo *oa = obdo_alloc();
896 RETURN(rc ? rc : -ENOMEM);
898 oa->o_id = lsm->lsm_object_id;
899 oa->o_valid = OBD_MD_FLID;
900 obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
901 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
903 err = obd_sync(ll_i2sbi(inode)->ll_osc_exp, oa, lsm,
913 int ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
915 struct inode *inode = file->f_dentry->d_inode;
916 struct ll_sb_info *sbi = ll_i2sbi(inode);
917 struct obd_device *obddev;
918 struct ldlm_res_id res_id =
919 { .name = {inode->i_ino, inode->i_generation, LDLM_FLOCK} };
920 struct lustre_handle lockh = {0};
921 struct ldlm_flock flock;
922 ldlm_mode_t mode = 0;
927 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu file_lock=%p\n",
928 inode->i_ino, file_lock);
930 flock.pid = file_lock->fl_pid;
931 flock.start = file_lock->fl_start;
932 flock.end = file_lock->fl_end;
934 switch (file_lock->fl_type) {
939 /* An unlock request may or may not have any relation to
940 * existing locks so we may not be able to pass a lock handle
941 * via a normal ldlm_lock_cancel() request. The request may even
942 * unlock a byte range in the middle of an existing lock. In
943 * order to process an unlock request we need all of the same
944 * information that is given with a normal read or write record
945 * lock request. To avoid creating another ldlm unlock (cancel)
946 * message we'll treat a LCK_NL flock request as an unlock. */
953 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
962 flags = LDLM_FL_BLOCK_NOWAIT;
965 flags = LDLM_FL_TEST_LOCK;
966 /* Save the old mode so that if the mode in the lock changes we
967 * can decrement the appropriate reader or writer refcount. */
968 file_lock->fl_type = mode;
971 CERROR("unknown fcntl lock command: %d\n", cmd);
975 CDEBUG(D_DLMTRACE, "inode=%lu, pid=%u, flags=%#x, mode=%u, "
976 "start="LPU64", end="LPU64"\n", inode->i_ino, flock.pid,
977 flags, mode, flock.start, flock.end);
979 obddev = sbi->ll_mdc_exp->exp_obd;
980 rc = ldlm_cli_enqueue(sbi->ll_mdc_exp, NULL, obddev->obd_namespace,
981 NULL, res_id, LDLM_FLOCK, &flock, sizeof(flock),
982 mode, &flags, ldlm_flock_completion_ast, NULL,
987 static int ll_have_md_lock(struct dentry *de)
989 struct ll_sb_info *sbi = ll_s2sbi(de->d_sb);
990 struct lustre_handle lockh;
991 struct ldlm_res_id res_id = { .name = {0} };
992 struct obd_device *obddev;
999 obddev = sbi->ll_mdc_exp->exp_obd;
1000 res_id.name[0] = de->d_inode->i_ino;
1001 res_id.name[1] = de->d_inode->i_generation;
1003 CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
1005 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
1006 if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_PLAIN,
1007 NULL, 0, LCK_PR, &lockh)) {
1008 ldlm_lock_decref(&lockh, LCK_PR);
1012 if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_PLAIN,
1013 NULL, 0, LCK_PW, &lockh)) {
1014 ldlm_lock_decref(&lockh, LCK_PW);
1020 int ll_inode_revalidate_it(struct dentry *dentry, struct lookup_intent *it)
1022 struct inode *inode = dentry->d_inode;
1023 struct lov_stripe_md *lsm;
1027 CERROR("REPORT THIS LINE TO PETER\n");
1030 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),name=%s\n",
1031 inode->i_ino, inode->i_generation, inode, dentry->d_name.name);
1032 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
1033 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_REVALIDATE);
1036 if (!ll_have_md_lock(dentry)) {
1037 struct ptlrpc_request *req = NULL;
1038 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
1040 unsigned long valid = 0;
1043 if (S_ISREG(inode->i_mode)) {
1044 ealen = obd_size_diskmd(sbi->ll_osc_exp, NULL);
1045 valid |= OBD_MD_FLEASIZE;
1047 ll_inode2fid(&fid, inode);
1048 rc = mdc_getattr(sbi->ll_mdc_exp, &fid, valid, ealen, &req);
1050 CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1053 rc = ll_prep_inode(sbi->ll_osc_exp, &inode, req, 0, NULL);
1055 ptlrpc_req_finished(req);
1058 ptlrpc_req_finished(req);
1062 if (ll_have_md_lock(dentry) &&
1063 test_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &ll_i2info(inode)->lli_flags))
1067 lsm = ll_i2info(inode)->lli_smd;
1068 if (!lsm) /* object not yet allocated, don't validate size */
1071 /* unfortunately stat comes in through revalidate and we don't
1072 * differentiate this use from initial instantiation. we're
1073 * also being wildly conservative and flushing write caches
1074 * so that stat really returns the proper size. */
1076 struct ldlm_extent extent = {0, OBD_OBJECT_EOF};
1077 struct lustre_handle lockh = {0};
1080 err = ll_extent_lock(NULL, inode, lsm, LCK_PR, &extent, &lockh);
1081 if (err != ELDLM_OK)
1084 ll_extent_unlock(NULL, inode, lsm, LCK_PR, &lockh);
1089 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1090 int ll_getattr(struct vfsmount *mnt, struct dentry *de,
1091 struct lookup_intent *it, struct kstat *stat)
1094 struct inode *inode = de->d_inode;
1096 res = ll_inode_revalidate_it(de, it);
1097 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_GETATTR);
1102 stat->dev = inode->i_sb->s_dev;
1103 stat->ino = inode->i_ino;
1104 stat->mode = inode->i_mode;
1105 stat->nlink = inode->i_nlink;
1106 stat->uid = inode->i_uid;
1107 stat->gid = inode->i_gid;
1108 stat->rdev = kdev_t_to_nr(inode->i_rdev);
1109 stat->atime = inode->i_atime;
1110 stat->mtime = inode->i_mtime;
1111 stat->ctime = inode->i_ctime;
1112 stat->size = inode->i_size;
1113 stat->blksize = inode->i_blksize;
1114 stat->blocks = inode->i_blocks;
1119 struct file_operations ll_file_operations = {
1121 write: ll_file_write,
1122 ioctl: ll_file_ioctl,
1124 release: ll_file_release,
1125 mmap: generic_file_mmap,
1126 llseek: ll_file_seek,
1128 //lock: ll_file_flock
1131 struct inode_operations ll_file_inode_operations = {
1132 setattr_raw: ll_setattr_raw,
1133 setattr: ll_setattr,
1134 truncate: ll_truncate,
1135 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1136 getattr_it: ll_getattr,
1138 revalidate_it: ll_inode_revalidate_it,
1142 struct inode_operations ll_special_inode_operations = {
1143 setattr_raw: ll_setattr_raw,
1144 setattr: ll_setattr,
1145 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1146 getattr_it: ll_getattr,
1148 revalidate_it: ll_inode_revalidate_it,