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 int ll_md_och_close(struct obd_export *md_exp, struct inode *inode,
38 struct obd_client_handle *och)
40 struct ptlrpc_request *req = NULL;
41 struct obdo *obdo = NULL;
42 struct obd_device *obd;
46 obd = class_exp2obd(md_exp);
48 CERROR("Invalid MDC connection handle "LPX64"\n",
49 md_exp->exp_handle.h_cookie);
55 * here we check if this is forced umount. If so this is called on
56 * canceling "open lock" and we do not call md_close() in this case , as
57 * it will not successful, as import is already deactivated.
59 if (obd->obd_no_recov)
62 /* closing opened file */
67 obdo->o_id = inode->i_ino;
68 obdo->o_valid = OBD_MD_FLID;
69 obdo_from_inode(obdo, inode, (OBD_MD_FLTYPE | OBD_MD_FLMODE |
70 OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
71 OBD_MD_FLATIME | OBD_MD_FLMTIME |
73 if (0 /* ll_is_inode_dirty(inode) */) {
74 obdo->o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
75 obdo->o_valid |= OBD_MD_FLFLAGS;
77 obdo->o_fid = id_fid(&ll_i2info(inode)->lli_id);
78 obdo->o_mds = id_group(&ll_i2info(inode)->lli_id);
79 rc = md_close(md_exp, obdo, och, &req);
84 * we are the last writer, so the MDS has instructed us to get
85 * the file size and any write cookies, then close again.
88 //ll_queue_done_writing(inode);
91 CERROR("inode %lu mdc close failed: rc = %d\n",
92 (unsigned long)inode->i_ino, rc);
95 /* objects are destroed on OST only if metadata close was
98 rc = ll_objects_destroy(req, inode, 1);
100 CERROR("inode %lu ll_objects destroy: rc = %d\n",
104 ptlrpc_req_finished(req);
107 mdc_clear_open_replay_data(md_exp, och);
108 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
109 OBD_FREE(och, sizeof *och);
113 int ll_md_real_close(struct obd_export *md_exp,
114 struct inode *inode, int flags)
116 struct ll_inode_info *lli = ll_i2info(inode);
117 struct obd_client_handle **och_p;
118 struct obd_client_handle *och;
123 if (flags & FMODE_WRITE) {
124 och_p = &lli->lli_mds_write_och;
125 och_usecount = &lli->lli_open_fd_write_count;
126 } else if (flags & FMODE_EXEC) {
127 och_p = &lli->lli_mds_exec_och;
128 och_usecount = &lli->lli_open_fd_exec_count;
130 och_p = &lli->lli_mds_read_och;
131 och_usecount = &lli->lli_open_fd_read_count;
134 down(&lli->lli_och_sem);
135 if (*och_usecount) { /* There are still users of this handle, so
137 up(&lli->lli_och_sem);
143 up(&lli->lli_och_sem);
146 * there might be a race and somebody have freed this och
147 * already. Another way to have this twice called is if file closing
148 * will fail due to netwok problems and on umount lock will be canceled
149 * and this will be called from block_ast callack.
151 if (och && och->och_fh.cookie != DEAD_HANDLE_MAGIC)
152 rc = ll_md_och_close(md_exp, inode, och);
157 int ll_md_close(struct obd_export *md_exp, struct inode *inode,
160 struct ll_file_data *fd = file->private_data;
161 struct ll_inode_info *lli = ll_i2info(inode);
165 /* clear group lock, if present */
166 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
167 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
168 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
169 rc = ll_extent_unlock(fd, inode, lsm, LCK_GROUP,
173 /* Let's see if we have good enough OPEN lock on the file and if
174 we can skip talking to MDS */
175 if (file->f_dentry->d_inode) {
177 struct obd_device *obddev;
178 struct lustre_handle lockh;
179 int flags = LDLM_FL_BLOCK_GRANTED;
180 struct ldlm_res_id file_res_id = {.name = {id_fid(&lli->lli_id),
181 id_group(&lli->lli_id)}};
182 ldlm_policy_data_t policy = {.l_inodebits={MDS_INODELOCK_OPEN}};
184 down(&lli->lli_och_sem);
185 if (fd->fd_omode & FMODE_WRITE) {
187 LASSERT(lli->lli_open_fd_write_count);
188 lli->lli_open_fd_write_count--;
189 } else if (fd->fd_omode & FMODE_EXEC) {
191 LASSERT(lli->lli_open_fd_exec_count);
192 lli->lli_open_fd_exec_count--;
195 LASSERT(lli->lli_open_fd_read_count);
196 lli->lli_open_fd_read_count--;
198 up(&lli->lli_och_sem);
200 obddev = md_get_real_obd(md_exp, &lli->lli_id);
201 if (!ldlm_lock_match(obddev->obd_namespace, flags, &file_res_id,
202 LDLM_IBITS, &policy, lockmode, &lockh))
204 rc = ll_md_real_close(md_exp, file->f_dentry->d_inode,
207 ldlm_lock_decref(&lockh, lockmode);
211 file->private_data = NULL;
212 OBD_SLAB_FREE(fd, ll_file_data_slab, sizeof(*fd));
216 /* While this returns an error code, fput() the caller does not, so we need
217 * to make every effort to clean up all of our state here. Also, applications
218 * rarely check close errors and even if an error is returned they will not
219 * re-try the close call.
221 int ll_file_release(struct inode *inode, struct file *file)
223 struct ll_file_data *fd;
224 struct ll_sb_info *sbi = ll_i2sbi(inode);
228 CDEBUG(D_VFSTRACE, "VFS Op:inode="DLID4"(%p)\n",
229 OLID4(&ll_i2info(inode)->lli_id), inode);
231 /* don't do anything for / */
232 if (inode->i_sb->s_root == file->f_dentry)
235 lprocfs_counter_incr(sbi->ll_stats, LPROC_LL_RELEASE);
236 fd = (struct ll_file_data *)file->private_data;
239 rc = ll_md_close(sbi->ll_md_exp, inode, file);
243 static int ll_intent_file_open(struct file *file, void *lmm,
244 int lmmsize, struct lookup_intent *itp)
246 struct ll_sb_info *sbi = ll_i2sbi(file->f_dentry->d_inode);
247 struct dentry *parent = file->f_dentry->d_parent;
248 const char *name = file->f_dentry->d_name.name;
249 const int len = file->f_dentry->d_name.len;
250 struct lustre_handle lockh;
251 struct mdc_op_data *op_data;
257 OBD_ALLOC(op_data, sizeof(*op_data));
261 ll_prepare_mdc_data(op_data, parent->d_inode, NULL,
264 rc = md_enqueue(sbi->ll_md_exp, LDLM_IBITS, itp, LCK_PR, op_data,
265 &lockh, lmm, lmmsize, ldlm_completion_ast,
266 ll_mdc_blocking_ast, NULL);
267 OBD_FREE(op_data, sizeof(*op_data));
269 if (LUSTRE_IT(itp)->it_lock_mode)
270 memcpy(&LUSTRE_IT(itp)->it_lock_handle,
271 &lockh, sizeof(lockh));
274 CERROR("lock enqueue: err: %d\n", rc);
279 void ll_och_fill(struct inode *inode, struct lookup_intent *it,
280 struct obd_client_handle *och)
282 struct ptlrpc_request *req = LUSTRE_IT(it)->it_data;
283 struct ll_inode_info *lli = ll_i2info(inode);
284 struct mds_body *body;
287 body = lustre_msg_buf (req->rq_repmsg, 1, sizeof (*body));
288 LASSERT (body != NULL); /* reply already checked out */
289 LASSERT_REPSWABBED (req, 1); /* and swabbed down */
291 memcpy(&och->och_fh, &body->handle, sizeof(body->handle));
292 och->och_magic = OBD_CLIENT_HANDLE_MAGIC;
293 lli->lli_io_epoch = body->io_epoch;
294 mdc_set_open_replay_data(ll_i2mdexp(inode), och,
295 LUSTRE_IT(it)->it_data);
298 int ll_local_open(struct file *file, struct lookup_intent *it,
299 struct obd_client_handle *och)
301 struct ll_file_data *fd;
305 ll_och_fill(file->f_dentry->d_inode, it, och);
307 LASSERTF(file->private_data == NULL, "file %.*s/%.*s ino %lu/%u (%o)\n",
308 file->f_dentry->d_name.len, file->f_dentry->d_name.name,
309 file->f_dentry->d_parent->d_name.len,
310 file->f_dentry->d_parent->d_name.name,
311 file->f_dentry->d_inode->i_ino,
312 file->f_dentry->d_inode->i_generation,
313 file->f_dentry->d_inode->i_mode);
315 OBD_SLAB_ALLOC(fd, ll_file_data_slab, SLAB_KERNEL, sizeof *fd);
317 /* We can't handle this well without reorganizing ll_file_open and
318 * ll_md_close(), so don't even try right now. */
321 file->private_data = fd;
322 ll_readahead_init(file->f_dentry->d_inode, &fd->fd_ras);
323 fd->fd_omode = it->it_flags;
327 /* Open a file, and (for the very first open) create objects on the OSTs at
328 * this time. If opened with O_LOV_DELAY_CREATE, then we don't do the object
329 * creation or open until ll_lov_setstripe() ioctl is called. We grab
330 * lli_open_sem to ensure no other process will create objects, send the
331 * stripe MD to the MDS, or try to destroy the objects if that fails.
333 * If we already have the stripe MD locally then we don't request it in
334 * mdc_open(), by passing a lmm_size = 0.
336 * It is up to the application to ensure no other processes open this file
337 * in the O_LOV_DELAY_CREATE case, or the default striping pattern will be
338 * used. We might be able to avoid races of that sort by getting lli_open_sem
339 * before returning in the O_LOV_DELAY_CREATE case and dropping it here
340 * or in ll_file_release(), but I'm not sure that is desirable/necessary.
342 int ll_file_open(struct inode *inode, struct file *file)
344 struct ll_inode_info *lli = ll_i2info(inode);
345 struct lookup_intent *it, oit = { .it_op = IT_OPEN,
346 .it_flags = file->f_flags };
347 struct lov_stripe_md *lsm;
348 struct ptlrpc_request *req;
350 struct obd_client_handle **och_p;
354 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), flags %o\n",
355 inode->i_ino, inode->i_generation, inode, file->f_flags);
357 /* don't do anything for / */
358 if (inode->i_sb->s_root == file->f_dentry)
361 if ((file->f_flags+1) & O_ACCMODE)
363 if (file->f_flags & O_TRUNC)
369 * sometimes LUSTRE_IT(it) may not be allocated like opening file by
370 * dentry_open() from GNS stuff.
372 if (!it || !LUSTRE_IT(it)) {
374 rc = ll_intent_alloc(it);
379 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_OPEN);
382 * mdc_intent_lock() didn't get a request ref if there was an open
383 * error, so don't do cleanup on the * request here (bug 3430)
385 if (LUSTRE_IT(it)->it_disposition) {
386 rc = it_open_error(DISP_OPEN_OPEN, it);
391 /* Let's see if we have file open on MDS already. */
392 if (it->it_flags & FMODE_WRITE) {
393 och_p = &lli->lli_mds_write_och;
394 och_usecount = &lli->lli_open_fd_write_count;
395 } else if (it->it_flags & FMODE_EXEC) {
396 och_p = &lli->lli_mds_exec_och;
397 och_usecount = &lli->lli_open_fd_exec_count;
399 och_p = &lli->lli_mds_read_och;
400 och_usecount = &lli->lli_open_fd_read_count;
403 down(&lli->lli_och_sem);
404 if (*och_p) { /* Open handle is present */
405 if (LUSTRE_IT(it)->it_disposition) {
406 struct obd_client_handle *och;
407 /* Well, there's extra open request that we do not need,
408 let's close it somehow*/
409 OBD_ALLOC(och, sizeof (struct obd_client_handle));
411 up(&lli->lli_och_sem);
415 ll_och_fill(inode, it, och);
416 /* ll_md_och_close() will free och */
417 ll_md_och_close(ll_i2mdexp(inode), inode, och);
421 rc = ll_local_open(file, it, NULL);
425 LASSERT(*och_usecount == 0);
426 OBD_ALLOC(*och_p, sizeof (struct obd_client_handle));
428 GOTO(out, rc = -ENOMEM);
431 if (!it || !LUSTRE_IT(it) || !LUSTRE_IT(it)->it_disposition) {
433 * we are going to replace intent here, and that may
434 * possibly change access mode (FMODE_EXEC can only be
435 * set in intent), but I hope it never happens (I was
436 * not able to trigger it yet at least) -- green
439 /* FIXME: FMODE_EXEC is not covered by O_ACCMODE! */
440 LASSERT(!(it->it_flags & FMODE_EXEC));
441 LASSERTF((it->it_flags & O_ACCMODE) ==
442 (oit.it_flags & O_ACCMODE), "Changing intent "
443 "flags %x to incompatible %x\n", it->it_flags,
446 rc = ll_intent_file_open(file, NULL, 0, it);
449 rc = it_open_error(DISP_OPEN_OPEN, it);
451 GOTO(out_och_free, rc);
453 mdc_set_lock_data(NULL, &LUSTRE_IT(it)->it_lock_handle,
454 file->f_dentry->d_inode);
456 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_OPEN);
457 rc = ll_local_open(file, it, *och_p);
458 LASSERTF(rc == 0, "rc = %d\n", rc);
460 up(&lli->lli_och_sem);
463 * must do this outside lli_och_sem lock to prevent deadlock where
464 * different kind of OPEN lock for this same inode gets cancelled by
468 if (!S_ISREG(inode->i_mode))
473 if (file->f_flags & O_LOV_DELAY_CREATE ||
474 !(file->f_mode & FMODE_WRITE)) {
475 CDEBUG(D_INODE, "object creation was delayed\n");
479 file->f_flags &= ~O_LOV_DELAY_CREATE;
482 req = LUSTRE_IT(it)->it_data;
483 ll_intent_drop_lock(it);
484 ll_intent_release(it);
485 ptlrpc_req_finished(req);
487 ll_open_complete(inode);
491 OBD_FREE(*och_p, sizeof (struct obd_client_handle));
492 *och_p = NULL; /* OBD_FREE writes some magic there */
495 up(&lli->lli_och_sem);
501 /* Fills the obdo with the attributes for the inode defined by lsm */
502 int ll_lsm_getattr(struct obd_export *exp, struct lov_stripe_md *lsm,
505 struct ptlrpc_request_set *set;
509 LASSERT(lsm != NULL);
511 memset(oa, 0, sizeof *oa);
512 oa->o_id = lsm->lsm_object_id;
513 oa->o_gr = lsm->lsm_object_gr;
514 oa->o_mode = S_IFREG;
515 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLSIZE |
516 OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
517 OBD_MD_FLCTIME | OBD_MD_FLGROUP;
519 set = ptlrpc_prep_set();
523 rc = obd_getattr_async(exp, oa, lsm, set);
525 rc = ptlrpc_set_wait(set);
526 ptlrpc_set_destroy(set);
531 oa->o_valid &= (OBD_MD_FLBLOCKS | OBD_MD_FLBLKSZ | OBD_MD_FLMTIME |
532 OBD_MD_FLCTIME | OBD_MD_FLSIZE);
536 static inline void ll_remove_suid(struct inode *inode)
540 /* set S_IGID if S_IXGRP is set, and always set S_ISUID */
541 mode = (inode->i_mode & S_IXGRP)*(S_ISGID/S_IXGRP) | S_ISUID;
543 /* was any of the uid bits set? */
544 mode &= inode->i_mode;
545 if (mode && !capable(CAP_FSETID)) {
546 inode->i_mode &= ~mode;
547 // XXX careful here - we cannot change the size
551 static int ll_lock_to_stripe_offset(struct inode *inode, struct ldlm_lock *lock)
553 struct ll_inode_info *lli = ll_i2info(inode);
554 struct lov_stripe_md *lsm = lli->lli_smd;
555 struct obd_export *exp = ll_i2dtexp(inode);
558 struct ldlm_lock *lock;
559 struct lov_stripe_md *lsm;
560 } key = { .name = "lock_to_stripe", .lock = lock, .lsm = lsm };
561 __u32 stripe, vallen = sizeof(stripe);
565 if (lsm->lsm_stripe_count == 1)
566 GOTO(check, stripe = 0);
568 /* get our offset in the lov */
569 rc = obd_get_info(exp, sizeof(key), &key, &vallen, &stripe);
571 CERROR("obd_get_info: rc = %d\n", rc);
574 LASSERT(stripe < lsm->lsm_stripe_count);
577 if (lsm->lsm_oinfo[stripe].loi_id != lock->l_resource->lr_name.name[0]||
578 lsm->lsm_oinfo[stripe].loi_gr != lock->l_resource->lr_name.name[2]){
579 LDLM_ERROR(lock, "resource doesn't match object "LPU64"/"LPU64
580 " inode=%lu/%u (%p)\n",
581 lsm->lsm_oinfo[stripe].loi_id,
582 lsm->lsm_oinfo[stripe].loi_gr,
583 inode->i_ino, inode->i_generation, inode);
584 return -ELDLM_NO_LOCK_DATA;
590 /* Flush the page cache for an extent as its canceled. When we're on an LOV,
591 * we get a lock cancellation for each stripe, so we have to map the obd's
592 * region back onto the stripes in the file that it held.
594 * No one can dirty the extent until we've finished our work and they can
595 * enqueue another lock. The DLM protects us from ll_file_read/write here,
596 * but other kernel actors could have pages locked.
598 * Called with the DLM lock held. */
599 void ll_pgcache_remove_extent(struct inode *inode, struct lov_stripe_md *lsm,
600 struct ldlm_lock *lock, __u32 stripe)
602 ldlm_policy_data_t tmpex;
603 unsigned long start, end, count, skip, i, j;
605 int rc, rc2, discard = lock->l_flags & LDLM_FL_DISCARD_DATA;
606 struct lustre_handle lockh;
609 memcpy(&tmpex, &lock->l_policy_data, sizeof(tmpex));
610 CDEBUG(D_INODE|D_PAGE, "inode %lu(%p) ["LPU64"->"LPU64"] size: %llu\n",
611 inode->i_ino, inode, tmpex.l_extent.start, tmpex.l_extent.end,
614 /* our locks are page granular thanks to osc_enqueue, we invalidate the
616 LASSERT((tmpex.l_extent.start & ~PAGE_CACHE_MASK) == 0);
617 LASSERT(((tmpex.l_extent.end + 1) & ~PAGE_CACHE_MASK) == 0);
621 start = tmpex.l_extent.start >> PAGE_CACHE_SHIFT;
622 end = tmpex.l_extent.end >> PAGE_CACHE_SHIFT;
623 if (lsm->lsm_stripe_count > 1) {
624 count = lsm->lsm_stripe_size >> PAGE_CACHE_SHIFT;
625 skip = (lsm->lsm_stripe_count - 1) * count;
626 start += start/count * skip + stripe * count;
628 end += end/count * skip + stripe * count;
630 if (end < tmpex.l_extent.end >> PAGE_CACHE_SHIFT)
633 i = inode->i_size ? (inode->i_size - 1) >> PAGE_CACHE_SHIFT : 0;
637 CDEBUG(D_INODE|D_PAGE, "walking page indices start: %lu j: %lu "
638 "count: %lu skip: %lu end: %lu%s\n", start, start % count,
639 count, skip, end, discard ? " (DISCARDING)" : "");
641 /* walk through the vmas on the inode and tear down mmaped pages that
642 * intersect with the lock. this stops immediately if there are no
643 * mmap()ed regions of the file. This is not efficient at all and
644 * should be short lived. We'll associate mmap()ed pages with the lock
645 * and will be able to find them directly */
647 for (i = start; i <= end; i += (j + skip)) {
648 j = min(count - (i % count), end - i + 1);
650 LASSERT(inode->i_mapping);
651 if (ll_teardown_mmaps(inode->i_mapping, i << PAGE_CACHE_SHIFT,
652 ((i+j) << PAGE_CACHE_SHIFT) - 1) )
656 /* this is the simplistic implementation of page eviction at
657 * cancelation. It is careful to get races with other page
658 * lockers handled correctly. fixes from bug 20 will make it
659 * more efficient by associating locks with pages and with
660 * batching writeback under the lock explicitly. */
661 for (i = start, j = start % count; i <= end;
662 j++, i++, tmpex.l_extent.start += PAGE_CACHE_SIZE) {
664 CDEBUG(D_PAGE, "skip index %lu to %lu\n", i, i + skip);
670 LASSERTF(tmpex.l_extent.start< lock->l_policy_data.l_extent.end,
671 LPU64" >= "LPU64" start %lu i %lu end %lu\n",
672 tmpex.l_extent.start, lock->l_policy_data.l_extent.end,
675 if (!mapping_has_pages(inode->i_mapping)) {
676 CDEBUG(D_INODE|D_PAGE, "nothing left\n");
682 page = find_get_page(inode->i_mapping, i);
685 LL_CDEBUG_PAGE(D_PAGE, page, "lock page idx %lu ext "LPU64"\n",
686 i, tmpex.l_extent.start);
689 /* page->mapping to check with racing against teardown */
690 if (!discard && clear_page_dirty_for_io(page)) {
691 rc = ll_call_writepage(inode, page);
693 CERROR("writepage of page %p failed: %d\n",
695 /* either waiting for io to complete or reacquiring
696 * the lock that the failed writepage released */
700 tmpex.l_extent.end = tmpex.l_extent.start + PAGE_CACHE_SIZE - 1;
701 /* check to see if another DLM lock covers this page */
702 rc2 = ldlm_lock_match(lock->l_resource->lr_namespace,
703 LDLM_FL_BLOCK_GRANTED|LDLM_FL_CBPENDING |
705 &lock->l_resource->lr_name, LDLM_EXTENT,
706 &tmpex, LCK_PR | LCK_PW, &lockh);
707 if (rc2 == 0 && page->mapping != NULL) {
708 // checking again to account for writeback's lock_page()
709 LL_CDEBUG_PAGE(D_PAGE, page, "truncating\n");
710 ll_ra_accounting(page, inode->i_mapping);
711 ll_truncate_complete_page(page);
714 page_cache_release(page);
716 LASSERTF(tmpex.l_extent.start <=
717 (lock->l_policy_data.l_extent.end == ~0ULL ? ~0ULL :
718 lock->l_policy_data.l_extent.end + 1),
719 "loop too long "LPU64" > "LPU64" start %lu i %lu end %lu\n",
720 tmpex.l_extent.start, lock->l_policy_data.l_extent.end,
725 static int ll_extent_lock_callback(struct ldlm_lock *lock,
726 struct ldlm_lock_desc *new, void *data,
729 struct lustre_handle lockh = { 0 };
733 if ((unsigned long)data > 0 && (unsigned long)data < 0x1000) {
734 LDLM_ERROR(lock, "cancelling lock with bad data %p", data);
739 case LDLM_CB_BLOCKING:
740 ldlm_lock2handle(lock, &lockh);
741 rc = ldlm_cli_cancel(&lockh);
743 CERROR("ldlm_cli_cancel failed: %d\n", rc);
745 case LDLM_CB_CANCELING: {
747 struct ll_inode_info *lli;
748 struct lov_stripe_md *lsm;
752 /* This lock wasn't granted, don't try to evict pages */
753 if (lock->l_req_mode != lock->l_granted_mode)
756 inode = ll_inode_from_lock(lock);
759 lli = ll_i2info(inode);
762 if (lli->lli_smd == NULL)
766 stripe = ll_lock_to_stripe_offset(inode, lock);
769 ll_pgcache_remove_extent(inode, lsm, lock, stripe);
771 l_lock(&lock->l_resource->lr_namespace->ns_lock);
772 down(&lli->lli_size_sem);
773 kms = ldlm_extent_shift_kms(lock,
774 lsm->lsm_oinfo[stripe].loi_kms);
776 if (lsm->lsm_oinfo[stripe].loi_kms != kms)
777 LDLM_DEBUG(lock, "updating kms from "LPU64" to "LPU64,
778 lsm->lsm_oinfo[stripe].loi_kms, kms);
779 lsm->lsm_oinfo[stripe].loi_kms = kms;
780 up(&lli->lli_size_sem);
781 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
782 //ll_try_done_writing(inode);
795 int ll_async_completion_ast(struct ldlm_lock *lock, int flags, void *data)
797 /* XXX ALLOCATE - 160 bytes */
798 struct inode *inode = ll_inode_from_lock(lock);
799 struct ll_inode_info *lli = ll_i2info(inode);
800 struct lustre_handle lockh = { 0 };
805 if (flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
806 LDLM_FL_BLOCK_CONV)) {
807 LBUG(); /* not expecting any blocked async locks yet */
808 LDLM_DEBUG(lock, "client-side async enqueue returned a blocked "
810 ldlm_lock_dump(D_OTHER, lock, 0);
811 ldlm_reprocess_all(lock->l_resource);
815 LDLM_DEBUG(lock, "client-side async enqueue: granted/glimpsed");
817 stripe = ll_lock_to_stripe_offset(inode, lock);
821 if (lock->l_lvb_len) {
822 struct lov_stripe_md *lsm = lli->lli_smd;
824 lvb = lock->l_lvb_data;
825 lsm->lsm_oinfo[stripe].loi_rss = lvb->lvb_size;
827 l_lock(&lock->l_resource->lr_namespace->ns_lock);
829 kms = MAX(lsm->lsm_oinfo[stripe].loi_kms, lvb->lvb_size);
830 kms = ldlm_extent_shift_kms(NULL, kms);
831 if (lsm->lsm_oinfo[stripe].loi_kms != kms)
832 LDLM_DEBUG(lock, "updating kms from "LPU64" to "LPU64,
833 lsm->lsm_oinfo[stripe].loi_kms, kms);
834 lsm->lsm_oinfo[stripe].loi_kms = kms;
836 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
841 wake_up(&lock->l_waitq);
843 ldlm_lock2handle(lock, &lockh);
844 ldlm_lock_decref(&lockh, LCK_PR);
849 static int ll_glimpse_callback(struct ldlm_lock *lock, void *reqp)
851 struct ptlrpc_request *req = reqp;
852 struct inode *inode = ll_inode_from_lock(lock);
853 struct ll_inode_info *lli;
855 struct lov_stripe_md *lsm;
856 int rc, size = sizeof(*lvb), stripe;
860 GOTO(out, rc = -ELDLM_NO_LOCK_DATA);
861 lli = ll_i2info(inode);
863 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
867 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
869 /* First, find out which stripe index this lock corresponds to. */
870 stripe = ll_lock_to_stripe_offset(inode, lock);
872 GOTO(iput, rc = -ELDLM_NO_LOCK_DATA);
874 rc = lustre_pack_reply(req, 1, &size, NULL);
876 CERROR("lustre_pack_reply: %d\n", rc);
880 lvb = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*lvb));
881 lvb->lvb_size = lli->lli_smd->lsm_oinfo[stripe].loi_kms;
882 lvb->lvb_mtime = LTIME_S(inode->i_mtime);
883 lvb->lvb_atime = LTIME_S(inode->i_atime);
884 lvb->lvb_ctime = LTIME_S(inode->i_ctime);
886 LDLM_DEBUG(lock, "i_size: %llu -> stripe number %u -> kms "LPU64,
887 inode->i_size, stripe, lvb->lvb_size);
893 /* These errors are normal races, so we don't want to fill the console
894 * with messages by calling ptlrpc_error() */
895 if (rc == -ELDLM_NO_LOCK_DATA)
896 lustre_pack_reply(req, 0, NULL, NULL);
902 __u64 lov_merge_size(struct lov_stripe_md *lsm, int kms);
903 __u64 lov_merge_blocks(struct lov_stripe_md *lsm);
904 __u64 lov_merge_mtime(struct lov_stripe_md *lsm, __u64 current_time);
906 /* NB: lov_merge_size will prefer locally cached writes if they extend the
907 * file (because it prefers KMS over RSS when larger) */
908 int ll_glimpse_size(struct inode *inode)
910 struct ll_inode_info *lli = ll_i2info(inode);
911 struct ll_sb_info *sbi = ll_i2sbi(inode);
912 ldlm_policy_data_t policy = { .l_extent = { 0, OBD_OBJECT_EOF } };
913 struct lustre_handle lockh = { 0 };
914 int rc, flags = LDLM_FL_HAS_INTENT;
917 CDEBUG(D_DLMTRACE, "Glimpsing inode %lu\n", inode->i_ino);
919 rc = obd_enqueue(sbi->ll_dt_exp, lli->lli_smd, LDLM_EXTENT, &policy,
920 LCK_PR, &flags, ll_extent_lock_callback,
921 ldlm_completion_ast, ll_glimpse_callback, inode,
922 sizeof(struct ost_lvb), lustre_swab_ost_lvb, &lockh);
927 CERROR("obd_enqueue returned rc %d, returning -EIO\n", rc);
928 RETURN(rc > 0 ? -EIO : rc);
931 down(&lli->lli_size_sem);
932 inode->i_size = lov_merge_size(lli->lli_smd, 0);
933 inode->i_blocks = lov_merge_blocks(lli->lli_smd);
934 up(&lli->lli_size_sem);
936 LTIME_S(inode->i_mtime) = lov_merge_mtime(lli->lli_smd,
937 LTIME_S(inode->i_mtime));
939 CDEBUG(D_DLMTRACE, "glimpse: size: "LPU64", blocks: "LPU64"\n",
940 (__u64)inode->i_size, (__u64)inode->i_blocks);
942 obd_cancel(sbi->ll_dt_exp, lli->lli_smd, LCK_PR, &lockh);
946 void ll_stime_record(struct ll_sb_info *sbi, struct timeval *start,
947 struct obd_service_time *stime)
950 do_gettimeofday(&stop);
952 spin_lock(&sbi->ll_lock);
953 lprocfs_stime_record(stime, &stop, start);
954 spin_unlock(&sbi->ll_lock);
957 int ll_extent_lock(struct ll_file_data *fd, struct inode *inode,
958 struct lov_stripe_md *lsm, int mode,
959 ldlm_policy_data_t *policy, struct lustre_handle *lockh,
960 int ast_flags, struct obd_service_time *stime)
962 struct ll_inode_info *lli = ll_i2info(inode);
963 struct ll_sb_info *sbi = ll_i2sbi(inode);
964 struct timeval start;
968 LASSERT(lockh->cookie == 0);
970 /* XXX phil: can we do this? won't it screw the file size up? */
971 if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
972 (sbi->ll_flags & LL_SBI_NOLCK))
975 CDEBUG(D_DLMTRACE, "Locking inode %lu, start "LPU64" end "LPU64"\n",
976 inode->i_ino, policy->l_extent.start, policy->l_extent.end);
978 do_gettimeofday(&start);
979 rc = obd_enqueue(sbi->ll_dt_exp, lsm, LDLM_EXTENT, policy, mode,
980 &ast_flags, ll_extent_lock_callback,
981 ldlm_completion_ast, ll_glimpse_callback, inode,
982 sizeof(struct ost_lvb), lustre_swab_ost_lvb, lockh);
986 ll_stime_record(sbi, &start, stime);
988 if (policy->l_extent.start == 0 &&
989 policy->l_extent.end == OBD_OBJECT_EOF) {
990 /* vmtruncate()->ll_truncate() first sets the i_size and then
991 * the kms under both a DLM lock and the i_sem. If we don't
992 * get the i_sem here we can match the DLM lock and reset
993 * i_size from the kms before the truncating path has updated
994 * the kms. generic_file_write can then trust the stale i_size
995 * when doing appending writes and effectively cancel the
996 * result of the truncate. Getting the i_sem after the enqueue
997 * maintains the DLM -> i_sem acquiry order. */
998 down(&lli->lli_size_sem);
999 inode->i_size = lov_merge_size(lsm, 1);
1000 up(&lli->lli_size_sem);
1004 LTIME_S(inode->i_mtime) =
1005 lov_merge_mtime(lsm, LTIME_S(inode->i_mtime));
1011 int ll_extent_unlock(struct ll_file_data *fd, struct inode *inode,
1012 struct lov_stripe_md *lsm, int mode,
1013 struct lustre_handle *lockh)
1015 struct ll_sb_info *sbi = ll_i2sbi(inode);
1019 /* XXX phil: can we do this? won't it screw the file size up? */
1020 if ((fd && (fd->fd_flags & LL_FILE_IGNORE_LOCK)) ||
1021 (sbi->ll_flags & LL_SBI_NOLCK))
1024 rc = obd_cancel(sbi->ll_dt_exp, lsm, mode, lockh);
1029 static ssize_t ll_file_read(struct file *file, char *buf, size_t count,
1032 struct inode *inode = file->f_dentry->d_inode;
1033 struct ll_inode_info *lli = ll_i2info(inode);
1034 struct lov_stripe_md *lsm = lli->lli_smd;
1035 struct ll_lock_tree tree;
1036 struct ll_lock_tree_node *node;
1041 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
1042 inode->i_ino, inode->i_generation, inode, count, *ppos);
1044 /* "If nbyte is 0, read() will return 0 and have no other results."
1045 * -- Single Unix Spec */
1049 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_READ_BYTES,
1055 node = ll_node_from_inode(inode, *ppos, *ppos + count - 1,
1058 tree.lt_fd = file->private_data;
1060 rc = ll_tree_lock(&tree, node, inode, buf, count,
1061 file->f_flags & O_NONBLOCK ? LDLM_FL_BLOCK_NOWAIT :0);
1065 down(&lli->lli_size_sem);
1066 kms = lov_merge_size(lsm, 1);
1067 if (*ppos + count - 1 > kms) {
1068 /* A glimpse is necessary to determine whether we return a short
1069 * read or some zeroes at the end of the buffer */
1070 up(&lli->lli_size_sem);
1071 retval = ll_glimpse_size(inode);
1075 inode->i_size = kms;
1076 up(&lli->lli_size_sem);
1079 CDEBUG(D_INFO, "Read ino %lu, "LPSZ" bytes, offset %lld, i_size %llu\n",
1080 inode->i_ino, count, *ppos, inode->i_size);
1082 /* turn off the kernel's read-ahead */
1083 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1086 file->f_ra.ra_pages = 0;
1088 retval = generic_file_read(file, buf, count, ppos);
1091 ll_tree_unlock(&tree, inode);
1096 * Write to a file (through the page cache).
1098 static ssize_t ll_file_write(struct file *file, const char *buf,
1099 size_t count, loff_t *ppos)
1101 struct inode *inode = file->f_dentry->d_inode;
1102 loff_t maxbytes = ll_file_maxbytes(inode);
1103 struct ll_lock_tree tree;
1104 struct ll_lock_tree_node *node;
1109 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),size="LPSZ",offset=%Ld\n",
1110 inode->i_ino, inode->i_generation, inode, count, *ppos);
1112 SIGNAL_MASK_ASSERT(); /* XXX BUG 1511 */
1114 /* POSIX, but surprised the VFS doesn't check this already */
1118 /* If file was opened for LL_IOC_LOV_SETSTRIPE but the ioctl wasn't
1119 * called on the file, don't fail the below assertion (bug 2388). */
1120 if (file->f_flags & O_LOV_DELAY_CREATE &&
1121 ll_i2info(inode)->lli_smd == NULL)
1124 LASSERT(ll_i2info(inode)->lli_smd != NULL);
1126 if (file->f_flags & O_APPEND)
1127 node = ll_node_from_inode(inode, 0, OBD_OBJECT_EOF, LCK_PW);
1129 node = ll_node_from_inode(inode, *ppos, *ppos + count - 1,
1133 RETURN(PTR_ERR(node));
1135 tree.lt_fd = file->private_data;
1137 rc = ll_tree_lock(&tree, node, inode, buf, count,
1138 file->f_flags & O_NONBLOCK ? LDLM_FL_BLOCK_NOWAIT :0);
1142 /* this is ok, g_f_w will overwrite this under i_sem if it races
1143 * with a local truncate, it just makes our maxbyte checking easier */
1144 if (file->f_flags & O_APPEND)
1145 *ppos = inode->i_size;
1147 if (*ppos >= maxbytes) {
1148 if (count || *ppos > maxbytes) {
1149 send_sig(SIGXFSZ, current, 0);
1150 GOTO(out, retval = -EFBIG);
1153 if (*ppos + count > maxbytes)
1154 count = maxbytes - *ppos;
1156 CDEBUG(D_INFO, "Writing inode %lu, "LPSZ" bytes, offset %Lu\n",
1157 inode->i_ino, count, *ppos);
1159 /* generic_file_write handles O_APPEND after getting i_sem */
1160 retval = generic_file_write(file, buf, count, ppos);
1163 ll_tree_unlock(&tree, inode);
1164 /* serialize with mmap/munmap/mremap */
1165 lprocfs_counter_add(ll_i2sbi(inode)->ll_stats, LPROC_LL_WRITE_BYTES,
1166 retval > 0 ? retval : 0);
1170 static int ll_lov_recreate_obj(struct inode *inode, struct file *file,
1173 struct ll_inode_info *lli = ll_i2info(inode);
1174 struct obd_export *exp = ll_i2dtexp(inode);
1175 struct ll_recreate_obj ucreatp;
1176 struct obd_trans_info oti = { 0 };
1177 struct obdo *oa = NULL;
1180 struct lov_stripe_md *lsm, *lsm2;
1183 if (!capable (CAP_SYS_ADMIN))
1186 rc = copy_from_user(&ucreatp, (struct ll_recreate_obj *)arg,
1187 sizeof(struct ll_recreate_obj));
1195 down(&lli->lli_open_sem);
1198 GOTO(out, rc = -ENOENT);
1199 lsm_size = sizeof(*lsm) + (sizeof(struct lov_oinfo) *
1200 (lsm->lsm_stripe_count));
1202 OBD_ALLOC(lsm2, lsm_size);
1204 GOTO(out, rc = -ENOMEM);
1206 oa->o_id = ucreatp.lrc_id;
1207 oa->o_nlink = ucreatp.lrc_ost_idx;
1208 oa->o_gr = ucreatp.lrc_group;
1209 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP | OBD_MD_FLFLAGS;
1210 oa->o_flags |= OBD_FL_RECREATE_OBJS;
1211 obdo_from_inode(oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
1212 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
1214 oti.oti_objid = NULL;
1215 memcpy(lsm2, lsm, lsm_size);
1216 rc = obd_create(exp, oa, &lsm2, &oti);
1218 OBD_FREE(lsm2, lsm_size);
1221 up(&lli->lli_open_sem);
1226 static int ll_lov_setstripe_ea_info(struct inode *inode, struct file *file,
1227 int flags, struct lov_user_md *lum,
1230 struct ll_inode_info *lli = ll_i2info(inode);
1232 struct obd_export *exp = ll_i2dtexp(inode);
1233 struct lov_stripe_md *lsm;
1234 struct lookup_intent oit = {.it_op = IT_OPEN, .it_flags = flags};
1235 struct ptlrpc_request *req = NULL;
1237 struct lustre_md md;
1238 struct obd_client_handle *och;
1242 if ((file->f_flags+1) & O_ACCMODE)
1244 if (file->f_flags & O_TRUNC)
1247 down(&lli->lli_open_sem);
1250 up(&lli->lli_open_sem);
1251 CDEBUG(D_IOCTL, "stripe already exists for ino %lu\n",
1256 f = get_empty_filp();
1260 f->f_dentry = file->f_dentry;
1261 f->f_vfsmnt = file->f_vfsmnt;
1264 rc = ll_intent_alloc(&oit);
1268 rc = ll_intent_file_open(f, lum, lum_size, &oit);
1271 if (it_disposition(&oit, DISP_LOOKUP_NEG))
1274 req = LUSTRE_IT(&oit)->it_data;
1275 rc = LUSTRE_IT(&oit)->it_status;
1280 rc = mdc_req2lustre_md(ll_i2mdexp(inode), req, 1, exp, &md);
1283 ll_update_inode(f->f_dentry->d_inode, &md);
1285 OBD_ALLOC(och, sizeof(struct obd_client_handle));
1286 rc = ll_local_open(f, &oit, och);
1287 if (rc) { /* Actually ll_local_open cannot fail! */
1290 if (LUSTRE_IT(&oit)->it_lock_mode) {
1291 ldlm_lock_decref_and_cancel((struct lustre_handle *)
1292 &LUSTRE_IT(&oit)->it_lock_handle,
1293 LUSTRE_IT(&oit)->it_lock_mode);
1294 LUSTRE_IT(&oit)->it_lock_mode = 0;
1297 ll_intent_release(&oit);
1299 /* ll_file_release will decrease the count, but won't free anything
1300 because we have at least one more reference coming from actual open
1302 down(&lli->lli_och_sem);
1303 lli->lli_open_fd_write_count++;
1304 up(&lli->lli_och_sem);
1305 rc = ll_file_release(f->f_dentry->d_inode, f);
1307 /* Now also destroy our supplemental och */
1308 ll_md_och_close(ll_i2mdexp(inode), f->f_dentry->d_inode, och);
1311 ll_intent_release(&oit);
1314 up(&lli->lli_open_sem);
1316 ptlrpc_req_finished(req);
1320 static int ll_lov_setea(struct inode *inode, struct file *file,
1323 int flags = MDS_OPEN_HAS_OBJS | FMODE_WRITE;
1324 struct lov_user_md *lump;
1325 int lum_size = sizeof(struct lov_user_md) +
1326 sizeof(struct lov_user_ost_data);
1330 if (!capable (CAP_SYS_ADMIN))
1333 OBD_ALLOC(lump, lum_size);
1337 rc = copy_from_user(lump, (struct lov_user_md *)arg, lum_size);
1339 OBD_FREE(lump, lum_size);
1343 rc = ll_lov_setstripe_ea_info(inode, file, flags, lump, lum_size);
1345 OBD_FREE(lump, lum_size);
1349 static int ll_lov_setstripe(struct inode *inode, struct file *file,
1352 struct lov_user_md lum, *lump = (struct lov_user_md *)arg;
1354 int flags = FMODE_WRITE;
1357 /* Bug 1152: copy properly when this is no longer true */
1358 LASSERT(sizeof(lum) == sizeof(*lump));
1359 LASSERT(sizeof(lum.lmm_objects[0]) == sizeof(lump->lmm_objects[0]));
1360 rc = copy_from_user(&lum, lump, sizeof(lum));
1364 rc = ll_lov_setstripe_ea_info(inode, file, flags, &lum, sizeof(lum));
1366 put_user(0, &lump->lmm_stripe_count);
1367 rc = obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode),
1368 0, ll_i2info(inode)->lli_smd, lump);
1373 static int ll_lov_getstripe(struct inode *inode, unsigned long arg)
1375 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1380 return obd_iocontrol(LL_IOC_LOV_GETSTRIPE, ll_i2dtexp(inode), 0, lsm,
1384 static int ll_get_grouplock(struct inode *inode, struct file *file,
1387 struct ll_file_data *fd = file->private_data;
1388 ldlm_policy_data_t policy = { .l_extent = { .start = 0,
1389 .end = OBD_OBJECT_EOF}};
1390 struct lustre_handle lockh = { 0 };
1391 struct ll_inode_info *lli = ll_i2info(inode);
1392 struct lov_stripe_md *lsm = lli->lli_smd;
1396 if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
1400 policy.l_extent.gid = arg;
1401 if (file->f_flags & O_NONBLOCK)
1402 flags = LDLM_FL_BLOCK_NOWAIT;
1404 rc = ll_extent_lock(fd, inode, lsm, LCK_GROUP, &policy, &lockh, flags,
1405 &ll_i2sbi(inode)->ll_grouplock_stime);
1409 fd->fd_flags |= LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK;
1411 memcpy(&fd->fd_cwlockh, &lockh, sizeof(lockh));
1416 static int ll_put_grouplock(struct inode *inode, struct file *file,
1419 struct ll_file_data *fd = file->private_data;
1420 struct ll_inode_info *lli = ll_i2info(inode);
1421 struct lov_stripe_md *lsm = lli->lli_smd;
1425 if (!(fd->fd_flags & LL_FILE_GROUP_LOCKED)) {
1426 /* Ugh, it's already unlocked. */
1430 if (fd->fd_gid != arg) /* Ugh? Unlocking with different gid? */
1433 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
1435 rc = ll_extent_unlock(fd, inode, lsm, LCK_GROUP, &fd->fd_cwlockh);
1440 memset(&fd->fd_cwlockh, 0, sizeof(fd->fd_cwlockh));
1445 int ll_file_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
1448 struct ll_file_data *fd = file->private_data;
1449 struct ll_sb_info *sbi = ll_i2sbi(inode);
1453 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),cmd=%x\n", inode->i_ino,
1454 inode->i_generation, inode, cmd);
1456 if (_IOC_TYPE(cmd) == 'T') /* tty ioctls */
1459 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_IOCTL);
1461 case LL_IOC_GETFLAGS:
1462 /* Get the current value of the file flags */
1463 return put_user(fd->fd_flags, (int *)arg);
1464 case LL_IOC_SETFLAGS:
1465 case LL_IOC_CLRFLAGS:
1466 /* Set or clear specific file flags */
1467 /* XXX This probably needs checks to ensure the flags are
1468 * not abused, and to handle any flag side effects.
1470 if (get_user(flags, (int *) arg))
1473 if (cmd == LL_IOC_SETFLAGS)
1474 fd->fd_flags |= flags;
1476 fd->fd_flags &= ~flags;
1478 case LL_IOC_LOV_SETSTRIPE:
1479 RETURN(ll_lov_setstripe(inode, file, arg));
1480 case LL_IOC_LOV_SETEA:
1481 RETURN(ll_lov_setea(inode, file, arg));
1482 case IOC_MDC_SHOWFID: {
1483 struct lustre_id *idp = (struct lustre_id *)arg;
1484 struct lustre_id id;
1488 filename = getname((const char *)arg);
1489 if (IS_ERR(filename))
1490 RETURN(PTR_ERR(filename));
1492 ll_inode2id(&id, inode);
1494 rc = ll_get_fid(sbi->ll_md_exp, &id, filename, &id);
1496 GOTO(out_filename, rc);
1498 rc = copy_to_user(idp, &id, sizeof(*idp));
1500 GOTO(out_filename, rc = -EFAULT);
1507 case LL_IOC_LOV_GETSTRIPE:
1508 RETURN(ll_lov_getstripe(inode, arg));
1509 case LL_IOC_RECREATE_OBJ:
1510 RETURN(ll_lov_recreate_obj(inode, file, arg));
1511 case EXT3_IOC_GETFLAGS:
1512 case EXT3_IOC_SETFLAGS:
1513 RETURN( ll_iocontrol(inode, file, cmd, arg) );
1514 case LL_IOC_GROUP_LOCK:
1515 RETURN(ll_get_grouplock(inode, file, arg));
1516 case LL_IOC_GROUP_UNLOCK:
1517 RETURN(ll_put_grouplock(inode, file, arg));
1518 case EXT3_IOC_GETVERSION_OLD:
1519 case EXT3_IOC_GETVERSION:
1520 return put_user(inode->i_generation, (int *) arg);
1521 /* We need to special case any other ioctls we want to handle,
1522 * to send them to the MDS/OST as appropriate and to properly
1523 * network encode the arg field.
1524 case EXT2_IOC_GETVERSION_OLD:
1525 case EXT2_IOC_GETVERSION_NEW:
1526 case EXT2_IOC_SETVERSION_OLD:
1527 case EXT2_IOC_SETVERSION_NEW:
1528 case EXT3_IOC_SETVERSION_OLD:
1529 case EXT3_IOC_SETVERSION:
1532 RETURN( obd_iocontrol(cmd, ll_i2dtexp(inode), 0, NULL,
1537 loff_t ll_file_seek(struct file *file, loff_t offset, int origin)
1539 struct inode *inode = file->f_dentry->d_inode;
1540 struct ll_file_data *fd = file->private_data;
1541 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1542 struct lustre_handle lockh = {0};
1545 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p),to=%llu\n", inode->i_ino,
1546 inode->i_generation, inode,
1547 offset + ((origin==2) ? inode->i_size : file->f_pos));
1549 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_LLSEEK);
1550 if (origin == 2) { /* SEEK_END */
1551 ldlm_policy_data_t policy = { .l_extent = {0, OBD_OBJECT_EOF }};
1552 struct ll_inode_info *lli = ll_i2info(inode);
1553 int nonblock = 0, rc;
1555 if (file->f_flags & O_NONBLOCK)
1556 nonblock = LDLM_FL_BLOCK_NOWAIT;
1558 rc = ll_extent_lock(fd, inode, lsm, LCK_PR, &policy, &lockh,
1559 nonblock, &ll_i2sbi(inode)->ll_seek_stime);
1563 down(&lli->lli_size_sem);
1564 offset += inode->i_size;
1565 up(&lli->lli_size_sem);
1566 } else if (origin == 1) { /* SEEK_CUR */
1567 offset += file->f_pos;
1571 if (offset >= 0 && offset <= ll_file_maxbytes(inode)) {
1572 if (offset != file->f_pos) {
1573 file->f_pos = offset;
1574 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1576 file->f_version = ++event;
1583 ll_extent_unlock(fd, inode, lsm, LCK_PR, &lockh);
1587 int ll_fsync(struct file *file, struct dentry *dentry, int data)
1589 struct inode *inode = dentry->d_inode;
1590 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1591 struct lustre_id id;
1592 struct ptlrpc_request *req;
1595 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1596 inode->i_generation, inode);
1598 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_FSYNC);
1600 /* fsync's caller has already called _fdata{sync,write}, we want
1601 * that IO to finish before calling the osc and mdc sync methods */
1602 rc = filemap_fdatawait(inode->i_mapping);
1604 ll_inode2id(&id, inode);
1605 err = md_sync(ll_i2sbi(inode)->ll_md_exp, &id, &req);
1609 ptlrpc_req_finished(req);
1612 struct obdo *oa = obdo_alloc();
1615 RETURN(rc ? rc : -ENOMEM);
1617 oa->o_id = lsm->lsm_object_id;
1618 oa->o_gr = lsm->lsm_object_gr;
1619 oa->o_valid = OBD_MD_FLID;
1620 obdo_from_inode(oa, inode, (OBD_MD_FLTYPE | OBD_MD_FLATIME |
1621 OBD_MD_FLMTIME | OBD_MD_FLCTIME |
1624 err = obd_sync(ll_i2sbi(inode)->ll_dt_exp, oa, lsm,
1634 int ll_file_flock(struct file *file, int cmd, struct file_lock *file_lock)
1636 struct inode *inode = file->f_dentry->d_inode;
1637 struct ll_inode_info *li = ll_i2info(inode);
1638 struct ll_sb_info *sbi = ll_i2sbi(inode);
1639 struct obd_device *obddev;
1640 struct ldlm_res_id res_id =
1641 { .name = {id_fid(&li->lli_id), id_group(&li->lli_id), LDLM_FLOCK} };
1642 struct lustre_handle lockh = {0};
1643 ldlm_policy_data_t flock;
1644 ldlm_mode_t mode = 0;
1649 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu file_lock=%p\n",
1650 inode->i_ino, file_lock);
1652 flock.l_flock.pid = file_lock->fl_pid;
1653 flock.l_flock.start = file_lock->fl_start;
1654 flock.l_flock.end = file_lock->fl_end;
1656 switch (file_lock->fl_type) {
1661 /* An unlock request may or may not have any relation to
1662 * existing locks so we may not be able to pass a lock handle
1663 * via a normal ldlm_lock_cancel() request. The request may even
1664 * unlock a byte range in the middle of an existing lock. In
1665 * order to process an unlock request we need all of the same
1666 * information that is given with a normal read or write record
1667 * lock request. To avoid creating another ldlm unlock (cancel)
1668 * message we'll treat a LCK_NL flock request as an unlock. */
1675 CERROR("unknown fcntl lock type: %d\n", file_lock->fl_type);
1690 flags = LDLM_FL_BLOCK_NOWAIT;
1696 flags = LDLM_FL_TEST_LOCK;
1697 /* Save the old mode so that if the mode in the lock changes we
1698 * can decrement the appropriate reader or writer refcount. */
1699 file_lock->fl_type = mode;
1702 CERROR("unknown fcntl lock command: %d\n", cmd);
1706 CDEBUG(D_DLMTRACE, "inode=%lu, pid="LPU64", flags=%#x, mode=%u, "
1707 "start="LPU64", end="LPU64"\n", inode->i_ino, flock.l_flock.pid,
1708 flags, mode, flock.l_flock.start, flock.l_flock.end);
1710 obddev = md_get_real_obd(sbi->ll_md_exp, &li->lli_id);
1711 rc = ldlm_cli_enqueue(obddev->obd_self_export, NULL,
1712 obddev->obd_namespace,
1713 res_id, LDLM_FLOCK, &flock, mode, &flags,
1714 NULL, ldlm_flock_completion_ast, NULL, file_lock,
1715 NULL, 0, NULL, &lockh);
1719 int ll_inode_revalidate_it(struct dentry *dentry)
1721 struct lookup_intent oit = { .it_op = IT_GETATTR };
1722 struct inode *inode = dentry->d_inode;
1723 struct ptlrpc_request *req = NULL;
1724 struct ll_inode_info *lli;
1725 struct lov_stripe_md *lsm;
1726 struct ll_sb_info *sbi;
1727 struct lustre_id id;
1732 CERROR("REPORT THIS LINE TO PETER\n");
1736 sbi = ll_i2sbi(inode);
1738 ll_inode2id(&id, inode);
1739 lli = ll_i2info(inode);
1740 LASSERT(id_fid(&id) != 0);
1742 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p), name=%s(%p)\n",
1743 inode->i_ino, inode->i_generation, inode, dentry->d_name.name,
1746 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,5,0))
1747 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_REVALIDATE);
1750 rc = ll_intent_alloc(&oit);
1754 rc = md_intent_lock(sbi->ll_md_exp, &id, NULL, 0, NULL, 0, &id,
1755 &oit, 0, &req, ll_mdc_blocking_ast);
1759 rc = revalidate_it_finish(req, 1, &oit, dentry);
1764 ll_lookup_finish_locks(&oit, dentry);
1767 if (lsm == NULL) /* object not yet allocated, don't validate size */
1771 * ll_glimpse_size() will prefer locally cached writes if they extend
1774 rc = ll_glimpse_size(inode);
1777 ll_intent_release(&oit);
1779 ptlrpc_req_finished(req);
1783 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1784 int ll_getattr(struct vfsmount *mnt, struct dentry *de, struct kstat *stat)
1787 struct inode *inode = de->d_inode;
1788 struct ll_inode_info *lli = ll_i2info(inode);
1790 res = ll_inode_revalidate_it(de);
1791 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_GETATTR);
1796 stat->ino = inode->i_ino;
1797 stat->mode = inode->i_mode;
1798 stat->nlink = inode->i_nlink;
1799 stat->uid = inode->i_uid;
1800 stat->gid = inode->i_gid;
1801 stat->atime = inode->i_atime;
1802 stat->mtime = inode->i_mtime;
1803 stat->ctime = inode->i_ctime;
1804 stat->blksize = inode->i_blksize;
1806 down(&lli->lli_size_sem);
1807 stat->size = inode->i_size;
1808 stat->blocks = inode->i_blocks;
1809 up(&lli->lli_size_sem);
1811 stat->rdev = kdev_t_to_nr(inode->i_rdev);
1812 stat->dev = id_group(&ll_i2info(inode)->lli_id);
1818 int ll_setxattr_internal(struct inode *inode, const char *name,
1819 const void *value, size_t size, int flags,
1822 struct ll_sb_info *sbi = ll_i2sbi(inode);
1823 struct ptlrpc_request *request = NULL;
1824 struct mdc_op_data op_data;
1829 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", inode->i_ino);
1830 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_SETXATTR);
1832 memset(&attr, 0x0, sizeof(attr));
1833 attr.ia_valid |= valid;
1834 attr.ia_attr_flags = flags;
1836 ll_prepare_mdc_data(&op_data, inode, NULL, NULL, 0, 0);
1838 rc = md_setattr(sbi->ll_md_exp, &op_data, &attr,
1839 (void*) name, strnlen(name, XATTR_NAME_MAX)+1,
1840 (void*) value, size, &request);
1842 CERROR("md_setattr fails: rc = %d\n", rc);
1847 ptlrpc_req_finished(request);
1851 int ll_setxattr(struct dentry *dentry, const char *name, const void *value,
1852 size_t size, int flags)
1855 struct posix_acl *acl;
1856 struct ll_inode_info *lli;
1859 rc = ll_setxattr_internal(dentry->d_inode, name, value, size,
1862 /* update inode's acl info */
1863 if (rc == 0 && strcmp(name, XATTR_NAME_ACL_ACCESS) == 0) {
1865 acl = posix_acl_from_xattr(value, size);
1867 CERROR("convert from xattr to acl error: %ld",
1871 error = posix_acl_valid(acl);
1873 CERROR("acl valid error: %d", error);
1874 posix_acl_release(acl);
1882 lli = ll_i2info(dentry->d_inode);
1883 spin_lock(&lli->lli_lock);
1884 if (lli->lli_acl_access != NULL)
1885 posix_acl_release(lli->lli_acl_access);
1886 lli->lli_acl_access = acl;
1887 spin_unlock(&lli->lli_lock);
1894 int ll_removexattr(struct dentry *dentry, const char *name)
1896 return ll_setxattr_internal(dentry->d_inode, name, NULL, 0, 0,
1901 int ll_getxattr_internal(struct inode *inode, const char *name, int namelen,
1902 void *value, size_t size, __u64 valid)
1904 struct ptlrpc_request *request = NULL;
1905 struct ll_sb_info *sbi = ll_i2sbi(inode);
1906 struct lustre_id id;
1907 struct mds_body *body;
1912 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_GETXATTR);
1914 ll_inode2id(&id, inode);
1915 rc = md_getattr(sbi->ll_md_exp, &id, valid, name, namelen,
1918 if (rc != -ENODATA && rc != -EOPNOTSUPP)
1919 CERROR("md_getattr fails: rc = %d\n", rc);
1923 body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
1924 LASSERT(body != NULL);
1925 LASSERT_REPSWABBED(request, 0);
1927 ea_size = body->eadatasize;
1928 LASSERT(ea_size <= request->rq_repmsg->buflens[0]);
1931 GOTO(out, rc = ea_size);
1933 ea_data = lustre_msg_buf(request->rq_repmsg, 1, ea_size);
1934 LASSERT(ea_data != NULL);
1935 LASSERT_REPSWABBED(request, 1);
1938 memcpy(value, ea_data, ea_size);
1941 ptlrpc_req_finished(request);
1945 int ll_getxattr(struct dentry *dentry, const char *name, void *value,
1948 return ll_getxattr_internal(dentry->d_inode, name, strlen(name) + 1,
1949 value, size, OBD_MD_FLEA);
1952 int ll_listxattr(struct dentry *dentry, char *list, size_t size)
1954 return ll_getxattr_internal(dentry->d_inode, NULL, 0, list, size,
1959 * XXX We could choose not to check DLM lock. Leave the decision
1960 * to remote acl handling.
1963 lustre_check_acl(struct inode *inode, int mask)
1965 struct lookup_intent it = { .it_op = IT_GETATTR };
1966 struct dentry de = { .d_inode = inode };
1967 struct ll_sb_info *sbi;
1968 struct lustre_id id;
1969 struct ptlrpc_request *req = NULL;
1970 struct ll_inode_info *lli = ll_i2info(inode);
1971 struct posix_acl *acl;
1975 sbi = ll_i2sbi(inode);
1976 ll_inode2id(&id, inode);
1978 if (ll_intent_alloc(&it))
1981 rc = md_intent_lock(sbi->ll_md_exp, &id, NULL, 0, NULL, 0, &id,
1982 &it, 0, &req, ll_mdc_blocking_ast);
1984 ll_intent_free(&it);
1988 rc = revalidate_it_finish(req, 1, &it, &de);
1990 ll_intent_release(&it);
1994 ll_lookup_finish_locks(&it, &de);
1995 ll_intent_free(&it);
1997 spin_lock(&lli->lli_lock);
1998 acl = posix_acl_dup(ll_i2info(inode)->lli_acl_access);
1999 spin_unlock(&lli->lli_lock);
2002 GOTO(out, rc = -EAGAIN);
2004 rc = posix_acl_permission(inode, acl, mask);
2005 posix_acl_release(acl);
2009 ptlrpc_req_finished(req);
2014 int ll_inode_permission(struct inode *inode, int mask, struct nameidata *nd)
2016 return generic_permission(inode, mask, lustre_check_acl);
2019 struct file_operations ll_file_operations = {
2020 .read = ll_file_read,
2021 .write = ll_file_write,
2022 .ioctl = ll_file_ioctl,
2023 .open = ll_file_open,
2024 .release = ll_file_release,
2025 .mmap = ll_file_mmap,
2026 .llseek = ll_file_seek,
2027 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
2028 .sendfile = generic_file_sendfile,
2031 .lock = ll_file_flock
2034 struct inode_operations ll_file_inode_operations = {
2035 .setattr = ll_setattr,
2036 .truncate = ll_truncate,
2037 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2038 .getattr = ll_getattr,
2040 .revalidate_it = ll_inode_revalidate_it,
2042 .setxattr = ll_setxattr,
2043 .getxattr = ll_getxattr,
2044 .listxattr = ll_listxattr,
2045 .removexattr = ll_removexattr,
2046 .permission = ll_inode_permission,