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 #include <linux/lustre_acl.h>
31 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
32 #include <linux/lustre_compat25.h>
34 #include "llite_internal.h"
35 #include <linux/obd_lov.h>
37 #define XATTR_NAME_MAX 255
38 int ll_md_close(struct obd_export *md_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 obdo *obdo = NULL;
48 /* clear group lock, if present */
49 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
50 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
51 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
52 rc = ll_extent_unlock(fd, inode, lsm, LCK_GROUP,
60 obdo->o_id = inode->i_ino;
61 obdo->o_valid = OBD_MD_FLID;
62 obdo_from_inode(obdo, inode, (OBD_MD_FLTYPE | OBD_MD_FLMODE |
63 OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
64 OBD_MD_FLATIME | OBD_MD_FLMTIME |
66 if (0 /* ll_is_inode_dirty(inode) */) {
67 obdo->o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
68 obdo->o_valid |= OBD_MD_FLFLAGS;
70 obdo->o_mds = id_group(&ll_i2info(inode)->lli_id);
71 rc = md_close(md_exp, obdo, och, &req);
75 /* We are the last writer, so the MDS has instructed us to get
76 * the file size and any write cookies, then close again. */
77 //ll_queue_done_writing(inode);
80 CERROR("inode %lu mdc close failed: rc = %d\n",
84 rc = ll_objects_destroy(req, file->f_dentry->d_inode, 1);
86 CERROR("inode %lu ll_objects destroy: rc = %d\n",
90 mdc_clear_open_replay_data(md_exp, och);
91 ptlrpc_req_finished(req);
92 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
93 file->private_data = NULL;
94 OBD_SLAB_FREE(fd, ll_file_data_slab, sizeof(*fd));
98 /* While this returns an error code, fput() the caller does not, so we need
99 * to make every effort to clean up all of our state here. Also, applications
100 * rarely check close errors and even if an error is returned they will not
101 * re-try the close call.
103 int ll_file_release(struct inode *inode, struct file *file)
105 struct ll_file_data *fd;
106 struct ll_sb_info *sbi = ll_i2sbi(inode);
110 CDEBUG(D_VFSTRACE, "VFS Op:inode="DLID4"(%p)\n",
111 OLID4(&ll_i2info(inode)->lli_id), inode);
113 /* don't do anything for / */
114 if (inode->i_sb->s_root == file->f_dentry)
117 lprocfs_counter_incr(sbi->ll_stats, LPROC_LL_RELEASE);
118 fd = (struct ll_file_data *)file->private_data;
121 rc = ll_md_close(sbi->ll_md_exp, inode, file);
125 static int ll_intent_file_open(struct file *file, void *lmm,
126 int lmmsize, struct lookup_intent *itp)
128 struct ll_sb_info *sbi = ll_i2sbi(file->f_dentry->d_inode);
129 struct dentry *parent = file->f_dentry->d_parent;
130 const char *name = file->f_dentry->d_name.name;
131 const int len = file->f_dentry->d_name.len;
132 struct lustre_handle lockh;
133 struct mdc_op_data *op_data;
139 OBD_ALLOC(op_data, sizeof(*op_data));
142 ll_prepare_mdc_data(op_data, parent->d_inode, NULL, name, len, O_RDWR);
144 rc = md_enqueue(sbi->ll_md_exp, LDLM_IBITS, itp, LCK_PR, op_data,
145 &lockh, lmm, lmmsize, ldlm_completion_ast,
146 ll_mdc_blocking_ast, NULL);
147 OBD_FREE(op_data, sizeof(*op_data));
149 if (LUSTRE_IT(itp)->it_lock_mode)
150 memcpy(&LUSTRE_IT(itp)->it_lock_handle,
151 &lockh, sizeof(lockh));
154 CERROR("lock enqueue: err: %d\n", rc);
160 int ll_local_open(struct file *file, struct lookup_intent *it)
162 struct ptlrpc_request *req = LUSTRE_IT(it)->it_data;
163 struct ll_inode_info *lli = ll_i2info(file->f_dentry->d_inode);
164 struct obd_export *md_exp = ll_i2mdexp(file->f_dentry->d_inode);
165 struct ll_file_data *fd;
166 struct mds_body *body;
169 body = lustre_msg_buf (req->rq_repmsg, 1, sizeof (*body));
170 LASSERT (body != NULL); /* reply already checked out */
171 LASSERT_REPSWABBED (req, 1); /* and swabbed down */
173 LASSERTF(file->private_data == NULL, "file %*s/%*s ino %lu/%u (%o)\n",
174 file->f_dentry->d_name.len, file->f_dentry->d_name.name,
175 file->f_dentry->d_parent->d_name.len,
176 file->f_dentry->d_parent->d_name.name,
177 file->f_dentry->d_inode->i_ino,
178 file->f_dentry->d_inode->i_generation,
179 file->f_dentry->d_inode->i_mode);
182 OBD_SLAB_ALLOC(fd, ll_file_data_slab, SLAB_KERNEL, sizeof *fd);
184 /* We can't handle this well without reorganizing ll_file_open and
185 * ll_md_close(), so don't even try right now. */
188 memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
189 fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
190 file->private_data = fd;
191 ll_readahead_init(file->f_dentry->d_inode, &fd->fd_ras);
193 lli->lli_io_epoch = body->io_epoch;
195 mdc_set_open_replay_data(md_exp, &fd->fd_mds_och, LUSTRE_IT(it)->it_data);
200 /* Open a file, and (for the very first open) create objects on the OSTs at
201 * this time. If opened with O_LOV_DELAY_CREATE, then we don't do the object
202 * creation or open until ll_lov_setstripe() ioctl is called. We grab
203 * lli_open_sem to ensure no other process will create objects, send the
204 * stripe MD to the MDS, or try to destroy the objects if that fails.
206 * If we already have the stripe MD locally then we don't request it in
207 * mdc_open(), by passing a lmm_size = 0.
209 * It is up to the application to ensure no other processes open this file
210 * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
211 * used. We might be able to avoid races of that sort by getting lli_open_sem
212 * before returning in the O_LOV_DELAY_CREATE case and dropping it here
213 * or in ll_file_release(), but I'm not sure that is desirable/necessary.
215 int ll_file_open(struct inode *inode, struct file *file)
217 struct ll_inode_info *lli = ll_i2info(inode);
218 struct lookup_intent *it, oit = { .it_op = IT_OPEN,
219 .it_flags = file->f_flags };
220 struct lov_stripe_md *lsm;
221 struct ptlrpc_request *req;
225 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), flags %o\n", inode->i_ino,
226 inode->i_generation, inode, file->f_flags);
228 /* don't do anything for / */
229 if (inode->i_sb->s_root == file->f_dentry)
234 if (!it || !LUSTRE_IT(it) || !LUSTRE_IT(it)->it_disposition) {
236 rc = ll_intent_alloc(it);
239 rc = ll_intent_file_open(file, NULL, 0, it);
245 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_OPEN);
246 /* mdc_intent_lock() didn't get a request ref if there was an open
247 * error, so don't do cleanup on the request here (bug 3430) */
248 rc = it_open_error(DISP_OPEN_OPEN, it);
252 rc = ll_local_open(file, it);
254 LASSERTF(rc == 0, "rc = %d\n", rc);
256 if (!S_ISREG(inode->i_mode))
261 if (file->f_flags & O_LOV_DELAY_CREATE ||
262 !(file->f_mode & FMODE_WRITE)) {
263 CDEBUG(D_INODE, "object creation was delayed\n");
267 file->f_flags &= ~O_LOV_DELAY_CREATE;
270 req = LUSTRE_IT(it)->it_data;
271 ll_intent_release(it);
273 ptlrpc_req_finished(req);
275 ll_open_complete(inode);
279 /* Fills the obdo with the attributes for the inode defined by lsm */
280 int ll_lsm_getattr(struct obd_export *exp, struct lov_stripe_md *lsm,
283 struct ptlrpc_request_set *set;
287 LASSERT(lsm != NULL);
289 memset(oa, 0, sizeof *oa);
290 oa->o_id = lsm->lsm_object_id;
291 oa->o_gr = lsm->lsm_object_gr;
292 oa->o_mode = S_IFREG;
293 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
294 OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
295 OBD_MD_FLCTIME | OBD_MD_FLGROUP;
297 set = ptlrpc_prep_set();
301 rc = obd_getattr_async(exp, oa, lsm, set);
303 rc = ptlrpc_set_wait(set);
304 ptlrpc_set_destroy(set);
309 oa->o_valid &= (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
310 OBD_MD_FLCTIME | OBD_MD_FLSIZE);
314 static inline void ll_remove_suid(struct inode *inode)
318 /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
319 mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
321 /* was any of the uid bits set? */
322 mode &= inode->i_mode;
323 if (mode && !capable(CAP_FSETID)) {
324 inode->i_mode &= ~mode;
325 // XXX careful here - we cannot change the size
329 static int ll_lock_to_stripe_offset(struct inode *inode, struct ldlm_lock *lock)
331 struct ll_inode_info *lli = ll_i2info(inode);
332 struct lov_stripe_md *lsm = lli->lli_smd;
333 struct obd_export *exp = ll_i2dtexp(inode);
336 struct ldlm_lock *lock;
337 struct lov_stripe_md *lsm;
338 } key = { .name = "lock_to_stripe", .lock = lock, .lsm = lsm };
339 __u32 stripe, vallen = sizeof(stripe);
343 if (lsm->lsm_stripe_count == 1)
344 GOTO(check, stripe = 0);
346 /* get our offset in the lov */
347 rc = obd_get_info(exp, sizeof(key), &key, &vallen, &stripe);
349 CERROR("obd_get_info: rc = %d\n", rc);
352 LASSERT(stripe < lsm->lsm_stripe_count);
355 if (lsm->lsm_oinfo[stripe].loi_id != lock->l_resource->lr_name.name[0]||
356 lsm->lsm_oinfo[stripe].loi_gr != lock->l_resource->lr_name.name[2]){
357 LDLM_ERROR(lock, "resource doesn't match object "LPU64"/"LPU64
358 " inode=%lu/%u (%p)\n",
359 lsm->lsm_oinfo[stripe].loi_id,
360 lsm->lsm_oinfo[stripe].loi_gr,
361 inode->i_ino, inode->i_generation, inode);
362 return -ELDLM_NO_LOCK_DATA;
368 /* Flush the page cache for an extent as its canceled. When we're on an LOV,
369 * we get a lock cancellation for each stripe, so we have to map the obd's
370 * region back onto the stripes in the file that it held.
372 * No one can dirty the extent until we've finished our work and they can
373 * enqueue another lock. The DLM protects us from ll_file_read/write here,
374 * but other kernel actors could have pages locked.
376 * Called with the DLM lock held. */
377 void ll_pgcache_remove_extent(struct inode *inode, struct lov_stripe_md *lsm,
378 struct ldlm_lock *lock, __u32 stripe)
380 ldlm_policy_data_t tmpex;
381 unsigned long start, end, count, skip, i, j;
383 int rc, rc2, discard = lock->l_flags & LDLM_FL_DISCARD_DATA;
384 struct lustre_handle lockh;
387 memcpy(&tmpex, &lock->l_policy_data, sizeof(tmpex));
388 CDEBUG(D_INODE|D_PAGE, "inode %lu(%p) ["LPU64"->"LPU64"] size: %llu\n",
389 inode->i_ino, inode, tmpex.l_extent.start, tmpex.l_extent.end,
392 /* our locks are page granular thanks to osc_enqueue, we invalidate the
394 LASSERT((tmpex.l_extent.start & ~PAGE_CACHE_MASK) == 0);
395 LASSERT(((tmpex.l_extent.end + 1) & ~PAGE_CACHE_MASK) == 0);
399 start = tmpex.l_extent.start >> PAGE_CACHE_SHIFT;
400 end = tmpex.l_extent.end >> PAGE_CACHE_SHIFT;
401 if (lsm->lsm_stripe_count > 1) {
402 count = lsm->lsm_stripe_size >> PAGE_CACHE_SHIFT;
403 skip = (lsm->lsm_stripe_count - 1) * count;
404 start += start/count * skip + stripe * count;
406 end += end/count * skip + stripe * count;
408 if (end < tmpex.l_extent.end >> PAGE_CACHE_SHIFT)
411 i = inode->i_size ? (inode->i_size - 1) >> PAGE_CACHE_SHIFT : 0;
415 CDEBUG(D_INODE|D_PAGE, "walking page indices start: %lu j: %lu "
416 "count: %lu skip: %lu end: %lu%s\n", start, start % count,
417 count, skip, end, discard ? " (DISCARDING)" : "");
419 /* walk through the vmas on the inode and tear down mmaped pages that
420 * intersect with the lock. this stops immediately if there are no
421 * mmap()ed regions of the file. This is not efficient at all and
422 * should be short lived. We'll associate mmap()ed pages with the lock
423 * and will be able to find them directly */
425 for (i = start; i <= end; i += (j + skip)) {
426 j = min(count - (i % count), end - i + 1);
427 LASSERT(inode->i_mapping);
428 if (ll_teardown_mmaps(inode->i_mapping, i << PAGE_CACHE_SHIFT,
429 ((i+j) << PAGE_CACHE_SHIFT) - 1) )
433 /* this is the simplistic implementation of page eviction at
434 * cancelation. It is careful to get races with other page
435 * lockers handled correctly. fixes from bug 20 will make it
436 * more efficient by associating locks with pages and with
437 * batching writeback under the lock explicitly. */
438 for (i = start, j = start % count; i <= end;
439 j++, i++, tmpex.l_extent.start += PAGE_CACHE_SIZE) {
441 CDEBUG(D_PAGE, "skip index %lu to %lu\n", i, i + skip);
447 LASSERTF(tmpex.l_extent.start< lock->l_policy_data.l_extent.end,
448 LPU64" >= "LPU64" start %lu i %lu end %lu\n",
449 tmpex.l_extent.start, lock->l_policy_data.l_extent.end,
452 if (!mapping_has_pages(inode->i_mapping)) {
453 CDEBUG(D_INODE|D_PAGE, "nothing left\n");
459 page = find_get_page(inode->i_mapping, i);
462 LL_CDEBUG_PAGE(D_PAGE, page, "lock page idx %lu ext "LPU64"\n",
463 i, tmpex.l_extent.start);
466 /* page->mapping to check with racing against teardown */
467 if (!discard && clear_page_dirty_for_io(page)) {
468 rc = ll_call_writepage(inode, page);
470 CERROR("writepage of page %p failed: %d\n",
472 /* either waiting for io to complete or reacquiring
473 * the lock that the failed writepage released */
477 tmpex.l_extent.end = tmpex.l_extent.start + PAGE_CACHE_SIZE - 1;
478 /* check to see if another DLM lock covers this page */
479 rc2 = ldlm_lock_match(lock->l_resource->lr_namespace,
480 LDLM_FL_BLOCK_GRANTED|LDLM_FL_CBPENDING |
482 &lock->l_resource->lr_name, LDLM_EXTENT,
483 &tmpex, LCK_PR | LCK_PW, &lockh);
484 if (rc2 == 0 && page->mapping != NULL) {
485 // checking again to account for writeback's lock_page()
486 LL_CDEBUG_PAGE(D_PAGE, page, "truncating\n");
487 ll_ra_accounting(page, inode->i_mapping);
488 ll_truncate_complete_page(page);
491 page_cache_release(page);
493 LASSERTF(tmpex.l_extent.start <=
494 (lock->l_policy_data.l_extent.end == ~0ULL ? ~0ULL :
495 lock->l_policy_data.l_extent.end + 1),
496 "loop too long "LPU64" > "LPU64" start %lu i %lu end %lu\n",
497 tmpex.l_extent.start, lock->l_policy_data.l_extent.end,
502 static int ll_extent_lock_callback(struct ldlm_lock *lock,
503 struct ldlm_lock_desc *new, void *data,
506 struct lustre_handle lockh = { 0 };
510 if ((unsigned long)data > 0 && (unsigned long)data < 0x1000) {
511 LDLM_ERROR(lock, "cancelling lock with bad data %p", data);
516 case LDLM_CB_BLOCKING:
517 ldlm_lock2handle(lock, &lockh);
518 rc = ldlm_cli_cancel(&lockh);
520 CERROR("ldlm_cli_cancel failed: %d\n", rc);
522 case LDLM_CB_CANCELING: {
524 struct ll_inode_info *lli;
525 struct lov_stripe_md *lsm;
529 /* This lock wasn't granted, don't try to evict pages */
530 if (lock->l_req_mode != lock->l_granted_mode)
533 inode = ll_inode_from_lock(lock);
536 lli = ll_i2info(inode);
539 if (lli->lli_smd == NULL)
543 stripe = ll_lock_to_stripe_offset(inode, lock);
546 ll_pgcache_remove_extent(inode, lsm, lock, stripe);
548 /* grabbing the i_sem will wait for write() to complete. ns
549 * lock hold times should be very short as ast processing
550 * requires them and has a short timeout. so, i_sem before ns
554 l_lock(&lock->l_resource->lr_namespace->ns_lock);
555 kms = ldlm_extent_shift_kms(lock,
556 lsm->lsm_oinfo[stripe].loi_kms);
558 if (lsm->lsm_oinfo[stripe].loi_kms != kms)
559 LDLM_DEBUG(lock, "updating kms from "LPU64" to "LPU64,
560 lsm->lsm_oinfo[stripe].loi_kms, kms);
561 lsm->lsm_oinfo[stripe].loi_kms = kms;
562 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
564 //ll_try_done_writing(inode);
577 int ll_async_completion_ast(struct ldlm_lock *lock, int flags, void *data)
579 /* XXX ALLOCATE - 160 bytes */
580 struct inode *inode = ll_inode_from_lock(lock);
581 struct ll_inode_info *lli = ll_i2info(inode);
582 struct lustre_handle lockh = { 0 };
587 if (flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
588 LDLM_FL_BLOCK_CONV)) {
589 LBUG(); /* not expecting any blocked async locks yet */
590 LDLM_DEBUG(lock, "client-side async enqueue returned a blocked "
592 ldlm_lock_dump(D_OTHER, lock, 0);
593 ldlm_reprocess_all(lock->l_resource);
597 LDLM_DEBUG(lock, "client-side async enqueue: granted/glimpsed");
599 stripe = ll_lock_to_stripe_offset(inode, lock);
603 if (lock->l_lvb_len) {
604 struct lov_stripe_md *lsm = lli->lli_smd;
606 lvb = lock->l_lvb_data;
607 lsm->lsm_oinfo[stripe].loi_rss = lvb->lvb_size;
609 l_lock(&lock->l_resource->lr_namespace->ns_lock);
611 kms = MAX(lsm->lsm_oinfo[stripe].loi_kms, lvb->lvb_size);
612 kms = ldlm_extent_shift_kms(NULL, kms);
613 if (lsm->lsm_oinfo[stripe].loi_kms != kms)
614 LDLM_DEBUG(lock, "updating kms from "LPU64" to "LPU64,
615 lsm->lsm_oinfo[stripe].loi_kms, kms);
616 lsm->lsm_oinfo[stripe].loi_kms = kms;
618 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
623 wake_up(&lock->l_waitq);
625 ldlm_lock2handle(lock, &lockh);
626 ldlm_lock_decref(&lockh, LCK_PR);
631 static int ll_glimpse_callback(struct ldlm_lock *lock, void *reqp)
633 struct ptlrpc_request *req = reqp;
634 struct inode *inode = ll_inode_from_lock(lock);
635 struct ll_inode_info *lli;
637 struct lov_stripe_md *lsm;
638 int rc, size = sizeof(*lvb), stripe;
642 GOTO(out, rc = -ELDLM_NO_LOCK_DATA);
643 lli = ll_i2info(inode);
645 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
649 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
651 /* First, find out which stripe index this lock corresponds to. */
652 stripe = ll_lock_to_stripe_offset(inode, lock);
654 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
656 rc = lustre_pack_reply(req, 1, &size, NULL);
658 CERROR("lustre_pack_reply: %d\n", rc);
662 lvb = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*lvb));
663 lvb->lvb_size = lli->lli_smd->lsm_oinfo[stripe].loi_kms;
665 LDLM_DEBUG(lock, "i_size: %llu -> stripe number %u -> kms "LPU64,
666 inode->i_size, stripe, lvb->lvb_size);
672 /* These errors are normal races, so we don't want to fill the console
673 * with messages by calling ptlrpc_error() */
674 if (rc == -ELDLM_NO_LOCK_DATA)
675 lustre_pack_reply(req, 0, NULL, NULL);
681 __u64 lov_merge_size(struct lov_stripe_md *lsm, int kms);
682 __u64 lov_merge_blocks(struct lov_stripe_md *lsm);
683 __u64 lov_merge_mtime(struct lov_stripe_md *lsm, __u64 current_time);
685 /* NB: lov_merge_size will prefer locally cached writes if they extend the
686 * file (because it prefers KMS over RSS when larger) */
687 int ll_glimpse_size(struct inode *inode)
689 struct ll_inode_info *lli = ll_i2info(inode);
690 struct ll_sb_info *sbi = ll_i2sbi(inode);
691 ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
692 struct lustre_handle lockh = { 0 };
693 int rc, flags = LDLM_FL_HAS_INTENT;
696 CDEBUG(D_DLMTRACE, "Glimpsing inode %lu\n", inode->i_ino);
698 rc = obd_enqueue(sbi->ll_dt_exp, lli->lli_smd, LDLM_EXTENT, &policy,
699 LCK_PR, &flags, ll_extent_lock_callback,
700 ldlm_completion_ast, ll_glimpse_callback, inode,
701 sizeof(struct ost_lvb), lustre_swab_ost_lvb, &lockh);
706 CERROR("obd_enqueue returned rc %d, returning -EIO\n", rc);
707 RETURN(rc > 0 ? -EIO : rc);
710 inode->i_size = lov_merge_size(lli->lli_smd, 0);
711 inode->i_blocks = lov_merge_blocks(lli->lli_smd);
712 //inode->i_mtime = lov_merge_mtime(lli->lli_smd, inode->i_mtime);
714 CDEBUG(D_DLMTRACE, "glimpse: size: "LPU64", blocks: "LPU64"\n",
715 (__u64)inode->i_size, (__u64)inode->i_blocks);
716 obd_cancel(sbi->ll_dt_exp, lli->lli_smd, LCK_PR, &lockh);
720 void ll_stime_record(struct ll_sb_info *sbi, struct timeval *start,
721 struct obd_service_time *stime)
724 do_gettimeofday(&stop);
726 spin_lock(&sbi->ll_lock);
727 lprocfs_stime_record(stime, &stop, start);
728 spin_unlock(&sbi->ll_lock);
731 int ll_extent_lock(struct ll_file_data *fd, struct inode *inode,
732 struct lov_stripe_md *lsm, int mode,
733 ldlm_policy_data_t *policy, struct lustre_handle *lockh,
734 int ast_flags, struct obd_service_time *stime)
736 struct ll_sb_info *sbi = ll_i2sbi(inode);
737 struct timeval start;
741 LASSERT(lockh->cookie == 0);
743 /* XXX phil: can we do this? won't it screw the file size up? */
744 if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
745 (sbi->ll_flags & LL_SBI_NOLCK))
748 CDEBUG(D_DLMTRACE, "Locking inode %lu, start "LPU64" end "LPU64"\n",
749 inode->i_ino, policy->l_extent.start, policy->l_extent.end);
751 do_gettimeofday(&start);
752 rc = obd_enqueue(sbi->ll_dt_exp, lsm, LDLM_EXTENT, policy, mode,
753 &ast_flags, ll_extent_lock_callback,
754 ldlm_completion_ast, ll_glimpse_callback, inode,
755 sizeof(struct ost_lvb), lustre_swab_ost_lvb, lockh);
759 ll_stime_record(sbi, &start, stime);
761 if (policy->l_extent.start == 0 &&
762 policy->l_extent.end == OBD_OBJECT_EOF) {
763 /* vmtruncate()->ll_truncate() first sets the i_size and then
764 * the kms under both a DLM lock and the i_sem. If we don't
765 * get the i_sem here we can match the DLM lock and reset
766 * i_size from the kms before the truncating path has updated
767 * the kms. generic_file_write can then trust the stale i_size
768 * when doing appending writes and effectively cancel the
769 * result of the truncate. Getting the i_sem after the enqueue
770 * maintains the DLM -> i_sem acquiry order. */
772 inode->i_size = lov_merge_size(lsm, 1);
775 //inode->i_mtime = lov_merge_mtime(lsm, inode->i_mtime);
780 int ll_extent_unlock(struct ll_file_data *fd, struct inode *inode,
781 struct lov_stripe_md *lsm, int mode,
782 struct lustre_handle *lockh)
784 struct ll_sb_info *sbi = ll_i2sbi(inode);
788 /* XXX phil: can we do this? won't it screw the file size up? */
789 if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
790 (sbi->ll_flags & LL_SBI_NOLCK))
793 rc = obd_cancel(sbi->ll_dt_exp, lsm, mode, lockh);
798 static ssize_t ll_file_read(struct file *file, char *buf, size_t count,
801 struct inode *inode = file->f_dentry->d_inode;
802 struct ll_inode_info *lli = ll_i2info(inode);
803 struct lov_stripe_md *lsm = lli->lli_smd;
804 struct ll_lock_tree tree;
805 struct ll_lock_tree_node *node;
810 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
811 inode->i_ino, inode->i_generation, inode, count, *ppos);
813 /* "If nbyte is 0, read() will return 0 and have no other results."
814 * -- Single Unix Spec */
818 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_READ_BYTES,
824 node = ll_node_from_inode(inode, *ppos, *ppos + count - 1,
827 tree.lt_fd = file->private_data;
829 rc = ll_tree_lock(&tree, node, inode, buf, count,
830 file->f_flags & O_NONBLOCK ? LDLM_FL_BLOCK_NOWAIT :0);
834 kms = lov_merge_size(lsm, 1);
835 if (*ppos + count - 1 > kms) {
836 /* A glimpse is necessary to determine whether we return a short
837 * read or some zeroes at the end of the buffer */
838 retval = ll_glimpse_size(inode);
845 CDEBUG(D_INFO, "Read ino %lu, "LPSZ" bytes, offset %lld, i_size %llu\n",
846 inode->i_ino, count, *ppos, inode->i_size);
848 /* turn off the kernel's read-ahead */
849 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
852 file->f_ra.ra_pages = 0;
854 retval = generic_file_read(file, buf, count, ppos);
857 ll_tree_unlock(&tree, inode);
862 * Write to a file (through the page cache).
864 static ssize_t ll_file_write(struct file *file, const char *buf,
865 size_t count, loff_t *ppos)
867 struct inode *inode = file->f_dentry->d_inode;
868 loff_t maxbytes = ll_file_maxbytes(inode);
869 struct ll_lock_tree tree;
870 struct ll_lock_tree_node *node;
875 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
876 inode->i_ino, inode->i_generation, inode, count, *ppos);
878 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
880 /* POSIX, but surprised the VFS doesn't check this already */
884 /* If file was opened for LL_IOC_LOV_SETSTRIPE but the ioctl wasn't
885 * called on the file, don't fail the below assertion (bug 2388). */
886 if (file->f_flags & O_LOV_DELAY_CREATE &&
887 ll_i2info(inode)->lli_smd == NULL)
890 LASSERT(ll_i2info(inode)->lli_smd != NULL);
892 if (file->f_flags & O_APPEND)
893 node = ll_node_from_inode(inode, 0, OBD_OBJECT_EOF, LCK_PW);
895 node = ll_node_from_inode(inode, *ppos, *ppos + count - 1,
899 RETURN(PTR_ERR(node));
901 tree.lt_fd = file->private_data;
903 rc = ll_tree_lock(&tree, node, inode, buf, count,
904 file->f_flags & O_NONBLOCK ? LDLM_FL_BLOCK_NOWAIT :0);
908 /* this is ok, g_f_w will overwrite this under i_sem if it races
909 * with a local truncate, it just makes our maxbyte checking easier */
910 if (file->f_flags & O_APPEND)
911 *ppos = inode->i_size;
913 if (*ppos >= maxbytes) {
914 if (count || *ppos > maxbytes) {
915 send_sig(SIGXFSZ, current, 0);
916 GOTO(out, retval = -EFBIG);
919 if (*ppos + count > maxbytes)
920 count = maxbytes - *ppos;
922 CDEBUG(D_INFO, "Writing inode %lu, "LPSZ" bytes, offset %Lu\n",
923 inode->i_ino, count, *ppos);
925 /* generic_file_write handles O_APPEND after getting i_sem */
926 retval = generic_file_write(file, buf, count, ppos);
929 ll_tree_unlock(&tree, inode);
930 /* serialize with mmap/munmap/mremap */
931 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_WRITE_BYTES,
932 retval > 0 ? retval : 0);
936 static int ll_lov_recreate_obj(struct inode *inode, struct file *file,
939 struct ll_inode_info *lli = ll_i2info(inode);
940 struct obd_export *exp = ll_i2dtexp(inode);
941 struct ll_recreate_obj ucreatp;
942 struct obd_trans_info oti = { 0 };
943 struct obdo *oa = NULL;
946 struct lov_stripe_md *lsm, *lsm2;
949 if (!capable (CAP_SYS_ADMIN))
952 rc = copy_from_user(&ucreatp, (struct ll_recreate_obj *)arg,
953 sizeof(struct ll_recreate_obj));
961 down(&lli->lli_open_sem);
964 GOTO(out, rc = -ENOENT);
965 lsm_size = sizeof(*lsm) + (sizeof(struct lov_oinfo) *
966 (lsm->lsm_stripe_count));
968 OBD_ALLOC(lsm2, lsm_size);
970 GOTO(out, rc = -ENOMEM);
972 oa->o_id = ucreatp.lrc_id;
973 oa->o_nlink = ucreatp.lrc_ost_idx;
974 oa->o_gr = ucreatp.lrc_group;
975 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP | OBD_MD_FLFLAGS;
976 oa->o_flags |= OBD_FL_RECREATE_OBJS;
977 obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
978 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
980 oti.oti_objid = NULL;
981 memcpy(lsm2, lsm, lsm_size);
982 rc = obd_create(exp, oa, &lsm2, &oti);
984 OBD_FREE(lsm2, lsm_size);
987 up(&lli->lli_open_sem);
992 static int ll_lov_setstripe_ea_info(struct inode *inode, struct file *file,
993 int flags, struct lov_user_md *lum,
996 struct ll_inode_info *lli = ll_i2info(inode);
998 struct obd_export *exp = ll_i2dtexp(inode);
999 struct lov_stripe_md *lsm;
1000 struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1001 struct ptlrpc_request *req = NULL;
1003 struct lustre_md md;
1006 down(&lli->lli_open_sem);
1009 up(&lli->lli_open_sem);
1010 CDEBUG(D_IOCTL, "stripe already exists for ino %lu\n",
1015 f = get_empty_filp();
1019 f->f_dentry = file->f_dentry;
1020 f->f_vfsmnt = file->f_vfsmnt;
1022 rc = ll_intent_alloc(&oit);
1026 rc = ll_intent_file_open(f, lum, lum_size, &oit);
1029 if (it_disposition(&oit, DISP_LOOKUP_NEG))
1032 req = LUSTRE_IT(&oit)->it_data;
1033 rc = LUSTRE_IT(&oit)->it_status;
1038 rc = mdc_req2lustre_md(ll_i2mdexp(inode), req, 1, exp, &md);
1041 ll_update_inode(f->f_dentry->d_inode, &md);
1043 rc = ll_local_open(f, &oit);
1046 ll_intent_release(&oit);
1048 rc = ll_file_release(f->f_dentry->d_inode, f);
1051 ll_intent_release(&oit);
1054 up(&lli->lli_open_sem);
1056 ptlrpc_req_finished(req);
1060 static int ll_lov_setea(struct inode *inode, struct file *file,
1063 int flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
1064 struct lov_user_md *lump;
1065 int lum_size = sizeof(struct lov_user_md) +
1066 sizeof(struct lov_user_ost_data);
1070 if (!capable (CAP_SYS_ADMIN))
1073 OBD_ALLOC(lump, lum_size);
1077 rc = copy_from_user(lump, (struct lov_user_md *)arg, lum_size);
1079 OBD_FREE(lump, lum_size);
1083 rc = ll_lov_setstripe_ea_info(inode, file, flags, lump, lum_size);
1085 OBD_FREE(lump, lum_size);
1089 static int ll_lov_setstripe(struct inode *inode, struct file *file,
1092 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1094 int flags = FMODE_WRITE;
1097 /* Bug 1152: copy properly when this is no longer true */
1098 LASSERT(sizeof(lum) == sizeof(*lump));
1099 LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
1100 rc = copy_from_user(&lum, lump, sizeof(lum));
1104 rc = ll_lov_setstripe_ea_info(inode, file, flags, &lum, sizeof(lum));
1106 put_user(0, &lump->lmm_stripe_count);
1107 rc = obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode),
1108 0, ll_i2info(inode)->lli_smd, lump);
1113 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
1115 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1120 return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode), 0, lsm,
1124 static int ll_get_grouplock(struct inode *inode, struct file *file,
1127 struct ll_file_data *fd = file->private_data;
1128 ldlm_policy_data_t policy = { .l_extent = { .start = 0,
1129 .end = OBD_OBJECT_EOF}};
1130 struct lustre_handle lockh = { 0 };
1131 struct ll_inode_info *lli = ll_i2info(inode);
1132 struct lov_stripe_md *lsm = lli->lli_smd;
1136 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1140 policy.l_extent.gid = arg;
1141 if (file->f_flags & O_NONBLOCK)
1142 flags = LDLM_FL_BLOCK_NOWAIT;
1144 rc = ll_extent_lock(fd, inode, lsm, LCK_GROUP, &policy, &lockh, flags,
1145 &ll_i2sbi(inode)->ll_grouplock_stime);
1149 fd->fd_flags |= LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK;
1151 memcpy(&fd->fd_cwlockh, &lockh, sizeof(lockh));
1156 static int ll_put_grouplock(struct inode *inode, struct file *file,
1159 struct ll_file_data *fd = file->private_data;
1160 struct ll_inode_info *lli = ll_i2info(inode);
1161 struct lov_stripe_md *lsm = lli->lli_smd;
1165 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1166 /* Ugh, it's already unlocked. */
1170 if (fd->fd_gid != arg) /* Ugh? Unlocking with different gid? */
1173 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
1175 rc = ll_extent_unlock(fd, inode, lsm, LCK_GROUP, &fd->fd_cwlockh);
1180 memset(&fd->fd_cwlockh, 0, sizeof(fd->fd_cwlockh));
1185 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
1188 struct ll_file_data *fd = file->private_data;
1189 struct ll_sb_info *sbi = ll_i2sbi(inode);
1193 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),cmd=%x\n", inode->i_ino,
1194 inode->i_generation, inode, cmd);
1196 if (_IOC_TYPE(cmd) == 'T') /* tty ioctls */
1199 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
1201 case LL_IOC_GETFLAGS:
1202 /* Get the current value of the file flags */
1203 return put_user(fd->fd_flags, (int *)arg);
1204 case LL_IOC_SETFLAGS:
1205 case LL_IOC_CLRFLAGS:
1206 /* Set or clear specific file flags */
1207 /* XXX This probably needs checks to ensure the flags are
1208 * not abused, and to handle any flag side effects.
1210 if (get_user(flags, (int *) arg))
1213 if (cmd == LL_IOC_SETFLAGS)
1214 fd->fd_flags |= flags;
1216 fd->fd_flags &= ~flags;
1218 case LL_IOC_LOV_SETSTRIPE:
1219 RETURN(ll_lov_setstripe(inode, file, arg));
1220 case LL_IOC_LOV_SETEA:
1221 RETURN(ll_lov_setea(inode, file, arg));
1222 case IOC_MDC_SHOWFID: {
1223 struct lustre_id *idp = (struct lustre_id *)arg;
1224 struct lustre_id id;
1228 filename = getname((const char *)arg);
1229 if (IS_ERR(filename))
1230 RETURN(PTR_ERR(filename));
1232 ll_inode2id(&id, inode);
1234 rc = ll_get_fid(sbi->ll_md_exp, &id, filename, &id);
1236 GOTO(out_filename, rc);
1238 rc = copy_to_user(idp, &id, sizeof(*idp));
1240 GOTO(out_filename, rc = -EFAULT);
1247 case LL_IOC_LOV_GETSTRIPE:
1248 RETURN(ll_lov_getstripe(inode, arg));
1249 case LL_IOC_RECREATE_OBJ:
1250 RETURN(ll_lov_recreate_obj(inode, file, arg));
1251 case EXT3_IOC_GETFLAGS:
1252 case EXT3_IOC_SETFLAGS:
1253 RETURN( ll_iocontrol(inode, file, cmd, arg) );
1254 case LL_IOC_GROUP_LOCK:
1255 RETURN(ll_get_grouplock(inode, file, arg));
1256 case LL_IOC_GROUP_UNLOCK:
1257 RETURN(ll_put_grouplock(inode, file, arg));
1258 case EXT3_IOC_GETVERSION_OLD:
1259 case EXT3_IOC_GETVERSION:
1260 return put_user(inode->i_generation, (int *) arg);
1261 /* We need to special case any other ioctls we want to handle,
1262 * to send them to the MDS/OST as appropriate and to properly
1263 * network encode the arg field.
1264 case EXT2_IOC_GETVERSION_OLD:
1265 case EXT2_IOC_GETVERSION_NEW:
1266 case EXT2_IOC_SETVERSION_OLD:
1267 case EXT2_IOC_SETVERSION_NEW:
1268 case EXT3_IOC_SETVERSION_OLD:
1269 case EXT3_IOC_SETVERSION:
1272 RETURN( obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
1277 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
1279 struct inode *inode = file->f_dentry->d_inode;
1280 struct ll_file_data *fd = file->private_data;
1281 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1282 struct lustre_handle lockh = {0};
1285 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),to=%llu\n", inode->i_ino,
1286 inode->i_generation, inode,
1287 offset + ((origin==2) ? inode->i_size : file->f_pos));
1289 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_LLSEEK);
1290 if (origin == 2) { /* SEEK_END */
1291 ldlm_policy_data_t policy = { .l_extent = {0, OBD_OBJECT_EOF }};
1292 int nonblock = 0, rc;
1294 if (file->f_flags & O_NONBLOCK)
1295 nonblock = LDLM_FL_BLOCK_NOWAIT;
1297 rc = ll_extent_lock(fd, inode, lsm, LCK_PR, &policy, &lockh,
1298 nonblock, &ll_i2sbi(inode)->ll_seek_stime);
1302 offset += inode->i_size;
1303 } else if (origin == 1) { /* SEEK_CUR */
1304 offset += file->f_pos;
1308 if (offset >= 0 && offset <= ll_file_maxbytes(inode)) {
1309 if (offset != file->f_pos) {
1310 file->f_pos = offset;
1311 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1313 file->f_version = ++event;
1320 ll_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
1324 int ll_fsync(struct file *file, struct dentry *dentry, int data)
1326 struct inode *inode = dentry->d_inode;
1327 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1328 struct lustre_id id;
1329 struct ptlrpc_request *req;
1332 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1333 inode->i_generation, inode);
1335 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_FSYNC);
1337 /* fsync's caller has already called _fdata{sync,write}, we want
1338 * that IO to finish before calling the osc and mdc sync methods */
1339 rc = filemap_fdatawait(inode->i_mapping);
1341 ll_inode2id(&id, inode);
1342 err = md_sync(ll_i2sbi(inode)->ll_md_exp, &id, &req);
1346 ptlrpc_req_finished(req);
1349 struct obdo *oa = obdo_alloc();
1352 RETURN(rc ? rc : -ENOMEM);
1354 oa->o_id = lsm->lsm_object_id;
1355 oa->o_gr = lsm->lsm_object_gr;
1356 oa->o_valid = OBD_MD_FLID;
1357 obdo_from_inode(oa, inode, (OBD_MD_FLTYPE | OBD_MD_FLATIME |
1358 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1361 err = obd_sync(ll_i2sbi(inode)->ll_dt_exp, oa, lsm,
1371 int ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
1373 struct inode *inode = file->f_dentry->d_inode;
1374 struct ll_inode_info *li = ll_i2info(inode);
1375 struct ll_sb_info *sbi = ll_i2sbi(inode);
1376 struct obd_device *obddev;
1377 struct ldlm_res_id res_id =
1378 { .name = {id_fid(&li->lli_id), id_group(&li->lli_id), LDLM_FLOCK} };
1379 struct lustre_handle lockh = {0};
1380 ldlm_policy_data_t flock;
1381 ldlm_mode_t mode = 0;
1386 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu file_lock=%p\n",
1387 inode->i_ino, file_lock);
1389 flock.l_flock.pid = file_lock->fl_pid;
1390 flock.l_flock.start = file_lock->fl_start;
1391 flock.l_flock.end = file_lock->fl_end;
1393 switch (file_lock->fl_type) {
1398 /* An unlock request may or may not have any relation to
1399 * existing locks so we may not be able to pass a lock handle
1400 * via a normal ldlm_lock_cancel() request. The request may even
1401 * unlock a byte range in the middle of an existing lock. In
1402 * order to process an unlock request we need all of the same
1403 * information that is given with a normal read or write record
1404 * lock request. To avoid creating another ldlm unlock (cancel)
1405 * message we'll treat a LCK_NL flock request as an unlock. */
1412 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1427 flags = LDLM_FL_BLOCK_NOWAIT;
1433 flags = LDLM_FL_TEST_LOCK;
1434 /* Save the old mode so that if the mode in the lock changes we
1435 * can decrement the appropriate reader or writer refcount. */
1436 file_lock->fl_type = mode;
1439 CERROR("unknown fcntl lock command: %d\n", cmd);
1443 CDEBUG(D_DLMTRACE, "inode=%lu, pid="LPU64", flags=%#x, mode=%u, "
1444 "start="LPU64", end="LPU64"\n", inode->i_ino, flock.l_flock.pid,
1445 flags, mode, flock.l_flock.start, flock.l_flock.end);
1447 obddev = md_get_real_obd(sbi->ll_md_exp, &li->lli_id);
1448 rc = ldlm_cli_enqueue(obddev->obd_self_export, NULL,
1449 obddev->obd_namespace,
1450 res_id, LDLM_FLOCK, &flock, mode, &flags,
1451 NULL, ldlm_flock_completion_ast, NULL, file_lock,
1452 NULL, 0, NULL, &lockh);
1456 int ll_inode_revalidate_it(struct dentry *dentry)
1458 struct lookup_intent oit = { .it_op = IT_GETATTR };
1459 struct inode *inode = dentry->d_inode;
1460 struct ptlrpc_request *req = NULL;
1461 struct ll_inode_info *lli;
1462 struct lov_stripe_md *lsm;
1463 struct ll_sb_info *sbi;
1464 struct lustre_id id;
1469 CERROR("REPORT THIS LINE TO PETER\n");
1473 sbi = ll_i2sbi(inode);
1475 ll_inode2id(&id, inode);
1476 lli = ll_i2info(inode);
1477 LASSERT(id_fid(&id) != 0);
1479 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), name=%s(%p)\n",
1480 inode->i_ino, inode->i_generation, inode, dentry->d_name.name,
1483 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
1484 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_REVALIDATE);
1487 rc = ll_intent_alloc(&oit);
1491 rc = md_intent_lock(sbi->ll_md_exp, &id, NULL, 0, NULL, 0, &id,
1492 &oit, 0, &req, ll_mdc_blocking_ast);
1496 rc = revalidate_it_finish(req, 1, &oit, dentry);
1501 ll_lookup_finish_locks(&oit, dentry);
1504 if (lsm == NULL) /* object not yet allocated, don't validate size */
1508 * ll_glimpse_size() will prefer locally cached writes if they extend
1511 rc = ll_glimpse_size(inode);
1514 ll_intent_release(&oit);
1516 ptlrpc_req_finished(req);
1520 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1521 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
1524 struct inode *inode = de->d_inode;
1526 res = ll_inode_revalidate_it(de);
1527 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_GETATTR);
1532 stat->ino = inode->i_ino;
1533 stat->mode = inode->i_mode;
1534 stat->nlink = inode->i_nlink;
1535 stat->uid = inode->i_uid;
1536 stat->gid = inode->i_gid;
1537 stat->atime = inode->i_atime;
1538 stat->mtime = inode->i_mtime;
1539 stat->ctime = inode->i_ctime;
1540 stat->size = inode->i_size;
1541 stat->blksize = inode->i_blksize;
1542 stat->blocks = inode->i_blocks;
1543 stat->rdev = kdev_t_to_nr(inode->i_rdev);
1544 stat->dev = id_group(&ll_i2info(inode)->lli_id);
1550 int ll_setxattr_internal(struct inode *inode, const char *name,
1551 const void *value, size_t size, int flags,
1554 struct ll_sb_info *sbi = ll_i2sbi(inode);
1555 struct ptlrpc_request *request = NULL;
1556 struct mdc_op_data op_data;
1561 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", inode->i_ino);
1562 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_SETXATTR);
1564 memset(&attr, 0x0, sizeof(attr));
1565 attr.ia_valid |= valid;
1566 attr.ia_attr_flags = flags;
1568 ll_prepare_mdc_data(&op_data, inode, NULL, NULL, 0, 0);
1570 rc = md_setattr(sbi->ll_md_exp, &op_data, &attr,
1571 (void*) name, strnlen(name, XATTR_NAME_MAX)+1,
1572 (void*) value, size, &request);
1574 CERROR("md_setattr fails: rc = %d\n", rc);
1579 ptlrpc_req_finished(request);
1583 int ll_setxattr(struct dentry *dentry, const char *name, const void *value,
1584 size_t size, int flags)
1587 struct posix_acl *acl;
1588 struct ll_inode_info *lli;
1591 rc = ll_setxattr_internal(dentry->d_inode, name, value, size,
1594 /* update inode's acl info */
1595 if (rc == 0 && strcmp(name, XATTR_NAME_ACL_ACCESS) == 0) {
1597 acl = posix_acl_from_xattr(value, size);
1599 CERROR("convert from xattr to acl error: %ld",
1603 error = posix_acl_valid(acl);
1605 CERROR("acl valid error: %d", error);
1606 posix_acl_release(acl);
1614 lli = ll_i2info(dentry->d_inode);
1615 spin_lock(&lli->lli_lock);
1616 if (lli->lli_acl_access != NULL)
1617 posix_acl_release(lli->lli_acl_access);
1618 lli->lli_acl_access = acl;
1619 spin_unlock(&lli->lli_lock);
1626 int ll_removexattr(struct dentry *dentry, const char *name)
1628 return ll_setxattr_internal(dentry->d_inode, name, NULL, 0, 0,
1633 int ll_getxattr_internal(struct inode *inode, const char *name, int namelen,
1634 void *value, size_t size, __u64 valid)
1636 struct ptlrpc_request *request = NULL;
1637 struct ll_sb_info *sbi = ll_i2sbi(inode);
1638 struct lustre_id id;
1639 struct mds_body *body;
1644 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_GETXATTR);
1646 ll_inode2id(&id, inode);
1647 rc = md_getattr(sbi->ll_md_exp, &id, valid, name, namelen,
1650 if (rc != -ENODATA && rc != -EOPNOTSUPP)
1651 CERROR("md_getattr fails: rc = %d\n", rc);
1655 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
1656 LASSERT(body != NULL);
1657 LASSERT_REPSWABBED(request, 0);
1659 ea_size = body->eadatasize;
1660 LASSERT(ea_size <= request->rq_repmsg->buflens[0]);
1663 GOTO(out, rc = ea_size);
1665 ea_data = lustre_msg_buf(request->rq_repmsg, 1, ea_size);
1666 LASSERT(ea_data != NULL);
1667 LASSERT_REPSWABBED(request, 1);
1670 memcpy(value, ea_data, ea_size);
1673 ptlrpc_req_finished(request);
1677 int ll_getxattr(struct dentry *dentry, const char *name, void *value,
1680 return ll_getxattr_internal(dentry->d_inode, name, strlen(name) + 1,
1681 value, size, OBD_MD_FLEA);
1684 int ll_listxattr(struct dentry *dentry, char *list, size_t size)
1686 return ll_getxattr_internal(dentry->d_inode, NULL, 0, list, size,
1690 int ll_inode_permission(struct inode *inode, int mask, struct nameidata *nd)
1692 struct lookup_intent it = { .it_op = IT_GETATTR };
1693 int mode = inode->i_mode;
1695 struct ll_sb_info *sbi;
1696 struct lustre_id id;
1697 struct ptlrpc_request *req = NULL;
1701 sbi = ll_i2sbi(inode);
1702 ll_inode2id(&id, inode);
1704 /* Nobody gets write access to a read-only fs */
1705 if ((mask & MAY_WRITE) && IS_RDONLY(inode) &&
1706 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
1708 /* Nobody gets write access to an immutable file */
1709 if ((mask & MAY_WRITE) && IS_IMMUTABLE(inode))
1711 if (current->fsuid == inode->i_uid) {
1714 struct ll_inode_info *lli = ll_i2info(inode);
1715 struct posix_acl *acl;
1717 /* The access ACL cannot grant access if the group class
1718 permission bits don't contain all requested permissions. */
1719 if (((mode >> 3) & mask & S_IRWXO) != mask)
1722 if (ll_intent_alloc(&it))
1726 rc = md_intent_lock(sbi->ll_md_exp, &id, NULL, 0, NULL, 0, &id,
1727 &it, 0, &req, ll_mdc_blocking_ast);
1729 ll_intent_free(&it);
1733 rc = revalidate_it_finish(req, 1, &it, &de);
1735 ll_intent_release(&it);
1739 ll_lookup_finish_locks(&it, &de);
1740 ll_intent_free(&it);
1742 spin_lock(&lli->lli_lock);
1743 acl = posix_acl_dup(ll_i2info(inode)->lli_acl_access);
1744 spin_unlock(&lli->lli_lock);
1749 rc = posix_acl_permission(inode, acl, mask);
1750 posix_acl_release(acl);
1752 goto check_capabilities;
1756 if (in_group_p(inode->i_gid))
1759 if ((mode & mask & S_IRWXO) == mask)
1764 /* Allowed to override Discretionary Access Control? */
1765 if (!(mask & MAY_EXEC) ||
1766 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
1767 if (capable(CAP_DAC_OVERRIDE))
1769 /* Read and search granted if capable(CAP_DAC_READ_SEARCH) */
1770 if (capable(CAP_DAC_READ_SEARCH) && ((mask == MAY_READ) ||
1771 (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE))))
1775 ptlrpc_req_finished(req);
1780 struct file_operations ll_file_operations = {
1781 .read = ll_file_read,
1782 .write = ll_file_write,
1783 .ioctl = ll_file_ioctl,
1784 .open = ll_file_open,
1785 .release = ll_file_release,
1786 .mmap = ll_file_mmap,
1787 .llseek = ll_file_seek,
1788 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
1789 .sendfile = generic_file_sendfile,
1792 .lock = ll_file_flock
1795 struct inode_operations ll_file_inode_operations = {
1796 .setattr = ll_setattr,
1797 .truncate = ll_truncate,
1798 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1799 .getattr = ll_getattr,
1801 .revalidate_it = ll_inode_revalidate_it,
1803 .setxattr = ll_setxattr,
1804 .getxattr = ll_getxattr,
1805 .listxattr = ll_listxattr,
1806 .removexattr = ll_removexattr,
1807 .permission = ll_inode_permission,