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;
162 ll_readahead_init(&fd->fd_ras);
164 lli->lli_io_epoch = body->io_epoch;
166 mdc_set_open_replay_data(&fd->fd_mds_och, it->d.lustre.it_data);
171 /* Open a file, and (for the very first open) create objects on the OSTs at
172 * this time. If opened with O_LOV_DELAY_CREATE, then we don't do the object
173 * creation or open until ll_lov_setstripe() ioctl is called. We grab
174 * lli_open_sem to ensure no other process will create objects, send the
175 * stripe MD to the MDS, or try to destroy the objects if that fails.
177 * If we already have the stripe MD locally then we don't request it in
178 * mdc_open(), by passing a lmm_size = 0.
180 * It is up to the application to ensure no other processes open this file
181 * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
182 * used. We might be able to avoid races of that sort by getting lli_open_sem
183 * before returning in the O_LOV_DELAY_CREATE case and dropping it here
184 * or in ll_file_release(), but I'm not sure that is desirable/necessary.
186 int ll_file_open(struct inode *inode, struct file *file)
188 struct ll_inode_info *lli = ll_i2info(inode);
189 struct lookup_intent *it;
190 struct lov_stripe_md *lsm;
191 struct ptlrpc_request *req;
195 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
196 inode->i_generation, inode);
198 /* don't do anything for / */
199 if (inode->i_sb->s_root == file->f_dentry)
204 if (!it->d.lustre.it_disposition) {
205 struct lookup_intent oit = { .it_op = IT_OPEN,
206 .it_flags = file->f_flags };
208 rc = ll_intent_file_open(file, NULL, 0, it);
213 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_OPEN);
214 rc = it_open_error(DISP_OPEN_OPEN, it);
218 rc = ll_local_open(file, it);
222 if (!S_ISREG(inode->i_mode))
227 if (file->f_flags & O_LOV_DELAY_CREATE ||
228 !(file->f_mode & FMODE_WRITE)) {
229 CDEBUG(D_INODE, "object creation was delayed\n");
233 file->f_flags &= ~O_LOV_DELAY_CREATE;
236 req = it->d.lustre.it_data;
237 ptlrpc_req_finished(req);
239 ll_open_complete(inode);
243 /* Fills the obdo with the attributes for the inode defined by lsm */
244 int ll_lsm_getattr(struct obd_export *exp, struct lov_stripe_md *lsm,
247 struct ptlrpc_request_set *set;
251 LASSERT(lsm != NULL);
253 memset(oa, 0, sizeof *oa);
254 oa->o_id = lsm->lsm_object_id;
255 oa->o_mode = S_IFREG;
256 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
257 OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
260 set = ptlrpc_prep_set();
262 CERROR ("ENOMEM allocing request set\n");
265 rc = obd_getattr_async(exp, oa, lsm, set);
267 rc = ptlrpc_set_wait(set);
268 ptlrpc_set_destroy(set);
273 oa->o_valid &= (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
274 OBD_MD_FLCTIME | OBD_MD_FLSIZE);
278 static inline void ll_remove_suid(struct inode *inode)
282 /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
283 mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
285 /* was any of the uid bits set? */
286 mode &= inode->i_mode;
287 if (mode && !capable(CAP_FSETID)) {
288 inode->i_mode &= ~mode;
289 // XXX careful here - we cannot change the size
293 /* Flush the page cache for an extent as its canceled. No one can dirty the
294 * extent until we've finished our work and they can enqueue another lock.
295 * The DLM protects us from ll_file_read/write here, but other kernel actors
296 * could have pages locked */
297 void ll_pgcache_remove_extent(struct inode *inode, struct lov_stripe_md *lsm,
298 struct ldlm_lock *lock)
300 struct ldlm_extent *extent = &lock->l_policy_data.l_extent;
301 struct obd_export *exp = ll_i2obdexp(inode);
302 struct ll_inode_info *lli = ll_i2info(inode);
303 unsigned long start, end, i;
305 int rc, discard = lock->l_flags & LDLM_FL_DISCARD_DATA;
308 CDEBUG(D_INODE, "obdo %lu inode %p ["LPU64"->"LPU64"] size: %llu\n",
309 inode->i_ino, inode, extent->start, extent->end, inode->i_size);
311 start = extent->start >> PAGE_CACHE_SHIFT;
312 end = (extent->end >> PAGE_CACHE_SHIFT) + 1;
313 if ((end << PAGE_CACHE_SHIFT) < extent->end)
316 i = (inode->i_size + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
318 clear_bit(LLI_F_HAVE_OST_SIZE_LOCK,
319 &(ll_i2info(inode)->lli_flags));
323 CDEBUG(D_INODE, "walking page indices start: %lu end: %lu\n", start,
326 for (i = start; i < end; i++) {
327 ll_pgcache_lock(inode->i_mapping);
328 if (list_empty(&inode->i_mapping->dirty_pages) &&
329 list_empty(&inode->i_mapping->clean_pages) &&
330 list_empty(&inode->i_mapping->locked_pages)) {
331 CDEBUG(D_INODE, "nothing left\n");
332 ll_pgcache_unlock(inode->i_mapping);
335 ll_pgcache_unlock(inode->i_mapping);
337 conditional_schedule();
339 page = find_get_page(inode->i_mapping, i);
343 LL_CDEBUG_PAGE(page, "locking\n");
346 /* page->mapping to check with racing against teardown */
347 if (page->mapping && PageDirty(page) && !discard) {
348 ClearPageDirty(page);
349 LL_CDEBUG_PAGE(page, "found dirty\n");
350 ll_pgcache_lock(inode->i_mapping);
351 list_del(&page->list);
352 list_add(&page->list, &inode->i_mapping->locked_pages);
353 ll_pgcache_unlock(inode->i_mapping);
355 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
356 rc = inode->i_mapping->a_ops->writepage(page);
358 rc = inode->i_mapping->a_ops->writepage(page, NULL);
361 CERROR("writepage of page %p failed: %d\n",
364 lock_page(page); /* wait for io to complete */
368 /* checking again to account for writeback's lock_page() */
369 if (page->mapping != NULL) {
370 LL_CDEBUG_PAGE(page, "truncating\n");
371 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
372 truncate_complete_page(page);
374 truncate_complete_page(page->mapping, page);
378 page_cache_release(page);
381 if (test_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags)) {
382 rc = obd_lock_contains(exp, lsm, lock, inode->i_size - 1);
385 CERROR("obd_lock_contains: rc = %d\n", rc);
386 clear_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags);
393 static int ll_extent_lock_callback(struct ldlm_lock *lock,
394 struct ldlm_lock_desc *new, void *data,
397 struct lustre_handle lockh = { 0 };
402 if ((unsigned long)data > 0 && (unsigned long)data < 0x1000) {
403 LDLM_ERROR(lock, "cancelling lock with bad data %p", data);
408 case LDLM_CB_BLOCKING:
409 ldlm_lock2handle(lock, &lockh);
410 rc = ldlm_cli_cancel(&lockh);
412 CERROR("ldlm_cli_cancel failed: %d\n", rc);
414 case LDLM_CB_CANCELING: {
415 struct inode *inode = ll_inode_from_lock(lock);
416 struct ll_inode_info *lli;
420 lli= ll_i2info(inode);
426 ll_pgcache_remove_extent(inode, lli->lli_smd, lock);
427 //ll_try_done_writing(inode);
439 * some callers, notably truncate, really don't want i_size set based
440 * on the the size returned by the getattr, or lock acquisition in
443 int ll_extent_lock_no_validate(struct ll_file_data *fd, struct inode *inode,
444 struct lov_stripe_md *lsm,
445 int mode, struct ldlm_extent *extent,
446 struct lustre_handle *lockh, int ast_flags)
448 struct ll_sb_info *sbi = ll_i2sbi(inode);
452 LASSERT(lockh->cookie == 0);
454 /* XXX phil: can we do this? won't it screw the file size up? */
455 if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
456 (sbi->ll_flags & LL_SBI_NOLCK))
459 CDEBUG(D_DLMTRACE, "Locking inode %lu, start "LPU64" end "LPU64"\n",
460 inode->i_ino, extent->start, extent->end);
462 rc = obd_enqueue(sbi->ll_osc_exp, lsm, NULL, LDLM_EXTENT, extent,
463 sizeof(extent), mode, &ast_flags,
464 ll_extent_lock_callback, inode, lockh);
471 * this grabs a lock and manually implements behaviour that makes it look like
472 * the OST is returning the file size with each lock acquisition.
474 int ll_extent_lock(struct ll_file_data *fd, struct inode *inode,
475 struct lov_stripe_md *lsm, int mode,
476 struct ldlm_extent *extent, struct lustre_handle *lockh)
478 struct ll_inode_info *lli = ll_i2info(inode);
479 struct obd_export *exp = ll_i2obdexp(inode);
480 struct ldlm_extent size_lock;
481 struct lustre_handle match_lockh = {0};
483 obd_flag refresh_valid;
484 int flags, rc, matched;
487 rc = ll_extent_lock_no_validate(fd, inode, lsm, mode, extent, lockh, 0);
491 if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
494 rc = ll_lsm_getattr(exp, lsm, &oa);
498 /* We set this flag in commit write as we extend the file size. When
499 * the bit is set and the lock is canceled that covers the file size,
500 * we clear the bit. This is enough to protect the window where our
501 * local size extension is needed for writeback. However, it relies on
502 * behaviour that won't be true in the near future. This assumes that
503 * all getattr callers get extent locks, which they currnetly do. It
504 * also assumes that we only send discarding asts for {0,eof} truncates
505 * as is currently the case. This will have to be replaced by the
506 * proper eoc communication between clients and the ost, which is on
508 refresh_valid = (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
509 OBD_MD_FLCTIME | OBD_MD_FLSIZE);
510 if (test_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags)) {
511 if (oa.o_size < inode->i_size)
512 refresh_valid &= ~OBD_MD_FLSIZE;
514 clear_bit(LLI_F_PREFER_EXTENDED_SIZE, &lli->lli_flags);
516 obdo_refresh_inode(inode, &oa, refresh_valid);
518 CDEBUG(D_INODE, "objid "LPX64" size %Lu, blocks %lu, blksize %lu\n",
519 lsm->lsm_object_id, inode->i_size, inode->i_blocks,
522 size_lock.start = inode->i_size;
523 size_lock.end = OBD_OBJECT_EOF;
525 /* XXX I bet we should be checking the lock ignore flags.. */
526 flags = LDLM_FL_CBPENDING | LDLM_FL_BLOCK_GRANTED;
527 matched = obd_match(exp, lsm, LDLM_EXTENT, &size_lock,
528 sizeof(size_lock), LCK_PR, &flags, inode,
531 GOTO(out, rc = matched);
533 /* hey, alright, we hold a size lock that covers the size we
534 * just found, its not going to change for a while.. */
536 set_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags);
537 obd_cancel(exp, lsm, LCK_PR, &match_lockh);
543 ll_extent_unlock(fd, inode, lsm, mode, lockh);
547 int ll_extent_unlock(struct ll_file_data *fd, struct inode *inode,
548 struct lov_stripe_md *lsm, int mode,
549 struct lustre_handle *lockh)
551 struct ll_sb_info *sbi = ll_i2sbi(inode);
555 /* XXX phil: can we do this? won't it screw the file size up? */
556 if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
557 (sbi->ll_flags & LL_SBI_NOLCK))
560 rc = obd_cancel(sbi->ll_osc_exp, lsm, mode, lockh);
565 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
568 struct ll_file_data *fd = filp->private_data;
569 struct inode *inode = filp->f_dentry->d_inode;
570 struct ll_inode_info *lli = ll_i2info(inode);
571 struct lov_stripe_md *lsm = lli->lli_smd;
572 struct lustre_handle lockh = { 0 };
573 struct ldlm_extent extent;
577 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
578 inode->i_ino, inode->i_generation, inode, count, *ppos);
580 /* "If nbyte is 0, read() will return 0 and have no other results."
581 * -- Single Unix Spec */
585 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_READ_BYTES,
591 /* grab a -> eof extent to push extending writes out of node's caches
592 * so we can see them at the getattr after lock acquisition. this will
593 * turn into a seperate [*ppos + count, EOF] 'size intent' lock attempt
595 extent.start = *ppos;
596 extent.end = OBD_OBJECT_EOF;
598 err = ll_extent_lock(fd, inode, lsm, LCK_PR, &extent, &lockh);
603 CDEBUG(D_INFO, "Reading inode %lu, "LPSZ" bytes, offset %Ld\n",
604 inode->i_ino, count, *ppos);
606 /* turn off the kernel's read-ahead */
607 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
610 filp->f_ra.ra_pages = 0;
612 retval = generic_file_read(filp, buf, count, ppos);
615 ll_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
620 * Write to a file (through the page cache).
622 static ssize_t ll_file_write(struct file *file, const char *buf, size_t count,
625 struct ll_file_data *fd = file->private_data;
626 struct inode *inode = file->f_dentry->d_inode;
627 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
628 struct lustre_handle lockh = { 0 };
629 struct ldlm_extent extent;
630 loff_t maxbytes = ll_file_maxbytes(inode);
633 char should_validate = 1;
635 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
636 inode->i_ino, inode->i_generation, inode, count, *ppos);
638 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
640 /* POSIX, but surprised the VFS doesn't check this already */
646 if (file->f_flags & O_APPEND) {
648 extent.end = OBD_OBJECT_EOF;
650 extent.start = *ppos;
651 extent.end = *ppos + count - 1;
652 /* we really don't care what i_size is if we're doing
653 * fully page aligned writes */
654 if ((*ppos & ~PAGE_CACHE_MASK) == 0 &&
655 (count & ~PAGE_CACHE_MASK) == 0)
660 err = ll_extent_lock(fd, inode, lsm, LCK_PW, &extent, &lockh);
662 err = ll_extent_lock_no_validate(fd, inode, lsm, LCK_PW,
667 /* this is ok, g_f_w will overwrite this under i_sem if it races
668 * with a local truncate, it just makes our maxbyte checking easier */
669 if (file->f_flags & O_APPEND)
670 *ppos = inode->i_size;
672 if (*ppos >= maxbytes) {
673 if (count || *ppos > maxbytes) {
674 send_sig(SIGXFSZ, current, 0);
675 GOTO(out, retval = -EFBIG);
678 if (*ppos + count > maxbytes)
679 count = maxbytes - *ppos;
681 CDEBUG(D_INFO, "Writing inode %lu, "LPSZ" bytes, offset %Lu\n",
682 inode->i_ino, count, *ppos);
684 /* generic_file_write handles O_APPEND after getting i_sem */
685 retval = generic_file_write(file, buf, count, ppos);
689 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_WRITE_BYTES,
691 ll_extent_unlock(fd, inode, lsm, LCK_PW, &lockh);
695 static int ll_lov_setstripe(struct inode *inode, struct file *file,
698 struct ll_inode_info *lli = ll_i2info(inode);
700 struct obd_export *exp = ll_i2obdexp(inode);
701 struct lov_stripe_md *lsm;
702 struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = FMODE_WRITE};
703 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
704 struct ptlrpc_request *req = NULL;
709 /* Bug 1152: copy properly when this is no longer true */
710 LASSERT(sizeof(lum) == sizeof(*lump));
711 LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
712 rc = copy_from_user(&lum, lump, sizeof(lum));
716 down(&lli->lli_open_sem);
719 up(&lli->lli_open_sem);
720 CDEBUG(D_IOCTL, "stripe already exists for ino %lu\n",
725 f = get_empty_filp();
729 f->f_dentry = file->f_dentry;
730 f->f_vfsmnt = file->f_vfsmnt;
732 rc = ll_intent_file_open(f, &lum, sizeof(lum), &oit);
735 if (it_disposition(&oit, DISP_LOOKUP_NEG))
737 req = oit.d.lustre.it_data;
738 rc = oit.d.lustre.it_status;
743 rc = mdc_req2lustre_md(req, 1, exp, &md);
746 ll_update_inode(f->f_dentry->d_inode, md.body, md.lsm);
748 rc = ll_local_open(f, &oit);
751 ll_intent_release(&oit);
753 rc = ll_file_release(f->f_dentry->d_inode, f);
758 up(&lli->lli_open_sem);
760 ptlrpc_req_finished(req);
764 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
766 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
771 return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2obdexp(inode), 0, lsm,
775 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
778 struct ll_file_data *fd = file->private_data;
782 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),cmd=%x\n", inode->i_ino,
783 inode->i_generation, inode, cmd);
785 if (_IOC_TYPE(cmd) == 'T') /* tty ioctls */
788 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
790 case LL_IOC_GETFLAGS:
791 /* Get the current value of the file flags */
792 return put_user(fd->fd_flags, (int *)arg);
793 case LL_IOC_SETFLAGS:
794 case LL_IOC_CLRFLAGS:
795 /* Set or clear specific file flags */
796 /* XXX This probably needs checks to ensure the flags are
797 * not abused, and to handle any flag side effects.
799 if (get_user(flags, (int *) arg))
802 if (cmd == LL_IOC_SETFLAGS)
803 fd->fd_flags |= flags;
805 fd->fd_flags &= ~flags;
807 case LL_IOC_LOV_SETSTRIPE:
808 RETURN(ll_lov_setstripe(inode, file, arg));
809 case LL_IOC_LOV_GETSTRIPE:
810 RETURN(ll_lov_getstripe(inode, arg));
811 case EXT3_IOC_GETFLAGS:
812 case EXT3_IOC_SETFLAGS:
813 RETURN( ll_iocontrol(inode, file, cmd, arg) );
814 /* We need to special case any other ioctls we want to handle,
815 * to send them to the MDS/OST as appropriate and to properly
816 * network encode the arg field.
817 case EXT2_IOC_GETVERSION_OLD:
818 case EXT2_IOC_GETVERSION_NEW:
819 case EXT2_IOC_SETVERSION_OLD:
820 case EXT2_IOC_SETVERSION_NEW:
823 RETURN( obd_iocontrol(cmd, ll_i2obdexp(inode), 0, NULL,
828 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
830 struct inode *inode = file->f_dentry->d_inode;
831 struct ll_file_data *fd = file->private_data;
832 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
833 struct lustre_handle lockh = {0};
836 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),to=%llu\n", inode->i_ino,
837 inode->i_generation, inode,
838 offset + ((origin==2) ? inode->i_size : file->f_pos));
840 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_LLSEEK);
841 if (origin == 2) { /* SEEK_END */
843 struct ldlm_extent extent = {0, OBD_OBJECT_EOF};
844 err = ll_extent_lock(fd, inode, lsm, LCK_PR, &extent, &lockh);
848 offset += inode->i_size;
849 } else if (origin == 1) { /* SEEK_CUR */
850 offset += file->f_pos;
854 if (offset >= 0 && offset <= ll_file_maxbytes(inode)) {
855 if (offset != file->f_pos) {
856 file->f_pos = offset;
857 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
859 file->f_version = ++event;
866 ll_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
870 int ll_fsync(struct file *file, struct dentry *dentry, int data)
872 struct inode *inode = dentry->d_inode;
873 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
875 struct ptlrpc_request *req;
878 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
879 inode->i_generation, inode);
881 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_FSYNC);
883 /* fsync's caller has already called _fdata{sync,write}, we want
884 * that IO to finish before calling the osc and mdc sync methods */
885 rc = filemap_fdatawait(inode->i_mapping);
887 ll_inode2fid(&fid, inode);
888 err = mdc_sync(ll_i2sbi(inode)->ll_mdc_exp, &fid, &req);
892 ptlrpc_req_finished(req);
895 struct obdo *oa = obdo_alloc();
898 RETURN(rc ? rc : -ENOMEM);
900 oa->o_id = lsm->lsm_object_id;
901 oa->o_valid = OBD_MD_FLID;
902 obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
903 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
905 err = obd_sync(ll_i2sbi(inode)->ll_osc_exp, oa, lsm,
915 int ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
917 struct inode *inode = file->f_dentry->d_inode;
918 struct ll_sb_info *sbi = ll_i2sbi(inode);
919 struct obd_device *obddev;
920 struct ldlm_res_id res_id =
921 { .name = {inode->i_ino, inode->i_generation, LDLM_FLOCK} };
922 struct lustre_handle lockh = {0};
923 struct ldlm_flock flock;
924 ldlm_mode_t mode = 0;
929 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu file_lock=%p\n",
930 inode->i_ino, file_lock);
932 flock.pid = file_lock->fl_pid;
933 flock.start = file_lock->fl_start;
934 flock.end = file_lock->fl_end;
936 switch (file_lock->fl_type) {
941 /* An unlock request may or may not have any relation to
942 * existing locks so we may not be able to pass a lock handle
943 * via a normal ldlm_lock_cancel() request. The request may even
944 * unlock a byte range in the middle of an existing lock. In
945 * order to process an unlock request we need all of the same
946 * information that is given with a normal read or write record
947 * lock request. To avoid creating another ldlm unlock (cancel)
948 * message we'll treat a LCK_NL flock request as an unlock. */
955 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
964 flags = LDLM_FL_BLOCK_NOWAIT;
967 flags = LDLM_FL_TEST_LOCK;
968 /* Save the old mode so that if the mode in the lock changes we
969 * can decrement the appropriate reader or writer refcount. */
970 file_lock->fl_type = mode;
973 CERROR("unknown fcntl lock command: %d\n", cmd);
977 CDEBUG(D_DLMTRACE, "inode=%lu, pid=%u, flags=%#x, mode=%u, "
978 "start="LPU64", end="LPU64"\n", inode->i_ino, flock.pid,
979 flags, mode, flock.start, flock.end);
981 obddev = sbi->ll_mdc_exp->exp_obd;
982 rc = ldlm_cli_enqueue(sbi->ll_mdc_exp, NULL, obddev->obd_namespace,
983 NULL, res_id, LDLM_FLOCK, &flock, sizeof(flock),
984 mode, &flags, ldlm_flock_completion_ast, NULL,
989 static int ll_have_md_lock(struct dentry *de)
991 struct ll_sb_info *sbi = ll_s2sbi(de->d_sb);
992 struct lustre_handle lockh;
993 struct ldlm_res_id res_id = { .name = {0} };
994 struct obd_device *obddev;
1001 obddev = sbi->ll_mdc_exp->exp_obd;
1002 res_id.name[0] = de->d_inode->i_ino;
1003 res_id.name[1] = de->d_inode->i_generation;
1005 CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
1007 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
1008 if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_PLAIN,
1009 NULL, 0, LCK_PR, &lockh)) {
1010 ldlm_lock_decref(&lockh, LCK_PR);
1014 if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_PLAIN,
1015 NULL, 0, LCK_PW, &lockh)) {
1016 ldlm_lock_decref(&lockh, LCK_PW);
1022 int ll_inode_revalidate_it(struct dentry *dentry, struct lookup_intent *it)
1024 struct inode *inode = dentry->d_inode;
1025 struct lov_stripe_md *lsm;
1029 CERROR("REPORT THIS LINE TO PETER\n");
1032 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),name=%s\n",
1033 inode->i_ino, inode->i_generation, inode, dentry->d_name.name);
1034 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
1035 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_REVALIDATE);
1038 if (!ll_have_md_lock(dentry)) {
1039 struct ptlrpc_request *req = NULL;
1040 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
1042 unsigned long valid = 0;
1045 if (S_ISREG(inode->i_mode)) {
1046 ealen = obd_size_diskmd(sbi->ll_osc_exp, NULL);
1047 valid |= OBD_MD_FLEASIZE;
1049 ll_inode2fid(&fid, inode);
1050 rc = mdc_getattr(sbi->ll_mdc_exp, &fid, valid, ealen, &req);
1052 CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1055 rc = ll_prep_inode(sbi->ll_osc_exp, &inode, req, 0, NULL);
1057 ptlrpc_req_finished(req);
1060 ptlrpc_req_finished(req);
1064 if (ll_have_md_lock(dentry) &&
1065 test_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &ll_i2info(inode)->lli_flags))
1069 lsm = ll_i2info(inode)->lli_smd;
1070 if (!lsm) /* object not yet allocated, don't validate size */
1073 /* unfortunately stat comes in through revalidate and we don't
1074 * differentiate this use from initial instantiation. we're
1075 * also being wildly conservative and flushing write caches
1076 * so that stat really returns the proper size. */
1078 struct ldlm_extent extent = {0, OBD_OBJECT_EOF};
1079 struct lustre_handle lockh = {0};
1082 err = ll_extent_lock(NULL, inode, lsm, LCK_PR, &extent, &lockh);
1083 if (err != ELDLM_OK)
1086 ll_extent_unlock(NULL, inode, lsm, LCK_PR, &lockh);
1091 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1092 int ll_getattr(struct vfsmount *mnt, struct dentry *de,
1093 struct lookup_intent *it, struct kstat *stat)
1096 struct inode *inode = de->d_inode;
1098 res = ll_inode_revalidate_it(de, it);
1099 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_GETATTR);
1104 stat->dev = inode->i_sb->s_dev;
1105 stat->ino = inode->i_ino;
1106 stat->mode = inode->i_mode;
1107 stat->nlink = inode->i_nlink;
1108 stat->uid = inode->i_uid;
1109 stat->gid = inode->i_gid;
1110 stat->rdev = kdev_t_to_nr(inode->i_rdev);
1111 stat->atime = inode->i_atime;
1112 stat->mtime = inode->i_mtime;
1113 stat->ctime = inode->i_ctime;
1114 stat->size = inode->i_size;
1115 stat->blksize = inode->i_blksize;
1116 stat->blocks = inode->i_blocks;
1121 struct file_operations ll_file_operations = {
1123 write: ll_file_write,
1124 ioctl: ll_file_ioctl,
1126 release: ll_file_release,
1127 mmap: generic_file_mmap,
1128 llseek: ll_file_seek,
1130 //lock: ll_file_flock
1133 struct inode_operations ll_file_inode_operations = {
1134 setattr_raw: ll_setattr_raw,
1135 setattr: ll_setattr,
1136 truncate: ll_truncate,
1137 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1138 getattr_it: ll_getattr,
1140 revalidate_it: ll_inode_revalidate_it,
1144 struct inode_operations ll_special_inode_operations = {
1145 setattr_raw: ll_setattr_raw,
1146 setattr: ll_setattr,
1147 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1148 getattr_it: ll_getattr,
1150 revalidate_it: ll_inode_revalidate_it,