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>
33 #include "llite_internal.h"
35 int ll_mdc_close(struct obd_export *mdc_exp, struct inode *inode,
38 struct ll_file_data *fd = file->private_data;
39 struct ptlrpc_request *req = NULL;
40 struct obd_client_handle *och = &fd->fd_mds_och;
45 /* clear group lock, if present */
46 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
47 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
48 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
49 rc = ll_extent_unlock(fd, inode, lsm, LCK_GROUP,
55 memset(&obdo, 0, sizeof(obdo));
56 obdo.o_id = inode->i_ino;
57 obdo.o_mode = inode->i_mode;
58 obdo.o_size = inode->i_size;
59 obdo.o_blocks = inode->i_blocks;
60 if (0 /* ll_is_inode_dirty(inode) */) {
61 obdo.o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
62 valid |= OBD_MD_FLFLAGS;
65 rc = mdc_close(mdc_exp, &obdo, och, &req);
67 /* We are the last writer, so the MDS has instructed us to get
68 * the file size and any write cookies, then close again. */
69 //ll_queue_done_writing(inode);
72 CERROR("inode %lu mdc close failed: rc = %d\n",
76 rc = ll_objects_destroy(req, file->f_dentry->d_inode);
78 CERROR("inode %lu ll_objects destroy: rc = %d\n",
82 mdc_clear_open_replay_data(och);
83 ptlrpc_req_finished(req);
84 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
85 file->private_data = NULL;
86 OBD_SLAB_FREE(fd, ll_file_data_slab, sizeof *fd);
91 /* While this returns an error code, fput() the caller does not, so we need
92 * to make every effort to clean up all of our state here. Also, applications
93 * rarely check close errors and even if an error is returned they will not
94 * re-try the close call.
96 int ll_file_release(struct inode *inode, struct file *file)
98 struct ll_file_data *fd;
99 struct ll_sb_info *sbi = ll_i2sbi(inode);
103 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
104 inode->i_generation, inode);
106 /* don't do anything for / */
107 if (inode->i_sb->s_root == file->f_dentry)
110 lprocfs_counter_incr(sbi->ll_stats, LPROC_LL_RELEASE);
111 fd = (struct ll_file_data *)file->private_data;
114 rc = ll_mdc_close(sbi->ll_mdc_exp, inode, file);
118 static int ll_intent_file_open(struct file *file, void *lmm,
119 int lmmsize, struct lookup_intent *itp)
121 struct ll_sb_info *sbi = ll_i2sbi(file->f_dentry->d_inode);
122 struct lustre_handle lockh;
123 struct mdc_op_data data;
124 struct dentry *parent = file->f_dentry->d_parent;
125 const char *name = file->f_dentry->d_name.name;
126 const int len = file->f_dentry->d_name.len;
132 ll_prepare_mdc_op_data(&data, parent->d_inode, NULL, name, len, O_RDWR);
134 rc = mdc_enqueue(sbi->ll_mdc_exp, LDLM_IBITS, itp, LCK_PW, &data,
135 &lockh, lmm, lmmsize, ldlm_completion_ast,
136 ll_mdc_blocking_ast, parent->d_inode);
138 CERROR("lock enqueue: err: %d\n", rc);
142 int ll_local_open(struct file *file, struct lookup_intent *it)
144 struct ptlrpc_request *req = it->d.lustre.it_data;
145 struct ll_inode_info *lli = ll_i2info(file->f_dentry->d_inode);
146 struct ll_file_data *fd;
147 struct mds_body *body;
150 body = lustre_msg_buf (req->rq_repmsg, 1, sizeof (*body));
151 LASSERT (body != NULL); /* reply already checked out */
152 LASSERT_REPSWABBED (req, 1); /* and swabbed down */
154 LASSERT(!file->private_data);
156 OBD_SLAB_ALLOC(fd, ll_file_data_slab, SLAB_KERNEL, sizeof *fd);
157 /* We can't handle this well without reorganizing ll_file_open and
158 * ll_mdc_close, so don't even try right now. */
161 memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
162 fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
163 file->private_data = fd;
164 ll_readahead_init(file->f_dentry->d_inode, &fd->fd_ras);
166 lli->lli_io_epoch = body->io_epoch;
168 mdc_set_open_replay_data(&fd->fd_mds_och, it->d.lustre.it_data);
173 /* Open a file, and (for the very first open) create objects on the OSTs at
174 * this time. If opened with O_LOV_DELAY_CREATE, then we don't do the object
175 * creation or open until ll_lov_setstripe() ioctl is called. We grab
176 * lli_open_sem to ensure no other process will create objects, send the
177 * stripe MD to the MDS, or try to destroy the objects if that fails.
179 * If we already have the stripe MD locally then we don't request it in
180 * mdc_open(), by passing a lmm_size = 0.
182 * It is up to the application to ensure no other processes open this file
183 * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
184 * used. We might be able to avoid races of that sort by getting lli_open_sem
185 * before returning in the O_LOV_DELAY_CREATE case and dropping it here
186 * or in ll_file_release(), but I'm not sure that is desirable/necessary.
188 int ll_file_open(struct inode *inode, struct file *file)
190 struct ll_inode_info *lli = ll_i2info(inode);
191 struct lookup_intent *it;
192 struct lov_stripe_md *lsm;
193 struct ptlrpc_request *req;
197 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
198 inode->i_generation, inode);
200 /* don't do anything for / */
201 if (inode->i_sb->s_root == file->f_dentry)
206 if (!it->d.lustre.it_disposition) {
207 struct lookup_intent oit = { .it_op = IT_OPEN,
208 .it_flags = file->f_flags };
210 rc = ll_intent_file_open(file, NULL, 0, it);
215 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_OPEN);
216 rc = it_open_error(DISP_OPEN_OPEN, it);
220 rc = ll_local_open(file, it);
224 if (!S_ISREG(inode->i_mode))
229 if (file->f_flags & O_LOV_DELAY_CREATE ||
230 !(file->f_mode & FMODE_WRITE)) {
231 CDEBUG(D_INODE, "object creation was delayed\n");
235 file->f_flags &= ~O_LOV_DELAY_CREATE;
238 req = it->d.lustre.it_data;
239 ptlrpc_req_finished(req);
241 ll_open_complete(inode);
245 /* Fills the obdo with the attributes for the inode defined by lsm */
246 int ll_lsm_getattr(struct obd_export *exp, struct lov_stripe_md *lsm,
249 struct ptlrpc_request_set *set;
253 LASSERT(lsm != NULL);
255 memset(oa, 0, sizeof *oa);
256 oa->o_id = lsm->lsm_object_id;
257 oa->o_mode = S_IFREG;
258 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
259 OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
262 set = ptlrpc_prep_set();
264 CERROR ("ENOMEM allocing request set\n");
267 rc = obd_getattr_async(exp, oa, lsm, set);
269 rc = ptlrpc_set_wait(set);
270 ptlrpc_set_destroy(set);
275 oa->o_valid &= (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
276 OBD_MD_FLCTIME | OBD_MD_FLSIZE);
280 static inline void ll_remove_suid(struct inode *inode)
284 /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
285 mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
287 /* was any of the uid bits set? */
288 mode &= inode->i_mode;
289 if (mode && !capable(CAP_FSETID)) {
290 inode->i_mode &= ~mode;
291 // XXX careful here - we cannot change the size
295 static int ll_lock_to_stripe_offset(struct inode *inode, struct ldlm_lock *lock)
297 struct ll_inode_info *lli = ll_i2info(inode);
298 struct lov_stripe_md *lsm = lli->lli_smd;
299 struct obd_export *exp = ll_i2obdexp(inode);
302 struct ldlm_lock *lock;
303 struct lov_stripe_md *lsm;
304 } key = { .name = "lock_to_stripe", .lock = lock, .lsm = lsm };
305 __u32 stripe, vallen = sizeof(stripe);
309 if (lsm->lsm_stripe_count == 1)
312 /* get our offset in the lov */
313 rc = obd_get_info(exp, sizeof(key), &key, &vallen, &stripe);
315 CERROR("obd_get_info: rc = %d\n", rc);
318 LASSERT(stripe < lsm->lsm_stripe_count);
322 /* Flush the page cache for an extent as its canceled. When we're on an LOV,
323 * we get a lock cancellation for each stripe, so we have to map the obd's
324 * region back onto the stripes in the file that it held.
326 * No one can dirty the extent until we've finished our work and they can
327 * enqueue another lock. The DLM protects us from ll_file_read/write here,
328 * but other kernel actors could have pages locked.
330 * Called with the DLM lock held. */
331 void ll_pgcache_remove_extent(struct inode *inode, struct lov_stripe_md *lsm,
332 struct ldlm_lock *lock, __u32 stripe)
334 ldlm_policy_data_t tmpex;
335 unsigned long start, end, count, skip, i, j;
337 int rc, rc2, discard = lock->l_flags & LDLM_FL_DISCARD_DATA;
338 struct lustre_handle lockh;
341 memcpy(&tmpex, &lock->l_policy_data, sizeof(tmpex));
342 CDEBUG(D_INODE|D_PAGE, "inode %lu(%p) ["LPU64"->"LPU64"] size: %llu\n",
343 inode->i_ino, inode, tmpex.l_extent.start, tmpex.l_extent.end,
346 /* our locks are page granular thanks to osc_enqueue, we invalidate the
348 LASSERT((tmpex.l_extent.start & ~PAGE_CACHE_MASK) == 0);
349 LASSERT(((tmpex.l_extent.end + 1) & ~PAGE_CACHE_MASK) == 0);
353 start = tmpex.l_extent.start >> PAGE_CACHE_SHIFT;
354 end = tmpex.l_extent.end >> PAGE_CACHE_SHIFT;
355 if (lsm->lsm_stripe_count > 1) {
356 count = lsm->lsm_stripe_size >> PAGE_CACHE_SHIFT;
357 skip = (lsm->lsm_stripe_count - 1) * count;
358 start += start/count * skip + stripe * count;
360 end += end/count * skip + stripe * count;
362 if (end < tmpex.l_extent.end >> PAGE_CACHE_SHIFT)
365 i = (inode->i_size + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
369 CDEBUG(D_INODE|D_PAGE, "walking page indices start: %lu j: %lu "
370 "count: %lu skip: %lu end: %lu%s\n", start, start % count,
371 count, skip, end, discard ? " (DISCARDING)" : "");
373 /* this is the simplistic implementation of page eviction at
374 * cancelation. It is careful to get races with other page
375 * lockers handled correctly. fixes from bug 20 will make it
376 * more efficient by associating locks with pages and with
377 * batching writeback under the lock explicitly. */
378 for (i = start, j = start % count; i <= end;
379 j++, i++, tmpex.l_extent.start += PAGE_CACHE_SIZE) {
381 CDEBUG(D_PAGE, "skip index %lu to %lu\n", i, i + skip);
387 LASSERTF(tmpex.l_extent.start< lock->l_policy_data.l_extent.end,
388 LPU64" >= "LPU64" start %lu i %lu end %lu\n",
389 tmpex.l_extent.start, lock->l_policy_data.l_extent.end,
392 if (!mapping_has_pages(inode->i_mapping)) {
393 CDEBUG(D_INODE|D_PAGE, "nothing left\n");
397 conditional_schedule();
399 page = find_get_page(inode->i_mapping, i);
402 LL_CDEBUG_PAGE(D_PAGE, page, "lock page idx %lu ext "LPU64"\n",
403 i, tmpex.l_extent.start);
406 /* page->mapping to check with racing against teardown */
407 if (!discard && clear_page_dirty_for_io(page)) {
408 rc = ll_call_writepage(inode, page);
410 CERROR("writepage of page %p failed: %d\n",
412 /* either waiting for io to complete or reacquiring
413 * the lock that the failed writepage released */
417 tmpex.l_extent.end = tmpex.l_extent.start + PAGE_CACHE_SIZE - 1;
418 /* check to see if another DLM lock covers this page */
419 ldlm_lock2handle(lock, &lockh);
420 rc2 = ldlm_lock_match(NULL,
421 LDLM_FL_BLOCK_GRANTED|LDLM_FL_CBPENDING |
423 NULL, 0, &tmpex, 0, &lockh);
424 if (rc2 == 0 && page->mapping != NULL) {
425 // checking again to account for writeback's lock_page()
426 LL_CDEBUG_PAGE(D_PAGE, page, "truncating\n");
427 ll_truncate_complete_page(page);
430 page_cache_release(page);
432 LASSERTF(tmpex.l_extent.start <=
433 (lock->l_policy_data.l_extent.end == ~0ULL ? ~0ULL :
434 lock->l_policy_data.l_extent.end + 1),
435 "loop too long "LPU64" > "LPU64" start %lu i %lu end %lu\n",
436 tmpex.l_extent.start, lock->l_policy_data.l_extent.end,
441 static int ll_extent_lock_callback(struct ldlm_lock *lock,
442 struct ldlm_lock_desc *new, void *data,
445 struct lustre_handle lockh = { 0 };
449 if ((unsigned long)data > 0 && (unsigned long)data < 0x1000) {
450 LDLM_ERROR(lock, "cancelling lock with bad data %p", data);
455 case LDLM_CB_BLOCKING:
456 ldlm_lock2handle(lock, &lockh);
457 rc = ldlm_cli_cancel(&lockh);
459 CERROR("ldlm_cli_cancel failed: %d\n", rc);
461 case LDLM_CB_CANCELING: {
463 struct ll_inode_info *lli;
464 struct lov_stripe_md *lsm;
468 /* This lock wasn't granted, don't try to evict pages */
469 if (lock->l_req_mode != lock->l_granted_mode)
472 inode = ll_inode_from_lock(lock);
475 lli = ll_i2info(inode);
478 if (lli->lli_smd == NULL)
482 stripe = ll_lock_to_stripe_offset(inode, lock);
483 ll_pgcache_remove_extent(inode, lsm, lock, stripe);
486 kms = ldlm_extent_shift_kms(lock,
487 lsm->lsm_oinfo[stripe].loi_kms);
489 if (lsm->lsm_oinfo[stripe].loi_kms != kms)
490 LDLM_DEBUG(lock, "updating kms from "LPU64" to "LPU64,
491 lsm->lsm_oinfo[stripe].loi_kms, kms);
492 lsm->lsm_oinfo[stripe].loi_kms = kms;
494 //ll_try_done_writing(inode);
507 int ll_async_completion_ast(struct ldlm_lock *lock, int flags, void *data)
509 /* XXX ALLOCATE - 160 bytes */
510 struct inode *inode = ll_inode_from_lock(lock);
511 struct ll_inode_info *lli = ll_i2info(inode);
512 struct lustre_handle lockh = { 0 };
517 if (flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
518 LDLM_FL_BLOCK_CONV)) {
519 LBUG(); /* not expecting any blocked async locks yet */
520 LDLM_DEBUG(lock, "client-side async enqueue returned a blocked "
522 ldlm_lock_dump(D_OTHER, lock, 0);
523 ldlm_reprocess_all(lock->l_resource);
527 LDLM_DEBUG(lock, "client-side async enqueue: granted/glimpsed");
529 stripe = ll_lock_to_stripe_offset(inode, lock);
531 if (lock->l_lvb_len) {
532 struct lov_stripe_md *lsm = lli->lli_smd;
534 lvb = lock->l_lvb_data;
535 lsm->lsm_oinfo[stripe].loi_rss = lvb->lvb_size;
538 kms = MAX(lsm->lsm_oinfo[stripe].loi_kms, lvb->lvb_size);
539 kms = ldlm_extent_shift_kms(NULL, kms);
540 if (lsm->lsm_oinfo[stripe].loi_kms != kms)
541 LDLM_DEBUG(lock, "updating kms from "LPU64" to "LPU64,
542 lsm->lsm_oinfo[stripe].loi_kms, kms);
543 lsm->lsm_oinfo[stripe].loi_kms = kms;
548 wake_up(&lock->l_waitq);
550 ldlm_lock2handle(lock, &lockh);
551 ldlm_lock_decref(&lockh, LCK_PR);
556 static int ll_glimpse_callback(struct ldlm_lock *lock, void *reqp)
558 struct ptlrpc_request *req = reqp;
559 struct inode *inode = ll_inode_from_lock(lock);
560 struct ll_inode_info *lli;
562 int rc, size = sizeof(*lvb), stripe = 0;
566 GOTO(out, rc = -ELDLM_NO_LOCK_DATA);
567 lli = ll_i2info(inode);
569 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
570 if (lli->lli_smd == NULL)
571 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
573 /* First, find out which stripe index this lock corresponds to. */
574 if (lli->lli_smd->lsm_stripe_count > 1)
575 stripe = ll_lock_to_stripe_offset(inode, lock);
577 rc = lustre_pack_reply(req, 1, &size, NULL);
579 CERROR("lustre_pack_reply: %d\n", rc);
583 lvb = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*lvb));
584 lvb->lvb_size = lli->lli_smd->lsm_oinfo[stripe].loi_kms;
586 LDLM_DEBUG(lock, "i_size: %llu -> stripe number %u -> kms "LPU64,
587 inode->i_size, stripe, lvb->lvb_size);
593 /* These errors are normal races, so we don't want to fill the console
594 * with messages by calling ptlrpc_error() */
595 if (rc == -ELDLM_NO_LOCK_DATA)
596 lustre_pack_reply(req, 0, NULL, NULL);
602 __u64 lov_merge_size(struct lov_stripe_md *lsm, int kms);
603 __u64 lov_merge_blocks(struct lov_stripe_md *lsm);
604 __u64 lov_merge_mtime(struct lov_stripe_md *lsm, __u64 current_time);
606 /* NB: lov_merge_size will prefer locally cached writes if they extend the
607 * file (because it prefers KMS over RSS when larger) */
608 int ll_glimpse_size(struct inode *inode, struct ost_lvb *lvb)
610 struct ll_inode_info *lli = ll_i2info(inode);
611 struct ll_sb_info *sbi = ll_i2sbi(inode);
612 ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
613 struct lustre_handle lockh = { 0 };
614 int rc, flags = LDLM_FL_HAS_INTENT;
617 CDEBUG(D_DLMTRACE, "Glimpsing inode %lu\n", inode->i_ino);
619 rc = obd_enqueue(sbi->ll_osc_exp, lli->lli_smd, LDLM_EXTENT, &policy,
620 LCK_PR, &flags, ll_extent_lock_callback,
621 ldlm_completion_ast, ll_glimpse_callback, inode,
622 sizeof(*lvb), lustre_swab_ost_lvb, &lockh);
624 CERROR("obd_enqueue returned rc %d, returning -EIO\n", rc);
625 RETURN(rc > 0 ? -EIO : rc);
628 lvb->lvb_size = lov_merge_size(lli->lli_smd, 0);
629 inode->i_blocks = lov_merge_blocks(lli->lli_smd);
630 //inode->i_mtime = lov_merge_mtime(lli->lli_smd, inode->i_mtime);
632 CDEBUG(D_DLMTRACE, "glimpse: size: "LPU64", blocks: "LPU64"\n",
633 lvb->lvb_size, lvb->lvb_blocks);
635 obd_cancel(sbi->ll_osc_exp, lli->lli_smd, LCK_PR, &lockh);
640 int ll_extent_lock(struct ll_file_data *fd, struct inode *inode,
641 struct lov_stripe_md *lsm, int mode,
642 ldlm_policy_data_t *policy, struct lustre_handle *lockh,
645 struct ll_sb_info *sbi = ll_i2sbi(inode);
649 LASSERT(lockh->cookie == 0);
651 /* XXX phil: can we do this? won't it screw the file size up? */
652 if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
653 (sbi->ll_flags & LL_SBI_NOLCK))
656 CDEBUG(D_DLMTRACE, "Locking inode %lu, start "LPU64" end "LPU64"\n",
657 inode->i_ino, policy->l_extent.start, policy->l_extent.end);
659 rc = obd_enqueue(sbi->ll_osc_exp, lsm, LDLM_EXTENT, policy, mode,
660 &ast_flags, ll_extent_lock_callback,
661 ldlm_completion_ast, ll_glimpse_callback, inode,
662 sizeof(struct ost_lvb), lustre_swab_ost_lvb, lockh);
666 if (policy->l_extent.start == 0 &&
667 policy->l_extent.end == OBD_OBJECT_EOF)
668 inode->i_size = lov_merge_size(lsm, 1);
670 //inode->i_mtime = lov_merge_mtime(lsm, inode->i_mtime);
675 int ll_extent_unlock(struct ll_file_data *fd, struct inode *inode,
676 struct lov_stripe_md *lsm, int mode,
677 struct lustre_handle *lockh)
679 struct ll_sb_info *sbi = ll_i2sbi(inode);
683 /* XXX phil: can we do this? won't it screw the file size up? */
684 if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
685 (sbi->ll_flags & LL_SBI_NOLCK))
688 rc = obd_cancel(sbi->ll_osc_exp, lsm, mode, lockh);
693 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
696 struct ll_file_data *fd = filp->private_data;
697 struct inode *inode = filp->f_dentry->d_inode;
698 struct ll_inode_info *lli = ll_i2info(inode);
699 struct lov_stripe_md *lsm = lli->lli_smd;
700 struct lustre_handle lockh = { 0 };
701 ldlm_policy_data_t policy;
706 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
707 inode->i_ino, inode->i_generation, inode, count, *ppos);
709 /* "If nbyte is 0, read() will return 0 and have no other results."
710 * -- Single Unix Spec */
714 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_READ_BYTES,
720 policy.l_extent.start = *ppos;
721 policy.l_extent.end = *ppos + count - 1;
723 err = ll_extent_lock(fd, inode, lsm, LCK_PR, &policy, &lockh,
724 (filp->f_flags & O_NONBLOCK)?LDLM_FL_BLOCK_NOWAIT:
729 kms = lov_merge_size(lsm, 1);
730 if (*ppos + count - 1 > kms) {
731 /* A glimpse is necessary to determine whether we return a short
732 * read or some zeroes at the end of the buffer */
734 retval = ll_glimpse_size(inode, &lvb);
737 inode->i_size = lvb.lvb_size;
742 CDEBUG(D_INFO, "Read ino %lu, "LPSZ" bytes, offset %lld, i_size %llu\n",
743 inode->i_ino, count, *ppos, inode->i_size);
745 /* turn off the kernel's read-ahead */
746 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
749 filp->f_ra.ra_pages = 0;
751 retval = generic_file_read(filp, buf, count, ppos);
754 ll_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
759 * Write to a file (through the page cache).
761 static ssize_t ll_file_write(struct file *file, const char *buf, size_t count,
764 struct ll_file_data *fd = file->private_data;
765 struct inode *inode = file->f_dentry->d_inode;
766 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
767 struct lustre_handle lockh = { 0 };
768 ldlm_policy_data_t policy;
769 loff_t maxbytes = ll_file_maxbytes(inode);
774 if (file->f_flags & O_NONBLOCK)
775 nonblock = LDLM_FL_BLOCK_NOWAIT;
776 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
777 inode->i_ino, inode->i_generation, inode, count, *ppos);
779 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
781 /* POSIX, but surprised the VFS doesn't check this already */
785 /* If file was opened for LL_IOC_LOV_SETSTRIPE but the ioctl wasn't
786 * called on the file, don't fail the below assertion (bug 2388). */
787 if (file->f_flags & O_LOV_DELAY_CREATE && lsm == NULL)
792 if (file->f_flags & O_APPEND) {
793 policy.l_extent.start = 0;
794 policy.l_extent.end = OBD_OBJECT_EOF;
796 policy.l_extent.start = *ppos;
797 policy.l_extent.end = *ppos + count - 1;
800 err = ll_extent_lock(fd, inode, lsm, LCK_PW, &policy, &lockh, nonblock);
804 /* this is ok, g_f_w will overwrite this under i_sem if it races
805 * with a local truncate, it just makes our maxbyte checking easier */
806 if (file->f_flags & O_APPEND)
807 *ppos = inode->i_size;
809 if (*ppos >= maxbytes) {
810 if (count || *ppos > maxbytes) {
811 send_sig(SIGXFSZ, current, 0);
812 GOTO(out, retval = -EFBIG);
815 if (*ppos + count > maxbytes)
816 count = maxbytes - *ppos;
818 CDEBUG(D_INFO, "Writing inode %lu, "LPSZ" bytes, offset %Lu\n",
819 inode->i_ino, count, *ppos);
821 /* generic_file_write handles O_APPEND after getting i_sem */
822 retval = generic_file_write(file, buf, count, ppos);
825 ll_extent_unlock(fd, inode, lsm, LCK_PW, &lockh);
826 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_WRITE_BYTES,
827 retval > 0 ? retval : 0);
831 static int ll_lov_recreate_obj(struct inode *inode, struct file *file,
834 struct ll_inode_info *lli = ll_i2info(inode);
835 struct obd_export *exp = ll_i2obdexp(inode);
836 struct ll_recreate_obj ucreatp;
837 struct obd_trans_info oti = { 0 };
838 struct obdo *oa = NULL;
841 struct lov_stripe_md *lsm, *lsm2;
844 if (!capable (CAP_SYS_ADMIN))
847 rc = copy_from_user(&ucreatp, (struct ll_recreate_obj *)arg,
848 sizeof(struct ll_recreate_obj));
857 down(&lli->lli_open_sem);
860 up(&lli->lli_open_sem);
864 lsm_size = sizeof(*lsm) + (sizeof(struct lov_oinfo) *
865 (lsm->lsm_stripe_count));
867 OBD_ALLOC(lsm2, lsm_size);
869 up(&lli->lli_open_sem);
874 oa->o_id = ucreatp.lrc_id;
875 oa->o_nlink = ucreatp.lrc_ost_idx;
876 oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS;
877 oa->o_flags |= OBD_FL_RECREATE_OBJS;
878 obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
879 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
881 oti.oti_objid = NULL;
882 memcpy(lsm2, lsm, lsm_size);
883 rc = obd_create(exp, oa, &lsm2, &oti);
885 up(&lli->lli_open_sem);
886 OBD_FREE(lsm2, lsm_size);
891 static int ll_lov_setstripe_ea_info(struct inode *inode, struct file *file,
892 int flags, struct lov_user_md *lum,
895 struct ll_inode_info *lli = ll_i2info(inode);
897 struct obd_export *exp = ll_i2obdexp(inode);
898 struct lov_stripe_md *lsm;
899 struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
900 struct ptlrpc_request *req = NULL;
905 down(&lli->lli_open_sem);
908 up(&lli->lli_open_sem);
909 CDEBUG(D_IOCTL, "stripe already exists for ino %lu\n",
914 f = get_empty_filp();
918 f->f_dentry = file->f_dentry;
919 f->f_vfsmnt = file->f_vfsmnt;
921 rc = ll_intent_file_open(f, lum, lum_size, &oit);
924 if (it_disposition(&oit, DISP_LOOKUP_NEG))
926 req = oit.d.lustre.it_data;
927 rc = oit.d.lustre.it_status;
932 rc = mdc_req2lustre_md(req, 1, exp, &md);
935 ll_update_inode(f->f_dentry->d_inode, md.body, md.lsm);
937 rc = ll_local_open(f, &oit);
940 ll_intent_release(&oit);
942 rc = ll_file_release(f->f_dentry->d_inode, f);
947 up(&lli->lli_open_sem);
949 ptlrpc_req_finished(req);
953 static int ll_lov_setea(struct inode *inode, struct file *file,
956 int flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
957 struct lov_user_md *lump;
958 int lum_size = sizeof(struct lov_user_md) +
959 sizeof(struct lov_user_ost_data);
963 if (!capable (CAP_SYS_ADMIN))
966 OBD_ALLOC(lump, lum_size);
970 rc = copy_from_user(lump, (struct lov_user_md *)arg, lum_size);
972 OBD_FREE(lump, lum_size);
976 rc = ll_lov_setstripe_ea_info(inode, file, flags, lump, lum_size);
978 OBD_FREE(lump, lum_size);
982 static int ll_lov_setstripe(struct inode *inode, struct file *file,
985 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
987 int flags = FMODE_WRITE;
990 /* Bug 1152: copy properly when this is no longer true */
991 LASSERT(sizeof(lum) == sizeof(*lump));
992 LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
993 rc = copy_from_user(&lum, lump, sizeof(lum));
997 rc = ll_lov_setstripe_ea_info(inode, file, flags, &lum, sizeof(lum));
1001 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
1003 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1008 return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2obdexp(inode), 0, lsm,
1012 static int ll_get_grouplock(struct inode *inode, struct file *file,
1015 struct ll_file_data *fd = file->private_data;
1016 ldlm_policy_data_t policy = { .l_extent = { .start = 0,
1017 .end = OBD_OBJECT_EOF}};
1018 struct lustre_handle lockh = { 0 };
1019 struct ll_inode_info *lli = ll_i2info(inode);
1020 struct lov_stripe_md *lsm = lli->lli_smd;
1025 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1029 policy.l_extent.gid = arg;
1030 if (file->f_flags & O_NONBLOCK)
1031 flags = LDLM_FL_BLOCK_NOWAIT;
1033 err = ll_extent_lock(fd, inode, lsm, LCK_GROUP, &policy, &lockh, flags);
1037 fd->fd_flags |= LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK;
1039 memcpy(&fd->fd_cwlockh, &lockh, sizeof(lockh));
1044 static int ll_put_grouplock(struct inode *inode, struct file *file,
1047 struct ll_file_data *fd = file->private_data;
1048 struct ll_inode_info *lli = ll_i2info(inode);
1049 struct lov_stripe_md *lsm = lli->lli_smd;
1053 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1054 /* Ugh, it's already unlocked. */
1058 if (fd->fd_gid != arg) /* Ugh? Unlocking with different gid? */
1061 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
1063 err = ll_extent_unlock(fd, inode, lsm, LCK_GROUP, &fd->fd_cwlockh);
1068 memset(&fd->fd_cwlockh, 0, sizeof(fd->fd_cwlockh));
1073 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
1076 struct ll_file_data *fd = file->private_data;
1080 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),cmd=%x\n", inode->i_ino,
1081 inode->i_generation, inode, cmd);
1083 if (_IOC_TYPE(cmd) == 'T') /* tty ioctls */
1086 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
1088 case LL_IOC_GETFLAGS:
1089 /* Get the current value of the file flags */
1090 return put_user(fd->fd_flags, (int *)arg);
1091 case LL_IOC_SETFLAGS:
1092 case LL_IOC_CLRFLAGS:
1093 /* Set or clear specific file flags */
1094 /* XXX This probably needs checks to ensure the flags are
1095 * not abused, and to handle any flag side effects.
1097 if (get_user(flags, (int *) arg))
1100 if (cmd == LL_IOC_SETFLAGS)
1101 fd->fd_flags |= flags;
1103 fd->fd_flags &= ~flags;
1105 case LL_IOC_LOV_SETSTRIPE:
1106 RETURN(ll_lov_setstripe(inode, file, arg));
1107 case LL_IOC_LOV_SETEA:
1108 RETURN(ll_lov_setea(inode, file, arg));
1109 case LL_IOC_LOV_GETSTRIPE:
1110 RETURN(ll_lov_getstripe(inode, arg));
1111 case LL_IOC_RECREATE_OBJ:
1112 RETURN(ll_lov_recreate_obj(inode, file, arg));
1113 case EXT3_IOC_GETFLAGS:
1114 case EXT3_IOC_SETFLAGS:
1115 RETURN( ll_iocontrol(inode, file, cmd, arg) );
1116 case LL_IOC_GROUP_LOCK:
1117 RETURN(ll_get_grouplock(inode, file, arg));
1118 case LL_IOC_GROUP_UNLOCK:
1119 RETURN(ll_put_grouplock(inode, file, arg));
1120 /* We need to special case any other ioctls we want to handle,
1121 * to send them to the MDS/OST as appropriate and to properly
1122 * network encode the arg field.
1123 case EXT2_IOC_GETVERSION_OLD:
1124 case EXT2_IOC_GETVERSION_NEW:
1125 case EXT2_IOC_SETVERSION_OLD:
1126 case EXT2_IOC_SETVERSION_NEW:
1129 RETURN( obd_iocontrol(cmd, ll_i2obdexp(inode), 0, NULL,
1134 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
1136 struct inode *inode = file->f_dentry->d_inode;
1137 struct ll_file_data *fd = file->private_data;
1138 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1139 struct lustre_handle lockh = {0};
1142 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),to=%llu\n", inode->i_ino,
1143 inode->i_generation, inode,
1144 offset + ((origin==2) ? inode->i_size : file->f_pos));
1146 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_LLSEEK);
1147 if (origin == 2) { /* SEEK_END */
1150 ldlm_policy_data_t policy = { .l_extent = {0, OBD_OBJECT_EOF }};
1152 if (file->f_flags & O_NONBLOCK)
1153 nonblock = LDLM_FL_BLOCK_NOWAIT;
1155 err = ll_extent_lock(fd, inode, lsm, LCK_PR, &policy, &lockh,
1157 if (err != ELDLM_OK)
1160 offset += inode->i_size;
1161 } else if (origin == 1) { /* SEEK_CUR */
1162 offset += file->f_pos;
1166 if (offset >= 0 && offset <= ll_file_maxbytes(inode)) {
1167 if (offset != file->f_pos) {
1168 file->f_pos = offset;
1169 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1171 file->f_version = ++event;
1178 ll_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
1182 int ll_fsync(struct file *file, struct dentry *dentry, int data)
1184 struct inode *inode = dentry->d_inode;
1185 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1187 struct ptlrpc_request *req;
1190 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1191 inode->i_generation, inode);
1193 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_FSYNC);
1195 /* fsync's caller has already called _fdata{sync,write}, we want
1196 * that IO to finish before calling the osc and mdc sync methods */
1197 rc = filemap_fdatawait(inode->i_mapping);
1199 ll_inode2fid(&fid, inode);
1200 err = mdc_sync(ll_i2sbi(inode)->ll_mdc_exp, &fid, &req);
1204 ptlrpc_req_finished(req);
1207 struct obdo *oa = obdo_alloc();
1210 RETURN(rc ? rc : -ENOMEM);
1212 oa->o_id = lsm->lsm_object_id;
1213 oa->o_valid = OBD_MD_FLID;
1214 obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
1215 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
1217 err = obd_sync(ll_i2sbi(inode)->ll_osc_exp, oa, lsm,
1227 int ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
1229 struct inode *inode = file->f_dentry->d_inode;
1230 struct ll_sb_info *sbi = ll_i2sbi(inode);
1231 struct obd_device *obddev;
1232 struct ldlm_res_id res_id =
1233 { .name = {inode->i_ino, inode->i_generation, LDLM_FLOCK} };
1234 struct lustre_handle lockh = {0};
1235 ldlm_policy_data_t flock;
1236 ldlm_mode_t mode = 0;
1241 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu file_lock=%p\n",
1242 inode->i_ino, file_lock);
1244 flock.l_flock.pid = file_lock->fl_pid;
1245 flock.l_flock.start = file_lock->fl_start;
1246 flock.l_flock.end = file_lock->fl_end;
1248 switch (file_lock->fl_type) {
1253 /* An unlock request may or may not have any relation to
1254 * existing locks so we may not be able to pass a lock handle
1255 * via a normal ldlm_lock_cancel() request. The request may even
1256 * unlock a byte range in the middle of an existing lock. In
1257 * order to process an unlock request we need all of the same
1258 * information that is given with a normal read or write record
1259 * lock request. To avoid creating another ldlm unlock (cancel)
1260 * message we'll treat a LCK_NL flock request as an unlock. */
1267 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1276 flags = LDLM_FL_BLOCK_NOWAIT;
1279 flags = LDLM_FL_TEST_LOCK;
1280 /* Save the old mode so that if the mode in the lock changes we
1281 * can decrement the appropriate reader or writer refcount. */
1282 file_lock->fl_type = mode;
1285 CERROR("unknown fcntl lock command: %d\n", cmd);
1289 CDEBUG(D_DLMTRACE, "inode=%lu, pid="LPU64", flags=%#x, mode=%u, "
1290 "start="LPU64", end="LPU64"\n", inode->i_ino, flock.l_flock.pid,
1291 flags, mode, flock.l_flock.start, flock.l_flock.end);
1293 obddev = sbi->ll_mdc_exp->exp_obd;
1294 rc = ldlm_cli_enqueue(sbi->ll_mdc_exp, NULL, obddev->obd_namespace,
1295 res_id, LDLM_FLOCK, &flock, mode, &flags,
1296 NULL, ldlm_flock_completion_ast, NULL, file_lock,
1297 NULL, 0, NULL, &lockh);
1301 static int ll_have_md_lock(struct dentry *de, __u64 lockpart)
1303 struct ll_sb_info *sbi = ll_s2sbi(de->d_sb);
1304 struct lustre_handle lockh;
1305 struct ldlm_res_id res_id = { .name = {0} };
1306 struct obd_device *obddev;
1308 ldlm_policy_data_t policy = { .l_inodebits = { lockpart } };
1314 obddev = sbi->ll_mdc_exp->exp_obd;
1315 res_id.name[0] = de->d_inode->i_ino;
1316 res_id.name[1] = de->d_inode->i_generation;
1318 CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
1320 /* FIXME use LDLM_FL_TEST_LOCK instead */
1321 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
1322 if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_IBITS,
1323 &policy, LCK_PR, &lockh)) {
1324 ldlm_lock_decref(&lockh, LCK_PR);
1328 if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_IBITS,
1329 &policy, LCK_PW, &lockh)) {
1330 ldlm_lock_decref(&lockh, LCK_PW);
1336 int ll_inode_revalidate_it(struct dentry *dentry, struct lookup_intent *it)
1338 struct inode *inode = dentry->d_inode;
1339 struct ll_inode_info *lli;
1340 struct lov_stripe_md *lsm;
1345 CERROR("REPORT THIS LINE TO PETER\n");
1348 lli = ll_i2info(inode);
1349 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),name=%s\n",
1350 inode->i_ino, inode->i_generation, inode, dentry->d_name.name);
1351 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
1352 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_REVALIDATE);
1355 if (!ll_have_md_lock(dentry, MDS_INODELOCK_UPDATE)) {
1356 struct ptlrpc_request *req = NULL;
1357 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
1359 unsigned long valid = 0;
1362 if (S_ISREG(inode->i_mode)) {
1363 ealen = obd_size_diskmd(sbi->ll_osc_exp, NULL);
1364 valid |= OBD_MD_FLEASIZE;
1366 ll_inode2fid(&fid, inode);
1367 rc = mdc_getattr(sbi->ll_mdc_exp, &fid, valid, ealen, &req);
1369 CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1372 rc = ll_prep_inode(sbi->ll_osc_exp, &inode, req, 0, NULL);
1374 ptlrpc_req_finished(req);
1377 ptlrpc_req_finished(req);
1381 if (lsm == NULL) /* object not yet allocated, don't validate size */
1384 /* ll_glimpse_size will prefer locally cached writes if they extend
1389 rc = ll_glimpse_size(inode, &lvb);
1390 inode->i_size = lvb.lvb_size;
1395 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1396 int ll_getattr(struct vfsmount *mnt, struct dentry *de,
1397 struct lookup_intent *it, struct kstat *stat)
1400 struct inode *inode = de->d_inode;
1402 res = ll_inode_revalidate_it(de, it);
1403 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_GETATTR);
1408 stat->dev = inode->i_sb->s_dev;
1409 stat->ino = inode->i_ino;
1410 stat->mode = inode->i_mode;
1411 stat->nlink = inode->i_nlink;
1412 stat->uid = inode->i_uid;
1413 stat->gid = inode->i_gid;
1414 stat->rdev = kdev_t_to_nr(inode->i_rdev);
1415 stat->atime = inode->i_atime;
1416 stat->mtime = inode->i_mtime;
1417 stat->ctime = inode->i_ctime;
1418 stat->size = inode->i_size;
1419 stat->blksize = inode->i_blksize;
1420 stat->blocks = inode->i_blocks;
1425 struct file_operations ll_file_operations = {
1426 .read = ll_file_read,
1427 .write = ll_file_write,
1428 .ioctl = ll_file_ioctl,
1429 .open = ll_file_open,
1430 .release = ll_file_release,
1431 .mmap = generic_file_mmap,
1432 .llseek = ll_file_seek,
1433 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
1434 .sendfile = generic_file_sendfile,
1437 //.lock ll_file_flock
1440 struct inode_operations ll_file_inode_operations = {
1441 .setattr_raw = ll_setattr_raw,
1442 .setattr = ll_setattr,
1443 .truncate = ll_truncate,
1444 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1445 .getattr_it = ll_getattr,
1447 .revalidate_it = ll_inode_revalidate_it,