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;
48 memset(&obdo, 0, sizeof(obdo));
49 obdo.o_id = inode->i_ino;
50 obdo.o_mode = inode->i_mode;
51 obdo.o_size = inode->i_size;
52 obdo.o_blocks = inode->i_blocks;
53 if (0 /* ll_is_inode_dirty(inode) */) {
54 obdo.o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
55 valid |= OBD_MD_FLFLAGS;
58 rc = mdc_close(mdc_exp, &obdo, och, &req);
60 /* We are the last writer, so the MDS has instructed us to get
61 * the file size and any write cookies, then close again. */
62 //ll_queue_done_writing(inode);
65 CERROR("inode %lu mdc close failed: rc = %d\n",
69 rc = ll_objects_destroy(req, file->f_dentry->d_inode);
71 CERROR("inode %lu ll_objects destroy: rc = %d\n",
75 mdc_clear_open_replay_data(och);
76 ptlrpc_req_finished(req);
77 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
78 file->private_data = NULL;
79 OBD_SLAB_FREE(fd, ll_file_data_slab, sizeof *fd);
84 /* While this returns an error code, fput() the caller does not, so we need
85 * to make every effort to clean up all of our state here. Also, applications
86 * rarely check close errors and even if an error is returned they will not
87 * re-try the close call.
89 int ll_file_release(struct inode *inode, struct file *file)
91 struct ll_file_data *fd;
92 struct ll_sb_info *sbi = ll_i2sbi(inode);
96 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
97 inode->i_generation, inode);
99 /* don't do anything for / */
100 if (inode->i_sb->s_root == file->f_dentry)
103 lprocfs_counter_incr(sbi->ll_stats, LPROC_LL_RELEASE);
104 fd = (struct ll_file_data *)file->private_data;
107 rc = ll_mdc_close(sbi->ll_mdc_exp, inode, file);
111 static int ll_intent_file_open(struct file *file, void *lmm,
112 int lmmsize, struct lookup_intent *itp)
114 struct ll_sb_info *sbi = ll_i2sbi(file->f_dentry->d_inode);
115 struct lustre_handle lockh;
116 struct mdc_op_data data;
117 struct dentry *parent = file->f_dentry->d_parent;
118 const char *name = file->f_dentry->d_name.name;
119 const int len = file->f_dentry->d_name.len;
125 ll_prepare_mdc_op_data(&data, parent->d_inode, NULL, name, len, O_RDWR);
127 rc = mdc_enqueue(sbi->ll_mdc_exp, LDLM_PLAIN, itp, LCK_PR, &data,
128 &lockh, lmm, lmmsize, ldlm_completion_ast,
129 ll_mdc_blocking_ast, parent->d_inode);
131 CERROR("lock enqueue: err: %d\n", rc);
135 static int ll_local_open(struct file *file, struct lookup_intent *it)
137 struct ptlrpc_request *req = it->d.lustre.it_data;
138 struct ll_inode_info *lli = ll_i2info(file->f_dentry->d_inode);
139 struct ll_file_data *fd;
140 struct mds_body *body;
143 body = lustre_msg_buf (req->rq_repmsg, 1, sizeof (*body));
144 LASSERT (body != NULL); /* reply already checked out */
145 LASSERT_REPSWABBED (req, 1); /* and swabbed down */
147 LASSERT(!file->private_data);
149 OBD_SLAB_ALLOC(fd, ll_file_data_slab, SLAB_KERNEL, sizeof *fd);
150 /* We can't handle this well without reorganizing ll_file_open and
151 * ll_mdc_close, so don't even try right now. */
154 memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
155 fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
156 file->private_data = fd;
157 ll_readahead_init(file->f_dentry->d_inode, &fd->fd_ras);
159 lli->lli_io_epoch = body->io_epoch;
161 mdc_set_open_replay_data(&fd->fd_mds_och, it->d.lustre.it_data);
166 /* Open a file, and (for the very first open) create objects on the OSTs at
167 * this time. If opened with O_LOV_DELAY_CREATE, then we don't do the object
168 * creation or open until ll_lov_setstripe() ioctl is called. We grab
169 * lli_open_sem to ensure no other process will create objects, send the
170 * stripe MD to the MDS, or try to destroy the objects if that fails.
172 * If we already have the stripe MD locally then we don't request it in
173 * mdc_open(), by passing a lmm_size = 0.
175 * It is up to the application to ensure no other processes open this file
176 * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
177 * used. We might be able to avoid races of that sort by getting lli_open_sem
178 * before returning in the O_LOV_DELAY_CREATE case and dropping it here
179 * or in ll_file_release(), but I'm not sure that is desirable/necessary.
181 int ll_file_open(struct inode *inode, struct file *file)
183 struct ll_inode_info *lli = ll_i2info(inode);
184 struct lookup_intent *it;
185 struct lov_stripe_md *lsm;
186 struct ptlrpc_request *req;
190 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
191 inode->i_generation, inode);
193 /* don't do anything for / */
194 if (inode->i_sb->s_root == file->f_dentry)
199 if (!it->d.lustre.it_disposition) {
200 struct lookup_intent oit = { .it_op = IT_OPEN,
201 .it_flags = file->f_flags };
203 rc = ll_intent_file_open(file, NULL, 0, it);
208 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_OPEN);
209 rc = it_open_error(DISP_OPEN_OPEN, it);
213 rc = ll_local_open(file, it);
217 if (!S_ISREG(inode->i_mode))
222 if (file->f_flags & O_LOV_DELAY_CREATE ||
223 !(file->f_mode & FMODE_WRITE)) {
224 CDEBUG(D_INODE, "object creation was delayed\n");
228 file->f_flags &= ~O_LOV_DELAY_CREATE;
231 req = it->d.lustre.it_data;
232 ptlrpc_req_finished(req);
234 ll_open_complete(inode);
238 /* Fills the obdo with the attributes for the inode defined by lsm */
239 int ll_lsm_getattr(struct obd_export *exp, struct lov_stripe_md *lsm,
242 struct ptlrpc_request_set *set;
246 LASSERT(lsm != NULL);
248 memset(oa, 0, sizeof *oa);
249 oa->o_id = lsm->lsm_object_id;
250 oa->o_mode = S_IFREG;
251 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
252 OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
255 set = ptlrpc_prep_set();
257 CERROR ("ENOMEM allocing request set\n");
260 rc = obd_getattr_async(exp, oa, lsm, set);
262 rc = ptlrpc_set_wait(set);
263 ptlrpc_set_destroy(set);
268 oa->o_valid &= (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
269 OBD_MD_FLCTIME | OBD_MD_FLSIZE);
273 static inline void ll_remove_suid(struct inode *inode)
277 /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
278 mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
280 /* was any of the uid bits set? */
281 mode &= inode->i_mode;
282 if (mode && !capable(CAP_FSETID)) {
283 inode->i_mode &= ~mode;
284 // XXX careful here - we cannot change the size
288 static int ll_lock_to_stripe_offset(struct inode *inode, struct ldlm_lock *lock)
290 struct ll_inode_info *lli = ll_i2info(inode);
291 struct lov_stripe_md *lsm = lli->lli_smd;
292 struct obd_export *exp = ll_i2obdexp(inode);
295 struct ldlm_lock *lock;
296 struct lov_stripe_md *lsm;
297 } key = { .name = "lock_to_stripe", .lock = lock, .lsm = lsm };
298 __u32 stripe, vallen = sizeof(stripe);
302 if (lsm->lsm_stripe_count == 1)
305 /* get our offset in the lov */
306 rc = obd_get_info(exp, sizeof(key), &key, &vallen, &stripe);
308 CERROR("obd_get_info: rc = %d\n", rc);
311 LASSERT(stripe < lsm->lsm_stripe_count);
315 /* Flush the page cache for an extent as its canceled. When we're on an LOV,
316 * we get a lock cancellation for each stripe, so we have to map the obd's
317 * region back onto the stripes in the file that it held.
319 * No one can dirty the extent until we've finished our work and they can
320 * enqueue another lock. The DLM protects us from ll_file_read/write here,
321 * but other kernel actors could have pages locked.
323 * Called with the DLM lock held. */
324 void ll_pgcache_remove_extent(struct inode *inode, struct lov_stripe_md *lsm,
325 struct ldlm_lock *lock, __u32 stripe)
327 ldlm_policy_data_t tmpex;
328 unsigned long start, end, count, skip, i, j;
330 int rc, rc2, discard = lock->l_flags & LDLM_FL_DISCARD_DATA;
331 struct lustre_handle lockh;
334 memcpy(&tmpex, &lock->l_policy_data, sizeof(tmpex));
335 CDEBUG(D_INODE|D_PAGE, "inode %lu(%p) ["LPU64"->"LPU64"] size: %llu\n",
336 inode->i_ino, inode, tmpex.l_extent.start, tmpex.l_extent.end,
339 /* our locks are page granular thanks to osc_enqueue, we invalidate the
341 LASSERT((tmpex.l_extent.start & ~PAGE_CACHE_MASK) == 0);
342 LASSERT(((tmpex.l_extent.end + 1) & ~PAGE_CACHE_MASK) == 0);
346 start = tmpex.l_extent.start >> PAGE_CACHE_SHIFT;
347 end = tmpex.l_extent.end >> PAGE_CACHE_SHIFT;
348 if (lsm->lsm_stripe_count > 1) {
349 count = lsm->lsm_stripe_size >> PAGE_CACHE_SHIFT;
350 skip = (lsm->lsm_stripe_count - 1) * count;
351 start += start/count * skip + stripe * count;
353 end += end/count * skip + stripe * count;
355 if (end < tmpex.l_extent.end >> PAGE_CACHE_SHIFT)
358 i = (inode->i_size + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
362 CDEBUG(D_INODE|D_PAGE, "walking page indices start: %lu j: %lu "
363 "count: %lu skip: %lu end: %lu%s\n", start, start % count,
364 count, skip, end, discard ? " (DISCARDING)" : "");
366 /* this is the simplistic implementation of page eviction at
367 * cancelation. It is careful to get races with other page
368 * lockers handled correctly. fixes from bug 20 will make it
369 * more efficient by associating locks with pages and with
370 * batching writeback under the lock explicitly. */
371 for (i = start, j = start % count ; i <= end;
372 j++, i++, tmpex.l_extent.start += PAGE_CACHE_SIZE) {
373 LASSERTF(tmpex.l_extent.start< lock->l_policy_data.l_extent.end,
374 LPU64" >= "LPU64" start %lu i %lu end %lu\n",
375 tmpex.l_extent.start, lock->l_policy_data.l_extent.end,
378 ll_pgcache_lock(inode->i_mapping);
379 if (list_empty(&inode->i_mapping->dirty_pages) &&
380 list_empty(&inode->i_mapping->clean_pages) &&
381 list_empty(&inode->i_mapping->locked_pages)) {
382 CDEBUG(D_INODE|D_PAGE, "nothing left\n");
383 ll_pgcache_unlock(inode->i_mapping);
386 ll_pgcache_unlock(inode->i_mapping);
388 conditional_schedule();
390 page = find_get_page(inode->i_mapping, i);
393 LL_CDEBUG_PAGE(D_PAGE, page, "locking page\n");
396 /* page->mapping to check with racing against teardown */
397 if (page->mapping && PageDirty(page) && !discard) {
398 ClearPageDirty(page);
399 LL_CDEBUG_PAGE(D_PAGE, page, "found dirty\n");
400 ll_pgcache_lock(inode->i_mapping);
401 list_del(&page->list);
402 list_add(&page->list, &inode->i_mapping->locked_pages);
403 ll_pgcache_unlock(inode->i_mapping);
405 rc = ll_call_writepage(inode, page);
407 CERROR("writepage of page %p failed: %d\n",
409 /* either waiting for io to complete or reacquiring
410 * the lock that the failed writepage released */
414 tmpex.l_extent.end = tmpex.l_extent.start + PAGE_CACHE_SIZE - 1;
415 /* check to see if another DLM lock covers this page */
416 rc2 = ldlm_lock_match(lock->l_resource->lr_namespace,
417 LDLM_FL_BLOCK_GRANTED|LDLM_FL_CBPENDING |
419 &lock->l_resource->lr_name, LDLM_EXTENT,
420 &tmpex, LCK_PR | LCK_PW, &lockh);
421 if (rc2 == 0 && page->mapping != NULL) {
422 // checking again to account for writeback's lock_page()
423 LL_CDEBUG_PAGE(D_PAGE, page, "truncating\n");
424 ll_truncate_complete_page(page);
427 page_cache_release(page);
438 static int ll_extent_lock_callback(struct ldlm_lock *lock,
439 struct ldlm_lock_desc *new, void *data,
442 struct lustre_handle lockh = { 0 };
446 if ((unsigned long)data > 0 && (unsigned long)data < 0x1000) {
447 LDLM_ERROR(lock, "cancelling lock with bad data %p", data);
452 case LDLM_CB_BLOCKING:
453 ldlm_lock2handle(lock, &lockh);
454 rc = ldlm_cli_cancel(&lockh);
456 CERROR("ldlm_cli_cancel failed: %d\n", rc);
458 case LDLM_CB_CANCELING: {
460 struct ll_inode_info *lli;
461 struct lov_stripe_md *lsm;
465 /* This lock wasn't granted, don't try to evict pages */
466 if (lock->l_req_mode != lock->l_granted_mode)
469 inode = ll_inode_from_lock(lock);
472 lli = ll_i2info(inode);
475 if (lli->lli_smd == NULL)
479 stripe = ll_lock_to_stripe_offset(inode, lock);
480 ll_pgcache_remove_extent(inode, lsm, lock, stripe);
483 kms = ldlm_extent_shift_kms(lock,
484 lsm->lsm_oinfo[stripe].loi_kms);
485 if (lsm->lsm_oinfo[stripe].loi_kms != kms)
486 LDLM_DEBUG(lock, "updating kms from "LPU64" to "LPU64,
487 lsm->lsm_oinfo[stripe].loi_kms, kms);
488 lsm->lsm_oinfo[stripe].loi_kms = kms;
490 //ll_try_done_writing(inode);
503 int ll_async_completion_ast(struct ldlm_lock *lock, int flags, void *data)
505 /* XXX ALLOCATE - 160 bytes */
506 struct inode *inode = ll_inode_from_lock(lock);
507 struct ll_inode_info *lli = ll_i2info(inode);
508 struct lustre_handle lockh = { 0 };
513 if (flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
514 LDLM_FL_BLOCK_CONV)) {
515 LBUG(); /* not expecting any blocked async locks yet */
516 LDLM_DEBUG(lock, "client-side async enqueue returned a blocked "
518 ldlm_lock_dump(D_OTHER, lock, 0);
519 ldlm_reprocess_all(lock->l_resource);
523 LDLM_DEBUG(lock, "client-side async enqueue: granted/glimpsed");
525 stripe = ll_lock_to_stripe_offset(inode, lock);
527 if (lock->l_lvb_len) {
528 struct lov_stripe_md *lsm = lli->lli_smd;
530 lvb = lock->l_lvb_data;
531 lsm->lsm_oinfo[stripe].loi_rss = lvb->lvb_size;
534 kms = MAX(lsm->lsm_oinfo[stripe].loi_kms, lvb->lvb_size);
535 kms = ldlm_extent_shift_kms(NULL, kms);
536 if (lsm->lsm_oinfo[stripe].loi_kms != kms)
537 LDLM_DEBUG(lock, "updating kms from "LPU64" to "LPU64,
538 lsm->lsm_oinfo[stripe].loi_kms, kms);
539 lsm->lsm_oinfo[stripe].loi_kms = kms;
544 wake_up(&lock->l_waitq);
546 ldlm_lock2handle(lock, &lockh);
547 ldlm_lock_decref(&lockh, LCK_PR);
552 static int ll_glimpse_callback(struct ldlm_lock *lock, void *reqp)
554 struct ptlrpc_request *req = reqp;
555 struct inode *inode = ll_inode_from_lock(lock);
556 struct ll_inode_info *lli;
558 int rc, size = sizeof(*lvb), stripe = 0;
562 GOTO(out, rc = -ELDLM_NO_LOCK_DATA);
563 lli = ll_i2info(inode);
565 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
566 if (lli->lli_smd == NULL)
567 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
569 /* First, find out which stripe index this lock corresponds to. */
570 if (lli->lli_smd->lsm_stripe_count > 1)
571 stripe = ll_lock_to_stripe_offset(inode, lock);
573 rc = lustre_pack_reply(req, 1, &size, NULL);
575 CERROR("lustre_pack_reply: %d\n", rc);
579 lvb = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*lvb));
580 lvb->lvb_size = lli->lli_smd->lsm_oinfo[stripe].loi_kms;
582 LDLM_DEBUG(lock, "i_size: %llu -> stripe number %u -> kms "LPU64,
583 inode->i_size, stripe, lvb->lvb_size);
589 /* These errors are normal races, so we don't want to fill the console
590 * with messages by calling ptlrpc_error() */
591 if (rc == -ELDLM_NO_LOCK_DATA)
592 lustre_pack_reply(req, 0, NULL, NULL);
598 __u64 lov_merge_size(struct lov_stripe_md *lsm, int kms);
599 __u64 lov_merge_mtime(struct lov_stripe_md *lsm, __u64 current_time);
601 /* NB: lov_merge_size will prefer locally cached writes if they extend the
602 * file (because it prefers KMS over RSS when larger) */
603 int ll_glimpse_size(struct inode *inode, struct ost_lvb *lvb)
605 struct ll_inode_info *lli = ll_i2info(inode);
606 struct ll_sb_info *sbi = ll_i2sbi(inode);
607 ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
608 struct lustre_handle lockh;
609 int rc, flags = LDLM_FL_HAS_INTENT;
612 CDEBUG(D_DLMTRACE, "Glimpsing inode %lu\n", inode->i_ino);
614 rc = obd_enqueue(sbi->ll_osc_exp, lli->lli_smd, LDLM_EXTENT, &policy,
615 LCK_PR, &flags, ll_extent_lock_callback,
616 ldlm_completion_ast, ll_glimpse_callback, inode,
617 sizeof(*lvb), lustre_swab_ost_lvb, &lockh);
619 CERROR("obd_enqueue returned rc %d, returning -EIO\n", rc);
623 lvb->lvb_size = lov_merge_size(lli->lli_smd, 0);
624 //inode->i_mtime = lov_merge_mtime(lli->lli_smd, inode->i_mtime);
626 CDEBUG(D_DLMTRACE, "glimpse: size: "LPU64"\n", lvb->lvb_size);
628 obd_cancel(sbi->ll_osc_exp, lli->lli_smd, LCK_PR, &lockh);
633 int ll_extent_lock(struct ll_file_data *fd, struct inode *inode,
634 struct lov_stripe_md *lsm, int mode,
635 ldlm_policy_data_t *policy, struct lustre_handle *lockh,
638 struct ll_sb_info *sbi = ll_i2sbi(inode);
642 LASSERT(lockh->cookie == 0);
644 /* XXX phil: can we do this? won't it screw the file size up? */
645 if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
646 (sbi->ll_flags & LL_SBI_NOLCK))
649 CDEBUG(D_DLMTRACE, "Locking inode %lu, start "LPU64" end "LPU64"\n",
650 inode->i_ino, policy->l_extent.start, policy->l_extent.end);
652 rc = obd_enqueue(sbi->ll_osc_exp, lsm, LDLM_EXTENT, policy, mode,
653 &ast_flags, ll_extent_lock_callback,
654 ldlm_completion_ast, ll_glimpse_callback, inode,
655 sizeof(struct ost_lvb), lustre_swab_ost_lvb, lockh);
659 if (policy->l_extent.start == 0 &&
660 policy->l_extent.end == OBD_OBJECT_EOF)
661 inode->i_size = lov_merge_size(lsm, 1);
663 //inode->i_mtime = lov_merge_mtime(lsm, inode->i_mtime);
668 int ll_extent_unlock(struct ll_file_data *fd, struct inode *inode,
669 struct lov_stripe_md *lsm, int mode,
670 struct lustre_handle *lockh)
672 struct ll_sb_info *sbi = ll_i2sbi(inode);
676 /* XXX phil: can we do this? won't it screw the file size up? */
677 if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
678 (sbi->ll_flags & LL_SBI_NOLCK))
681 rc = obd_cancel(sbi->ll_osc_exp, lsm, mode, lockh);
686 static ssize_t ll_file_read(struct file *filp, char *buf, size_t count,
689 struct ll_file_data *fd = filp->private_data;
690 struct inode *inode = filp->f_dentry->d_inode;
691 struct ll_inode_info *lli = ll_i2info(inode);
692 struct lov_stripe_md *lsm = lli->lli_smd;
693 struct lustre_handle lockh = { 0 };
694 ldlm_policy_data_t policy;
699 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
700 inode->i_ino, inode->i_generation, inode, count, *ppos);
702 /* "If nbyte is 0, read() will return 0 and have no other results."
703 * -- Single Unix Spec */
707 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_READ_BYTES,
713 policy.l_extent.start = *ppos;
714 policy.l_extent.end = *ppos + count - 1;
716 err = ll_extent_lock(fd, inode, lsm, LCK_PR, &policy, &lockh, 0);
720 kms = lov_merge_size(lsm, 1);
721 if (policy.l_extent.end > kms) {
722 /* A glimpse is necessary to determine whether we return a short
723 * read or some zeroes at the end of the buffer */
725 retval = ll_glimpse_size(inode, &lvb);
728 inode->i_size = lvb.lvb_size;
733 CDEBUG(D_INFO, "Read ino %lu, "LPSZ" bytes, offset %lld, i_size %llu\n",
734 inode->i_ino, count, *ppos, inode->i_size);
736 /* turn off the kernel's read-ahead */
737 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
740 filp->f_ra.ra_pages = 0;
742 retval = generic_file_read(filp, buf, count, ppos);
745 ll_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
750 * Write to a file (through the page cache).
752 static ssize_t ll_file_write(struct file *file, const char *buf, size_t count,
755 struct ll_file_data *fd = file->private_data;
756 struct inode *inode = file->f_dentry->d_inode;
757 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
758 struct lustre_handle lockh = { 0 };
759 ldlm_policy_data_t policy;
760 loff_t maxbytes = ll_file_maxbytes(inode);
764 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
765 inode->i_ino, inode->i_generation, inode, count, *ppos);
767 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
769 /* POSIX, but surprised the VFS doesn't check this already */
773 /* If file was opened for LL_IOC_LOV_SETSTRIPE but the ioctl wasn't
774 * called on the file, don't fail the below assertion (bug 2388). */
775 if (file->f_flags & O_LOV_DELAY_CREATE && lsm == NULL)
780 if (file->f_flags & O_APPEND) {
781 policy.l_extent.start = 0;
782 policy.l_extent.end = OBD_OBJECT_EOF;
784 policy.l_extent.start = *ppos;
785 policy.l_extent.end = *ppos + count - 1;
788 err = ll_extent_lock(fd, inode, lsm, LCK_PW, &policy, &lockh, 0);
792 /* this is ok, g_f_w will overwrite this under i_sem if it races
793 * with a local truncate, it just makes our maxbyte checking easier */
794 if (file->f_flags & O_APPEND)
795 *ppos = inode->i_size;
797 if (*ppos >= maxbytes) {
798 if (count || *ppos > maxbytes) {
799 send_sig(SIGXFSZ, current, 0);
800 GOTO(out, retval = -EFBIG);
803 if (*ppos + count > maxbytes)
804 count = maxbytes - *ppos;
806 CDEBUG(D_INFO, "Writing inode %lu, "LPSZ" bytes, offset %Lu\n",
807 inode->i_ino, count, *ppos);
809 /* generic_file_write handles O_APPEND after getting i_sem */
810 retval = generic_file_write(file, buf, count, ppos);
814 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_WRITE_BYTES,
816 ll_extent_unlock(fd, inode, lsm, LCK_PW, &lockh);
820 static int ll_lov_recreate_obj(struct inode *inode, struct file *file,
823 struct ll_inode_info *lli = ll_i2info(inode);
824 struct obd_export *exp = ll_i2obdexp(inode);
825 struct ll_recreate_obj ucreatp;
826 struct obd_trans_info oti = { 0 };
827 struct obdo *oa = NULL;
830 struct lov_stripe_md *lsm, *lsm2;
833 if (!capable (CAP_SYS_ADMIN))
836 rc = copy_from_user(&ucreatp, (struct ll_recreate_obj *)arg,
837 sizeof(struct ll_recreate_obj));
846 down(&lli->lli_open_sem);
849 up(&lli->lli_open_sem);
853 lsm_size = sizeof(*lsm) + (sizeof(struct lov_oinfo) *
854 (lsm->lsm_stripe_count));
856 OBD_ALLOC(lsm2, lsm_size);
858 up(&lli->lli_open_sem);
863 oa->o_id = ucreatp.lrc_id;
864 oa->o_nlink = ucreatp.lrc_ost_idx;
865 oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS;
866 oa->o_flags |= OBD_FL_RECREATE_OBJS;
867 obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
868 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
870 oti.oti_objid = NULL;
871 memcpy(lsm2, lsm, lsm_size);
872 rc = obd_create(exp, oa, &lsm2, &oti);
874 up(&lli->lli_open_sem);
875 OBD_FREE(lsm2, lsm_size);
880 static int ll_lov_setstripe_ea_info(struct inode *inode, struct file *file,
881 int flags, struct lov_user_md *lum, int lum_size)
883 struct ll_inode_info *lli = ll_i2info(inode);
885 struct obd_export *exp = ll_i2obdexp(inode);
886 struct lov_stripe_md *lsm;
887 struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
888 struct ptlrpc_request *req = NULL;
893 down(&lli->lli_open_sem);
896 up(&lli->lli_open_sem);
897 CDEBUG(D_IOCTL, "stripe already exists for ino %lu\n",
902 f = get_empty_filp();
906 f->f_dentry = file->f_dentry;
907 f->f_vfsmnt = file->f_vfsmnt;
909 rc = ll_intent_file_open(f, lum, lum_size, &oit);
912 if (it_disposition(&oit, DISP_LOOKUP_NEG))
914 req = oit.d.lustre.it_data;
915 rc = oit.d.lustre.it_status;
920 rc = mdc_req2lustre_md(req, 1, exp, &md);
923 ll_update_inode(f->f_dentry->d_inode, md.body, md.lsm);
925 rc = ll_local_open(f, &oit);
928 ll_intent_release(&oit);
930 rc = ll_file_release(f->f_dentry->d_inode, f);
935 up(&lli->lli_open_sem);
937 ptlrpc_req_finished(req);
941 static int ll_lov_setea(struct inode *inode, struct file *file,
944 int flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
945 struct lov_user_md *lump;
946 int lum_size = sizeof(struct lov_user_md) +
947 sizeof(struct lov_user_ost_data);
951 if (!capable (CAP_SYS_ADMIN))
954 OBD_ALLOC(lump, lum_size);
958 rc = copy_from_user(lump, (struct lov_user_md *)arg,
961 OBD_FREE(lump, lum_size);
965 rc = ll_lov_setstripe_ea_info(inode, file, flags, lump, lum_size);
967 OBD_FREE(lump, lum_size);
971 static int ll_lov_setstripe(struct inode *inode, struct file *file,
974 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
976 int flags = FMODE_WRITE;
979 /* Bug 1152: copy properly when this is no longer true */
980 LASSERT(sizeof(lum) == sizeof(*lump));
981 LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
982 rc = copy_from_user(&lum, lump, sizeof(lum));
986 rc = ll_lov_setstripe_ea_info(inode, file, flags, &lum, sizeof(lum));
990 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
992 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
997 return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2obdexp(inode), 0, lsm,
1001 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
1004 struct ll_file_data *fd = file->private_data;
1008 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),cmd=%x\n", inode->i_ino,
1009 inode->i_generation, inode, cmd);
1011 if (_IOC_TYPE(cmd) == 'T') /* tty ioctls */
1014 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
1016 case LL_IOC_GETFLAGS:
1017 /* Get the current value of the file flags */
1018 return put_user(fd->fd_flags, (int *)arg);
1019 case LL_IOC_SETFLAGS:
1020 case LL_IOC_CLRFLAGS:
1021 /* Set or clear specific file flags */
1022 /* XXX This probably needs checks to ensure the flags are
1023 * not abused, and to handle any flag side effects.
1025 if (get_user(flags, (int *) arg))
1028 if (cmd == LL_IOC_SETFLAGS)
1029 fd->fd_flags |= flags;
1031 fd->fd_flags &= ~flags;
1033 case LL_IOC_LOV_SETSTRIPE:
1034 RETURN(ll_lov_setstripe(inode, file, arg));
1035 case LL_IOC_LOV_SETEA:
1036 RETURN( ll_lov_setea(inode, file, arg) );
1037 case LL_IOC_LOV_GETSTRIPE:
1038 RETURN(ll_lov_getstripe(inode, arg));
1039 case LL_IOC_RECREATE_OBJ:
1040 RETURN(ll_lov_recreate_obj(inode, file, arg));
1041 case EXT3_IOC_GETFLAGS:
1042 case EXT3_IOC_SETFLAGS:
1043 RETURN( ll_iocontrol(inode, file, cmd, arg) );
1044 /* We need to special case any other ioctls we want to handle,
1045 * to send them to the MDS/OST as appropriate and to properly
1046 * network encode the arg field.
1047 case EXT2_IOC_GETVERSION_OLD:
1048 case EXT2_IOC_GETVERSION_NEW:
1049 case EXT2_IOC_SETVERSION_OLD:
1050 case EXT2_IOC_SETVERSION_NEW:
1053 RETURN( obd_iocontrol(cmd, ll_i2obdexp(inode), 0, NULL,
1058 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
1060 struct inode *inode = file->f_dentry->d_inode;
1061 struct ll_file_data *fd = file->private_data;
1062 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1063 struct lustre_handle lockh = {0};
1066 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),to=%llu\n", inode->i_ino,
1067 inode->i_generation, inode,
1068 offset + ((origin==2) ? inode->i_size : file->f_pos));
1070 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_LLSEEK);
1071 if (origin == 2) { /* SEEK_END */
1073 ldlm_policy_data_t policy = { .l_extent = {0, OBD_OBJECT_EOF }};
1074 err = ll_extent_lock(fd, inode, lsm, LCK_PR, &policy, &lockh,0);
1075 if (err != ELDLM_OK)
1078 offset += inode->i_size;
1079 } else if (origin == 1) { /* SEEK_CUR */
1080 offset += file->f_pos;
1084 if (offset >= 0 && offset <= ll_file_maxbytes(inode)) {
1085 if (offset != file->f_pos) {
1086 file->f_pos = offset;
1087 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1089 file->f_version = ++event;
1096 ll_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
1100 int ll_fsync(struct file *file, struct dentry *dentry, int data)
1102 struct inode *inode = dentry->d_inode;
1103 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1105 struct ptlrpc_request *req;
1108 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1109 inode->i_generation, inode);
1111 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_FSYNC);
1113 /* fsync's caller has already called _fdata{sync,write}, we want
1114 * that IO to finish before calling the osc and mdc sync methods */
1115 rc = filemap_fdatawait(inode->i_mapping);
1117 ll_inode2fid(&fid, inode);
1118 err = mdc_sync(ll_i2sbi(inode)->ll_mdc_exp, &fid, &req);
1122 ptlrpc_req_finished(req);
1125 struct obdo *oa = obdo_alloc();
1128 RETURN(rc ? rc : -ENOMEM);
1130 oa->o_id = lsm->lsm_object_id;
1131 oa->o_valid = OBD_MD_FLID;
1132 obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
1133 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
1135 err = obd_sync(ll_i2sbi(inode)->ll_osc_exp, oa, lsm,
1145 int ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
1147 struct inode *inode = file->f_dentry->d_inode;
1148 struct ll_sb_info *sbi = ll_i2sbi(inode);
1149 struct obd_device *obddev;
1150 struct ldlm_res_id res_id =
1151 { .name = {inode->i_ino, inode->i_generation, LDLM_FLOCK} };
1152 struct lustre_handle lockh = {0};
1153 ldlm_policy_data_t flock;
1154 ldlm_mode_t mode = 0;
1159 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu file_lock=%p\n",
1160 inode->i_ino, file_lock);
1162 flock.l_flock.pid = file_lock->fl_pid;
1163 flock.l_flock.start = file_lock->fl_start;
1164 flock.l_flock.end = file_lock->fl_end;
1166 switch (file_lock->fl_type) {
1171 /* An unlock request may or may not have any relation to
1172 * existing locks so we may not be able to pass a lock handle
1173 * via a normal ldlm_lock_cancel() request. The request may even
1174 * unlock a byte range in the middle of an existing lock. In
1175 * order to process an unlock request we need all of the same
1176 * information that is given with a normal read or write record
1177 * lock request. To avoid creating another ldlm unlock (cancel)
1178 * message we'll treat a LCK_NL flock request as an unlock. */
1185 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1194 flags = LDLM_FL_BLOCK_NOWAIT;
1197 flags = LDLM_FL_TEST_LOCK;
1198 /* Save the old mode so that if the mode in the lock changes we
1199 * can decrement the appropriate reader or writer refcount. */
1200 file_lock->fl_type = mode;
1203 CERROR("unknown fcntl lock command: %d\n", cmd);
1207 CDEBUG(D_DLMTRACE, "inode=%lu, pid=%u, flags=%#x, mode=%u, "
1208 "start="LPU64", end="LPU64"\n", inode->i_ino, flock.l_flock.pid,
1209 flags, mode, flock.l_flock.start, flock.l_flock.end);
1211 obddev = sbi->ll_mdc_exp->exp_obd;
1212 rc = ldlm_cli_enqueue(sbi->ll_mdc_exp, NULL, obddev->obd_namespace,
1213 res_id, LDLM_FLOCK, &flock, mode, &flags,
1214 NULL, ldlm_flock_completion_ast, NULL, file_lock,
1215 NULL, 0, NULL, &lockh);
1219 static int ll_have_md_lock(struct dentry *de)
1221 struct ll_sb_info *sbi = ll_s2sbi(de->d_sb);
1222 struct lustre_handle lockh;
1223 struct ldlm_res_id res_id = { .name = {0} };
1224 struct obd_device *obddev;
1231 obddev = sbi->ll_mdc_exp->exp_obd;
1232 res_id.name[0] = de->d_inode->i_ino;
1233 res_id.name[1] = de->d_inode->i_generation;
1235 CDEBUG(D_INFO, "trying to match res "LPU64"\n", res_id.name[0]);
1237 /* FIXME use LDLM_FL_TEST_LOCK instead */
1238 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
1239 if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_PLAIN,
1240 NULL, LCK_PR, &lockh)) {
1241 ldlm_lock_decref(&lockh, LCK_PR);
1245 if (ldlm_lock_match(obddev->obd_namespace, flags, &res_id, LDLM_PLAIN,
1246 NULL, LCK_PW, &lockh)) {
1247 ldlm_lock_decref(&lockh, LCK_PW);
1253 int ll_inode_revalidate_it(struct dentry *dentry, struct lookup_intent *it)
1255 struct inode *inode = dentry->d_inode;
1256 struct ll_inode_info *lli;
1257 struct lov_stripe_md *lsm;
1261 CERROR("REPORT THIS LINE TO PETER\n");
1264 lli = ll_i2info(inode);
1265 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),name=%s\n",
1266 inode->i_ino, inode->i_generation, inode, dentry->d_name.name);
1267 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
1268 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_REVALIDATE);
1271 if (!ll_have_md_lock(dentry)) {
1272 struct ptlrpc_request *req = NULL;
1273 struct ll_sb_info *sbi = ll_i2sbi(dentry->d_inode);
1275 unsigned long valid = 0;
1278 if (S_ISREG(inode->i_mode)) {
1279 ealen = obd_size_diskmd(sbi->ll_osc_exp, NULL);
1280 valid |= OBD_MD_FLEASIZE;
1282 ll_inode2fid(&fid, inode);
1283 rc = mdc_getattr(sbi->ll_mdc_exp, &fid, valid, ealen, &req);
1285 CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1288 rc = ll_prep_inode(sbi->ll_osc_exp, &inode, req, 0, NULL);
1290 ptlrpc_req_finished(req);
1293 ptlrpc_req_finished(req);
1297 if (lsm == NULL) /* object not yet allocated, don't validate size */
1300 /* ll_glimpse_size will prefer locally cached writes if they extend
1306 err = ll_glimpse_size(inode, &lvb);
1307 inode->i_size = lvb.lvb_size;
1312 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1313 int ll_getattr(struct vfsmount *mnt, struct dentry *de,
1314 struct lookup_intent *it, struct kstat *stat)
1317 struct inode *inode = de->d_inode;
1319 res = ll_inode_revalidate_it(de, it);
1320 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_GETATTR);
1325 stat->dev = inode->i_sb->s_dev;
1326 stat->ino = inode->i_ino;
1327 stat->mode = inode->i_mode;
1328 stat->nlink = inode->i_nlink;
1329 stat->uid = inode->i_uid;
1330 stat->gid = inode->i_gid;
1331 stat->rdev = kdev_t_to_nr(inode->i_rdev);
1332 stat->atime = inode->i_atime;
1333 stat->mtime = inode->i_mtime;
1334 stat->ctime = inode->i_ctime;
1335 stat->size = inode->i_size;
1336 stat->blksize = inode->i_blksize;
1337 stat->blocks = inode->i_blocks;
1342 struct file_operations ll_file_operations = {
1344 write: ll_file_write,
1345 ioctl: ll_file_ioctl,
1347 release: ll_file_release,
1348 mmap: generic_file_mmap,
1349 llseek: ll_file_seek,
1351 //lock: ll_file_flock
1354 struct inode_operations ll_file_inode_operations = {
1355 setattr_raw: ll_setattr_raw,
1356 setattr: ll_setattr,
1357 truncate: ll_truncate,
1358 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1359 getattr_it: ll_getattr,
1361 revalidate_it: ll_inode_revalidate_it,
1365 struct inode_operations ll_special_inode_operations = {
1366 setattr_raw: ll_setattr_raw,
1367 setattr: ll_setattr,
1368 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1369 getattr_it: ll_getattr,
1371 revalidate_it: ll_inode_revalidate_it,