1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Lustre Light Super operations
6 * Copyright (c) 2002-2005 Cluster File Systems, Inc.
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define DEBUG_SUBSYSTEM S_LLITE
26 #include <linux/module.h>
27 #include <linux/types.h>
28 #include <linux/random.h>
29 #include <linux/version.h>
31 #include <linux/lustre_lite.h>
32 #include <linux/lustre_ha.h>
33 #include <linux/lustre_ver.h>
34 #include <linux/lustre_dlm.h>
35 #include <linux/lprocfs_status.h>
36 #include <linux/lustre_disk.h>
37 #include "llite_internal.h"
39 kmem_cache_t *ll_file_data_slab;
41 LIST_HEAD(ll_super_blocks);
42 spinlock_t ll_sb_lock = SPIN_LOCK_UNLOCKED;
44 extern struct address_space_operations ll_aops;
45 extern struct address_space_operations ll_dir_aops;
48 #define log2(n) ffz(~(n))
51 /* We need to have some extra twiddling here because some systems have
52 * no random state when they start up. */
54 lustre_generate_random_uuid(class_uuid_t uuid)
60 LASSERT(sizeof(class_uuid_t) % sizeof(*i) == 0);
66 generate_random_uuid(uuid);
68 for (i = (int *)uuid; (char *)i < (char *)uuid + sizeof(class_uuid_t); i++) {
70 j = ((j << 8) & 0xffffff00) | ((j >> 24) & 0x000000ff);
71 k = ((k >> 8) & 0x00ffffff) | ((k << 24) & 0xff000000);
77 struct ll_sb_info *ll_init_sbi(void)
79 struct ll_sb_info *sbi = NULL;
83 OBD_ALLOC(sbi, sizeof(*sbi));
87 spin_lock_init(&sbi->ll_lock);
88 spin_lock_init(&sbi->ll_lco.lco_lock);
89 INIT_LIST_HEAD(&sbi->ll_pglist);
90 sbi->ll_pglist_gen = 0;
91 if (num_physpages >> (20 - PAGE_SHIFT) < 512)
92 sbi->ll_async_page_max = num_physpages / 2;
94 sbi->ll_async_page_max = (num_physpages / 4) * 3;
95 sbi->ll_ra_info.ra_max_pages = min(num_physpages / 8,
96 SBI_DEFAULT_READAHEAD_MAX);
98 INIT_LIST_HEAD(&sbi->ll_conn_chain);
99 INIT_HLIST_HEAD(&sbi->ll_orphan_dentry_list);
101 lustre_generate_random_uuid(uuid);
102 class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
103 CDEBUG(D_HA, "generated uuid: %s\n", sbi->ll_sb_uuid.uuid);
105 spin_lock(&ll_sb_lock);
106 list_add_tail(&sbi->ll_list, &ll_super_blocks);
107 spin_unlock(&ll_sb_lock);
111 void ll_free_sbi(struct super_block *sb)
113 struct ll_sb_info *sbi = ll_s2sbi(sb);
117 spin_lock(&ll_sb_lock);
118 list_del(&sbi->ll_list);
119 spin_unlock(&ll_sb_lock);
120 OBD_FREE(sbi, sizeof(*sbi));
125 int client_common_fill_super(struct super_block *sb, char *mdc, char *osc)
127 struct inode *root = 0;
128 struct ll_sb_info *sbi = ll_s2sbi(sb);
129 struct obd_device *obd;
130 struct ll_fid rootfid;
131 struct obd_statfs osfs;
132 struct ptlrpc_request *request = NULL;
133 struct lustre_handle osc_conn = {0, };
134 struct lustre_handle mdc_conn = {0, };
136 struct obd_connect_data *data = NULL;
140 obd = class_name2obd(mdc);
142 CERROR("MDC %s: not setup or attached\n", mdc);
146 OBD_ALLOC(data, sizeof(*data));
150 if (proc_lustre_fs_root) {
151 err = lprocfs_register_mountpoint(proc_lustre_fs_root, sb,
154 CERROR("could not register mount in /proc/lustre");
157 if (sb->s_flags & MS_RDONLY)
158 data->ocd_connect_flags |= OBD_CONNECT_RDONLY;
160 if (sbi->ll_flags & LL_SBI_FLOCK) {
161 sbi->ll_fop = &ll_file_operations_flock;
163 sbi->ll_fop = &ll_file_operations;
166 data->ocd_connect_flags |= OBD_CONNECT_VERSION;
167 data->ocd_version = LUSTRE_VERSION_CODE;
168 err = obd_connect(&mdc_conn, obd, &sbi->ll_sb_uuid, data);
170 CERROR("An MDT (mdc %s) is performing recovery, of which this"
171 " client is not a part. Please wait for recovery to "
172 "complete, abort, or time out.\n", mdc);
175 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
178 sbi->ll_mdc_exp = class_conn2export(&mdc_conn);
180 err = obd_statfs(obd, &osfs, jiffies - HZ);
184 LASSERT(osfs.os_bsize);
185 sb->s_blocksize = osfs.os_bsize;
186 sb->s_blocksize_bits = log2(osfs.os_bsize);
187 sb->s_magic = LL_SUPER_MAGIC;
188 sb->s_maxbytes = PAGE_CACHE_MAXBYTES;
189 sbi->ll_namelen = osfs.os_namelen;
191 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
192 /* We set sb->s_dev equal on all lustre clients in order to support
193 * NFS export clustering. NFSD requires that the FSID be the same
195 /* s_dev is also used in lt_compare() to compare two fs, but that is
196 * only a node-local comparison. */
197 sb->s_dev = get_uuid2int(sbi2mdc(sbi)->cl_import->imp_target_uuid.uuid,
198 strlen(sbi2mdc(sbi)->cl_import->imp_target_uuid.uuid));
201 obd = class_name2obd(osc);
203 CERROR("OSC %s: not setup or attached\n", osc);
207 data->ocd_connect_flags =
208 OBD_CONNECT_GRANT|OBD_CONNECT_VERSION|OBD_CONNECT_REQPORTAL;
210 CDEBUG(D_RPCTRACE, "ocd_connect_flags: "LPX64" ocd_version: %d "
211 "ocd_grant: %d\n", data->ocd_connect_flags,
212 data->ocd_version, data->ocd_grant);
214 obd->obd_upcall.onu_owner = &sbi->ll_lco;
215 obd->obd_upcall.onu_upcall = ll_ocd_update;
217 err = obd_connect(&osc_conn, obd, &sbi->ll_sb_uuid, data);
219 CERROR("An OST (osc %s) is performing recovery, of which this"
220 " client is not a part. Please wait for recovery to "
221 "complete, abort, or time out.\n", osc);
224 CERROR("cannot connect to %s: rc = %d\n", osc, err);
227 sbi->ll_osc_exp = class_conn2export(&osc_conn);
228 spin_lock(&sbi->ll_lco.lco_lock);
229 sbi->ll_lco.lco_flags = data->ocd_connect_flags;
230 spin_unlock(&sbi->ll_lco.lco_lock);
232 mdc_init_ea_size(sbi->ll_mdc_exp, sbi->ll_osc_exp);
234 if (!ll_async_page_slab) {
235 ll_async_page_slab_size =
236 size_round(sizeof(struct ll_async_page)) +
237 obd_prep_async_page(sbi->ll_osc_exp, NULL, NULL, NULL,
238 0, NULL, NULL, NULL);
239 ll_async_page_slab = kmem_cache_create("ll_async_page",
240 ll_async_page_slab_size,
242 if (!ll_async_page_slab)
243 GOTO(out_osc, -ENOMEM);
246 err = mdc_getstatus(sbi->ll_mdc_exp, &rootfid);
248 CERROR("cannot mds_connect: rc = %d\n", err);
251 CDEBUG(D_SUPER, "rootfid "LPU64"\n", rootfid.id);
252 sbi->ll_rootino = rootfid.id;
254 sb->s_op = &lustre_super_operations;
257 * XXX: move this to after cbd setup? */
258 err = mdc_getattr(sbi->ll_mdc_exp, &rootfid,
259 OBD_MD_FLGETATTR | OBD_MD_FLBLOCKS, 0, &request);
261 CERROR("mdc_getattr failed for root: rc = %d\n", err);
265 err = mdc_req2lustre_md(request, 0, sbi->ll_osc_exp, &md);
267 CERROR("failed to understand root inode md: rc = %d\n",err);
268 ptlrpc_req_finished (request);
272 LASSERT(sbi->ll_rootino != 0);
273 root = ll_iget(sb, sbi->ll_rootino, &md);
275 ptlrpc_req_finished(request);
277 if (root == NULL || is_bad_inode(root)) {
279 obd_free_memmd(sbi->ll_osc_exp, &md.lsm);
280 CERROR("lustre_lite: bad iget4 for root\n");
281 GOTO(out_root, err = -EBADF);
284 err = ll_close_thread_start(&sbi->ll_lcq);
286 CERROR("cannot start close thread: rc %d\n", err);
290 /* making vm readahead 0 for 2.4.x. In the case of 2.6.x,
291 backing dev info assigned to inode mapping is used for
292 determining maximal readahead. */
293 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)) && \
294 !defined(KERNEL_HAS_AS_MAX_READAHEAD)
295 /* bug 2805 - set VM readahead to zero */
296 vm_max_readahead = vm_min_readahead = 0;
299 sb->s_root = d_alloc_root(root);
301 OBD_FREE(data, sizeof(*data));
308 obd_disconnect(sbi->ll_osc_exp);
310 obd_disconnect(sbi->ll_mdc_exp);
313 OBD_FREE(data, sizeof(*data));
314 lprocfs_unregister_mountpoint(sbi);
318 void ll_dump_inode(struct inode *inode)
320 struct list_head *tmp;
321 int dentry_count = 0;
323 LASSERT(inode != NULL);
325 list_for_each(tmp, &inode->i_dentry)
328 CERROR("inode %p dump: dev=%s ino=%lu mode=%o count=%u, %d dentries\n",
329 inode, ll_i2mdcexp(inode)->exp_obd->obd_name, inode->i_ino,
330 inode->i_mode, atomic_read(&inode->i_count), dentry_count);
333 void lustre_dump_dentry(struct dentry *dentry, int recur)
335 struct list_head *tmp;
338 LASSERT(dentry != NULL);
340 list_for_each(tmp, &dentry->d_subdirs)
343 CERROR("dentry %p dump: name=%.*s parent=%.*s (%p), inode=%p, count=%u,"
344 " flags=0x%x, fsdata=%p, %d subdirs\n", dentry,
345 dentry->d_name.len, dentry->d_name.name,
346 dentry->d_parent->d_name.len, dentry->d_parent->d_name.name,
347 dentry->d_parent, dentry->d_inode, atomic_read(&dentry->d_count),
348 dentry->d_flags, dentry->d_fsdata, subdirs);
349 if (dentry->d_inode != NULL)
350 ll_dump_inode(dentry->d_inode);
355 list_for_each(tmp, &dentry->d_subdirs) {
356 struct dentry *d = list_entry(tmp, struct dentry, d_child);
357 lustre_dump_dentry(d, recur - 1);
361 void client_common_put_super(struct super_block *sb)
363 struct ll_sb_info *sbi = ll_s2sbi(sb);
364 struct hlist_node *tmp, *next;
367 ll_close_thread_shutdown(sbi->ll_lcq);
369 list_del(&sbi->ll_conn_chain);
370 obd_disconnect(sbi->ll_osc_exp);
372 lprocfs_unregister_mountpoint(sbi);
373 if (sbi->ll_proc_root) {
374 lprocfs_remove(sbi->ll_proc_root);
375 sbi->ll_proc_root = NULL;
378 obd_disconnect(sbi->ll_mdc_exp);
380 // We do this to get rid of orphaned dentries. That is not really trw.
381 hlist_for_each_safe(tmp, next, &sbi->ll_orphan_dentry_list) {
382 struct dentry *dentry = hlist_entry(tmp, struct dentry, d_hash);
383 CWARN("found orphan dentry %.*s (%p->%p) at unmount, dumping "
384 "before and after shrink_dcache_parent\n",
385 dentry->d_name.len, dentry->d_name.name, dentry, next);
386 lustre_dump_dentry(dentry, 1);
387 shrink_dcache_parent(dentry);
388 lustre_dump_dentry(dentry, 1);
393 char *ll_read_opt(const char *opt, char *data)
399 CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
400 if (strncmp(opt, data, strlen(opt)))
402 if ((value = strchr(data, '=')) == NULL)
406 OBD_ALLOC(retval, strlen(value) + 1);
408 CERROR("out of memory!\n");
412 memcpy(retval, value, strlen(value)+1);
413 CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
417 int ll_set_opt(const char *opt, char *data, int fl)
421 CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
422 if (strncmp(opt, data, strlen(opt)))
428 void ll_options(char *options, int *flags)
432 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
433 char *opt_ptr = options;
442 CERROR("Parsing opts %s\n", options);
443 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
444 for (this_char = strtok (options, ",");
446 this_char = strtok (NULL, ","))
448 while ((this_char = strsep (&opt_ptr, ",")) != NULL)
451 CDEBUG(D_SUPER, "this_char %s\n", this_char);
452 tmp = ll_set_opt("nolock", this_char, LL_SBI_NOLCK);
457 tmp = ll_set_opt("flock", this_char, LL_SBI_FLOCK);
462 tmp = ll_set_opt("noflock", this_char, LL_SBI_FLOCK);
467 tmp = ll_set_opt("user_xattr", this_char, LL_SBI_USER_XATTR);
472 tmp = ll_set_opt("nouser_xattr", this_char, LL_SBI_USER_XATTR);
478 CERROR("flags %#x\n", *flags);
483 void ll_lli_init(struct ll_inode_info *lli)
485 sema_init(&lli->lli_open_sem, 1);
486 sema_init(&lli->lli_size_sem, 1);
488 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
489 spin_lock_init(&lli->lli_lock);
490 INIT_LIST_HEAD(&lli->lli_pending_write_llaps);
491 lli->lli_inode_magic = LLI_INODE_MAGIC;
494 int ll_fill_super(struct super_block *sb)
496 struct lustre_profile *lprof;
497 struct lustre_sb_info *lsi = s2lsi(sb);
498 struct ll_sb_info *sbi;
501 char *profilenm = get_profile_name(sb);
502 struct config_llog_instance cfg;
503 char ll_instance[sizeof(sb) * 2 + 1];
507 CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
509 /* client additional sb info */
510 lsi->lsi_llsbi = sbi = ll_init_sbi();
514 ll_options(lsi->lsi_lmd->lmd_opts, &sbi->ll_flags);
516 /* Generate a string unique to this super, in case some joker tries
517 to mount the same fs at two mount points.
518 Use the address of the super itself.*/
519 sprintf(ll_instance, "%p", sb);
520 cfg.cfg_instance = ll_instance;
521 cfg.cfg_uuid = lsi->lsi_llsbi->ll_sb_uuid;
523 /* set up client obds */
524 err = lustre_get_process_log(sb, profilenm, &cfg);
526 CERROR("Unable to process log: %d\n", err);
530 lprof = class_get_profile(profilenm);
532 CERROR("No profile found: %s\n", profilenm);
533 GOTO(out_free, err = -EINVAL);
535 CERROR("Found profile %s: mdc=%s osc=%s\n", profilenm,
536 lprof->lp_mdc, lprof->lp_osc);
538 OBD_ALLOC(osc, strlen(lprof->lp_osc) +
539 strlen(ll_instance) + 2);
541 GOTO(out_free, err = -ENOMEM);
542 sprintf(osc, "%s-%s", lprof->lp_osc, ll_instance);
544 OBD_ALLOC(mdc, strlen(lprof->lp_mdc) +
545 strlen(ll_instance) + 2);
547 GOTO(out_free, err = -ENOMEM);
548 sprintf(mdc, "%s-%s", lprof->lp_mdc, ll_instance);
550 /* connections, registrations, sb setup */
551 err = client_common_fill_super(sb, mdc, osc);
555 OBD_FREE(mdc, strlen(mdc) + 1);
557 OBD_FREE(osc, strlen(osc) + 1);
559 struct obd_device *obd;
561 /* like client_put_super below */
562 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next))
564 class_manual_cleanup(obd);
566 class_del_profile(profilenm);
568 lsi->lsi_llsbi = NULL;
571 } /* ll_fill_super */
574 void ll_put_super(struct super_block *sb)
576 struct obd_device *obd;
577 struct lustre_sb_info *lsi = s2lsi(sb);
578 struct ll_sb_info *sbi = ll_s2sbi(sb);
579 char *profilenm = get_profile_name(sb);
583 CDEBUG(D_VFSTRACE, "VFS Op: sb %p - %s\n", sb, profilenm);
584 obd = class_exp2obd(sbi->ll_mdc_exp);
587 int force = obd->obd_no_recov;
588 /* We need to set force before the lov_disconnect in
589 lustre_common_put_super, since l_d cleans up osc's as well. */
590 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next))
592 obd->obd_force = force;
596 client_common_put_super(sb);
598 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) !=NULL) {
599 class_manual_cleanup(obd);
603 class_del_profile(profilenm);
606 lsi->lsi_llsbi = NULL;
608 lustre_common_put_super(sb);
611 } /* client_put_super */
613 #ifdef HAVE_REGISTER_CACHE
614 #include <linux/cache_def.h>
615 #ifdef HAVE_CACHE_RETURN_INT
620 ll_shrink_cache(int priority, unsigned int gfp_mask)
622 struct ll_sb_info *sbi;
625 list_for_each_entry(sbi, &ll_super_blocks, ll_list)
626 count += llap_shrink_cache(sbi, priority);
628 #ifdef HAVE_CACHE_RETURN_INT
633 struct cache_definition ll_cache_definition = {
634 .name = "llap_cache",
635 .shrink = ll_shrink_cache
637 #endif /* HAVE_REGISTER_CACHE */
639 struct inode *ll_inode_from_lock(struct ldlm_lock *lock)
641 struct inode *inode = NULL;
642 l_lock(&lock->l_resource->lr_namespace->ns_lock);
643 if (lock->l_ast_data) {
644 struct ll_inode_info *lli = ll_i2info(lock->l_ast_data);
645 if (lli->lli_inode_magic == LLI_INODE_MAGIC) {
646 inode = igrab(lock->l_ast_data);
648 inode = lock->l_ast_data;
649 __LDLM_DEBUG(inode->i_state & I_FREEING ?
650 D_INFO : D_WARNING, lock,
651 "l_ast_data %p is bogus: magic %08x",
652 lock->l_ast_data, lli->lli_inode_magic);
656 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
660 static int null_if_equal(struct ldlm_lock *lock, void *data)
662 if (data == lock->l_ast_data) {
663 lock->l_ast_data = NULL;
665 if (lock->l_req_mode != lock->l_granted_mode)
666 LDLM_ERROR(lock,"clearing inode with ungranted lock");
669 return LDLM_ITER_CONTINUE;
672 void ll_clear_inode(struct inode *inode)
675 struct ll_inode_info *lli = ll_i2info(inode);
676 struct ll_sb_info *sbi = ll_i2sbi(inode);
679 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
680 inode->i_generation, inode);
682 ll_inode2fid(&fid, inode);
683 clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &(ll_i2info(inode)->lli_flags));
684 mdc_change_cbdata(sbi->ll_mdc_exp, &fid, null_if_equal, inode);
687 obd_change_cbdata(sbi->ll_osc_exp, lli->lli_smd,
688 null_if_equal, inode);
690 obd_free_memmd(sbi->ll_osc_exp, &lli->lli_smd);
694 if (lli->lli_symlink_name) {
695 OBD_FREE(lli->lli_symlink_name,
696 strlen(lli->lli_symlink_name) + 1);
697 lli->lli_symlink_name = NULL;
699 lli->lli_inode_magic = LLI_INODE_DEAD;
704 /* If this inode has objects allocated to it (lsm != NULL), then the OST
705 * object(s) determine the file size and mtime. Otherwise, the MDS will
706 * keep these values until such a time that objects are allocated for it.
707 * We do the MDS operations first, as it is checking permissions for us.
708 * We don't to the MDS RPC if there is nothing that we want to store there,
709 * otherwise there is no harm in updating mtime/atime on the MDS if we are
710 * going to do an RPC anyways.
712 * If we are doing a truncate, we will send the mtime and ctime updates
713 * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
714 * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
717 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
719 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
720 struct ll_sb_info *sbi = ll_i2sbi(inode);
721 struct ptlrpc_request *request = NULL;
722 struct mdc_op_data op_data;
723 int ia_valid = attr->ia_valid;
727 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu valid %x\n", inode->i_ino,
729 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_SETATTR);
731 if (ia_valid & ATTR_SIZE) {
732 if (attr->ia_size > ll_file_maxbytes(inode)) {
733 CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
734 attr->ia_size, ll_file_maxbytes(inode));
738 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
741 /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
742 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
743 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
747 /* We mark all of the fields "set" so MDS/OST does not re-set them */
748 if (attr->ia_valid & ATTR_CTIME) {
749 attr->ia_ctime = CURRENT_TIME;
750 attr->ia_valid |= ATTR_CTIME_SET;
752 if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
753 attr->ia_atime = CURRENT_TIME;
754 attr->ia_valid |= ATTR_ATIME_SET;
756 if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
757 attr->ia_mtime = CURRENT_TIME;
758 attr->ia_valid |= ATTR_MTIME_SET;
761 if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
762 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
763 LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
767 /* NB: ATTR_SIZE will only be set after this point if the size
768 * resides on the MDS, ie, this file has no objects. */
770 attr->ia_valid &= ~ATTR_SIZE;
772 /* If only OST attributes being set on objects, don't do MDS RPC.
773 * In that case, we need to check permissions and update the local
774 * inode ourselves so we can call obdo_from_inode() always. */
775 if (ia_valid & (lsm ? ~(ATTR_SIZE | ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
777 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
779 rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
780 attr, NULL, 0, NULL, 0, &request);
783 ptlrpc_req_finished(request);
784 if (rc != -EPERM && rc != -EACCES)
785 CERROR("mdc_setattr fails: rc = %d\n", rc);
789 rc = mdc_req2lustre_md(request, 0, sbi->ll_osc_exp, &md);
791 ptlrpc_req_finished(request);
795 /* We call inode_setattr to adjust timestamps.
796 * If there is at least some data in file, we cleared ATTR_SIZE
797 * above to avoid invoking vmtruncate, otherwise it is important
798 * to call vmtruncate in inode_setattr to update inode->i_size
800 inode_setattr(inode, attr);
802 ll_update_inode(inode, md.body, md.lsm);
803 ptlrpc_req_finished(request);
805 if (!lsm || !S_ISREG(inode->i_mode)) {
806 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
810 /* The OST doesn't check permissions, but the alternative is
811 * a gratuitous RPC to the MDS. We already rely on the client
812 * to do read/write/truncate permission checks, so is mtime OK?
814 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
815 /* from sys_utime() */
816 if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
817 if (current->fsuid != inode->i_uid &&
818 (rc=ll_permission(inode,MAY_WRITE,NULL))!=0)
821 /* from inode_change_ok() */
822 if (current->fsuid != inode->i_uid &&
823 !capable(CAP_FOWNER))
828 /* Won't invoke vmtruncate, as we already cleared ATTR_SIZE */
829 inode_setattr(inode, attr);
832 /* We really need to get our PW lock before we change inode->i_size.
833 * If we don't we can race with other i_size updaters on our node, like
834 * ll_file_read. We can also race with i_size propogation to other
835 * nodes through dirtying and writeback of final cached pages. This
836 * last one is especially bad for racing o_append users on other
838 if (ia_valid & ATTR_SIZE) {
839 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
841 struct lustre_handle lockh = { 0 };
842 struct ll_inode_info *lli = ll_i2info(inode);
843 int err, ast_flags = 0;
844 /* XXX when we fix the AST intents to pass the discard-range
845 * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
847 if (attr->ia_size == 0)
848 ast_flags = LDLM_AST_DISCARD_DATA;
851 UP_WRITE_I_ALLOC_SEM(inode);
852 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW, &policy, &lockh,
854 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
855 DOWN_WRITE_I_ALLOC_SEM(inode);
859 DOWN_WRITE_I_ALLOC_SEM(inode);
864 /* Only ll_inode_size_lock is taken at this level.
865 * lov_stripe_lock() is grabbed by ll_truncate() only over
866 * call to obd_adjust_kms(). If vmtruncate returns 0, then
867 * ll_truncate dropped ll_inode_size_lock() */
868 ll_inode_size_lock(inode, 0);
869 rc = vmtruncate(inode, attr->ia_size);
871 LASSERT(atomic_read(&lli->lli_size_sem.count) <= 0);
872 ll_inode_size_unlock(inode, 0);
875 err = ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
877 CERROR("ll_extent_unlock failed: %d\n", err);
881 } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
884 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
885 inode->i_ino, LTIME_S(attr->ia_mtime));
886 oa.o_id = lsm->lsm_object_id;
887 oa.o_valid = OBD_MD_FLID;
888 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
889 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
890 rc = obd_setattr(sbi->ll_osc_exp, &oa, lsm, NULL);
892 CERROR("obd_setattr fails: rc=%d\n", rc);
897 int ll_setattr(struct dentry *de, struct iattr *attr)
899 LBUG(); /* code is unused, but leave this in case of VFS changes */
903 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
904 unsigned long max_age)
906 struct ll_sb_info *sbi = ll_s2sbi(sb);
907 struct obd_statfs obd_osfs;
911 rc = obd_statfs(class_exp2obd(sbi->ll_mdc_exp), osfs, max_age);
913 CERROR("mdc_statfs fails: rc = %d\n", rc);
917 osfs->os_type = sb->s_magic;
919 CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
920 osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
922 rc = obd_statfs(class_exp2obd(sbi->ll_osc_exp), &obd_osfs, max_age);
924 CERROR("obd_statfs fails: rc = %d\n", rc);
928 CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
929 obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
932 osfs->os_blocks = obd_osfs.os_blocks;
933 osfs->os_bfree = obd_osfs.os_bfree;
934 osfs->os_bavail = obd_osfs.os_bavail;
936 /* If we don't have as many objects free on the OST as inodes
937 * on the MDS, we reduce the total number of inodes to
938 * compensate, so that the "inodes in use" number is correct.
940 if (obd_osfs.os_ffree < osfs->os_ffree) {
941 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
943 osfs->os_ffree = obd_osfs.os_ffree;
949 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
951 struct obd_statfs osfs;
954 CDEBUG(D_VFSTRACE, "VFS Op:\n");
955 lprocfs_counter_incr(ll_s2sbi(sb)->ll_stats, LPROC_LL_STAFS);
957 /* For now we will always get up-to-date statfs values, but in the
958 * future we may allow some amount of caching on the client (e.g.
959 * from QOS or lprocfs updates). */
960 rc = ll_statfs_internal(sb, &osfs, jiffies - 1);
964 statfs_unpack(sfs, &osfs);
966 if (sizeof(sfs->f_blocks) == 4) {
967 while (osfs.os_blocks > ~0UL) {
970 osfs.os_blocks >>= 1;
972 osfs.os_bavail >>= 1;
976 sfs->f_blocks = osfs.os_blocks;
977 sfs->f_bfree = osfs.os_bfree;
978 sfs->f_bavail = osfs.os_bavail;
983 void ll_inode_size_lock(struct inode *inode, int lock_lsm)
985 struct ll_inode_info *lli;
986 struct lov_stripe_md *lsm;
988 lli = ll_i2info(inode);
989 LASSERT(lli->lli_size_sem_owner != current);
990 down(&lli->lli_size_sem);
991 LASSERT(lli->lli_size_sem_owner == NULL);
992 lli->lli_size_sem_owner = current;
994 LASSERTF(lsm != NULL || lock_lsm == 0, "lsm %p, lock_lsm %d\n",
997 lov_stripe_lock(lsm);
1000 void ll_inode_size_unlock(struct inode *inode, int unlock_lsm)
1002 struct ll_inode_info *lli;
1003 struct lov_stripe_md *lsm;
1005 lli = ll_i2info(inode);
1007 LASSERTF(lsm != NULL || unlock_lsm == 0, "lsm %p, lock_lsm %d\n",
1010 lov_stripe_unlock(lsm);
1011 LASSERT(lli->lli_size_sem_owner == current);
1012 lli->lli_size_sem_owner = NULL;
1013 up(&lli->lli_size_sem);
1016 void ll_update_inode(struct inode *inode, struct mds_body *body,
1017 struct lov_stripe_md *lsm)
1019 struct ll_inode_info *lli = ll_i2info(inode);
1021 LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
1023 if (lli->lli_smd == NULL) {
1024 if (lsm->lsm_magic != LOV_MAGIC) {
1025 dump_lsm(D_ERROR, lsm);
1028 CDEBUG(D_INODE, "adding lsm %p to inode %lu/%u(%p)\n",
1029 lsm, inode->i_ino, inode->i_generation, inode);
1030 /* ll_inode_size_lock() requires it is only called
1031 * with lli_smd != NULL or lock_lsm == 0 or we can
1032 * race between lock/unlock. bug 9547 */
1034 lli->lli_maxbytes = lsm->lsm_maxbytes;
1035 if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1036 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1038 if (lov_stripe_md_cmp(lli->lli_smd, lsm)) {
1039 CERROR("lsm mismatch for inode %ld\n",
1041 CERROR("lli_smd:\n");
1042 dump_lsm(D_ERROR, lli->lli_smd);
1044 dump_lsm(D_ERROR, lsm);
1048 /* bug 2844 - limit i_blksize for broken user-space apps */
1049 LASSERTF(lsm->lsm_xfersize != 0, "%lu\n", lsm->lsm_xfersize);
1050 inode->i_blksize = min(lsm->lsm_xfersize, LL_MAX_BLKSIZE);
1051 if (lli->lli_smd != lsm)
1052 obd_free_memmd(ll_i2obdexp(inode), &lsm);
1054 inode->i_blksize = max(inode->i_blksize,
1055 inode->i_sb->s_blocksize);
1058 if (body->valid & OBD_MD_FLID)
1059 inode->i_ino = body->ino;
1060 if (body->valid & OBD_MD_FLATIME)
1061 LTIME_S(inode->i_atime) = body->atime;
1062 if (body->valid & OBD_MD_FLMTIME &&
1063 body->mtime > LTIME_S(inode->i_mtime)) {
1064 CDEBUG(D_INODE, "setting ino %lu mtime from %lu to "LPU64"\n",
1065 inode->i_ino, LTIME_S(inode->i_mtime), body->mtime);
1066 LTIME_S(inode->i_mtime) = body->mtime;
1068 if (body->valid & OBD_MD_FLCTIME &&
1069 body->ctime > LTIME_S(inode->i_ctime))
1070 LTIME_S(inode->i_ctime) = body->ctime;
1071 if (body->valid & OBD_MD_FLMODE)
1072 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
1073 if (body->valid & OBD_MD_FLTYPE)
1074 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
1075 if (body->valid & OBD_MD_FLUID)
1076 inode->i_uid = body->uid;
1077 if (body->valid & OBD_MD_FLGID)
1078 inode->i_gid = body->gid;
1079 if (body->valid & OBD_MD_FLFLAGS)
1080 inode->i_flags = body->flags;
1081 if (body->valid & OBD_MD_FLNLINK)
1082 inode->i_nlink = body->nlink;
1083 if (body->valid & OBD_MD_FLGENER)
1084 inode->i_generation = body->generation;
1085 if (body->valid & OBD_MD_FLRDEV)
1086 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1087 inode->i_rdev = body->rdev;
1089 inode->i_rdev = old_decode_dev(body->rdev);
1091 if (body->valid & OBD_MD_FLSIZE)
1092 inode->i_size = body->size;
1093 if (body->valid & OBD_MD_FLBLOCKS)
1094 inode->i_blocks = body->blocks;
1096 if (body->valid & OBD_MD_FLSIZE)
1097 set_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
1100 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
1101 static struct backing_dev_info ll_backing_dev_info = {
1102 .ra_pages = 0, /* No readahead */
1103 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,12))
1104 .capabilities = 0, /* Does contribute to dirty memory */
1106 .memory_backed = 0, /* Does contribute to dirty memory */
1111 void ll_read_inode2(struct inode *inode, void *opaque)
1113 struct lustre_md *md = opaque;
1114 struct ll_inode_info *lli = ll_i2info(inode);
1117 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1118 inode->i_generation, inode);
1122 LASSERT(!lli->lli_smd);
1124 /* Core attributes from the MDS first. This is a new inode, and
1125 * the VFS doesn't zero times in the core inode so we have to do
1126 * it ourselves. They will be overwritten by either MDS or OST
1127 * attributes - we just need to make sure they aren't newer. */
1128 LTIME_S(inode->i_mtime) = 0;
1129 LTIME_S(inode->i_atime) = 0;
1130 LTIME_S(inode->i_ctime) = 0;
1132 ll_update_inode(inode, md->body, md->lsm);
1134 /* OIDEBUG(inode); */
1136 if (S_ISREG(inode->i_mode)) {
1137 struct ll_sb_info *sbi = ll_i2sbi(inode);
1138 inode->i_op = &ll_file_inode_operations;
1139 inode->i_fop = sbi->ll_fop;
1140 inode->i_mapping->a_ops = &ll_aops;
1142 } else if (S_ISDIR(inode->i_mode)) {
1143 inode->i_op = &ll_dir_inode_operations;
1144 inode->i_fop = &ll_dir_operations;
1145 inode->i_mapping->a_ops = &ll_dir_aops;
1147 } else if (S_ISLNK(inode->i_mode)) {
1148 inode->i_op = &ll_fast_symlink_inode_operations;
1151 inode->i_op = &ll_special_inode_operations;
1153 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1154 init_special_inode(inode, inode->i_mode,
1155 kdev_t_to_nr(inode->i_rdev));
1157 /* initializing backing dev info. */
1158 inode->i_mapping->backing_dev_info = &ll_backing_dev_info;
1160 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1162 lli->ll_save_ifop = inode->i_fop;
1164 if (S_ISCHR(inode->i_mode))
1165 inode->i_fop = &ll_special_chr_inode_fops;
1166 else if (S_ISBLK(inode->i_mode))
1167 inode->i_fop = &ll_special_blk_inode_fops;
1168 else if (S_ISFIFO(inode->i_mode))
1169 inode->i_fop = &ll_special_fifo_inode_fops;
1170 else if (S_ISSOCK(inode->i_mode))
1171 inode->i_fop = &ll_special_sock_inode_fops;
1176 int ll_iocontrol(struct inode *inode, struct file *file,
1177 unsigned int cmd, unsigned long arg)
1179 struct ll_sb_info *sbi = ll_i2sbi(inode);
1180 struct ptlrpc_request *req = NULL;
1185 case EXT3_IOC_GETFLAGS: {
1187 struct mds_body *body;
1189 ll_inode2fid(&fid, inode);
1190 rc = mdc_getattr(sbi->ll_mdc_exp, &fid, OBD_MD_FLFLAGS,0,&req);
1192 CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1196 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1198 if (body->flags & S_APPEND)
1199 flags |= EXT3_APPEND_FL;
1200 if (body->flags & S_IMMUTABLE)
1201 flags |= EXT3_IMMUTABLE_FL;
1202 if (body->flags & S_NOATIME)
1203 flags |= EXT3_NOATIME_FL;
1205 ptlrpc_req_finished (req);
1207 RETURN(put_user(flags, (int *)arg));
1209 case EXT3_IOC_SETFLAGS: {
1210 struct mdc_op_data op_data;
1213 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1215 if (get_user(flags, (int *)arg))
1222 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
1224 memset(&attr, 0x0, sizeof(attr));
1225 attr.ia_attr_flags = flags;
1226 attr.ia_valid |= ATTR_ATTR_FLAG;
1228 rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
1229 &attr, NULL, 0, NULL, 0, &req);
1231 ptlrpc_req_finished(req);
1232 if (rc != -EPERM && rc != -EACCES)
1233 CERROR("mdc_setattr fails: rc = %d\n", rc);
1237 ptlrpc_req_finished(req);
1239 oa->o_id = lsm->lsm_object_id;
1240 oa->o_flags = flags;
1241 oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS;
1243 rc = obd_setattr(sbi->ll_osc_exp, oa, lsm, NULL);
1246 if (rc != -EPERM && rc != -EACCES)
1247 CERROR("mdc_setattr fails: rc = %d\n", rc);
1251 if (flags & EXT3_APPEND_FL)
1252 inode->i_flags |= S_APPEND;
1254 inode->i_flags &= ~S_APPEND;
1255 if (flags & EXT3_IMMUTABLE_FL)
1256 inode->i_flags |= S_IMMUTABLE;
1258 inode->i_flags &= ~S_IMMUTABLE;
1259 if (flags & EXT3_NOATIME_FL)
1260 inode->i_flags |= S_NOATIME;
1262 inode->i_flags &= ~S_NOATIME;
1273 void ll_umount_begin(struct super_block *sb)
1275 struct ll_sb_info *sbi = ll_s2sbi(sb);
1276 struct obd_device *obd;
1277 struct obd_ioctl_data ioc_data = { 0 };
1279 CDEBUG(D_VFSTRACE, "VFS Op: superblock %p count %d active %d\n", sb,
1280 sb->s_count, atomic_read(&sb->s_active));
1282 obd = class_exp2obd(sbi->ll_mdc_exp);
1284 CERROR("Invalid MDC connection handle "LPX64"\n",
1285 sbi->ll_mdc_exp->exp_handle.h_cookie);
1289 obd->obd_no_recov = 1;
1290 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_mdc_exp, sizeof ioc_data,
1293 obd = class_exp2obd(sbi->ll_osc_exp);
1295 CERROR("Invalid LOV connection handle "LPX64"\n",
1296 sbi->ll_osc_exp->exp_handle.h_cookie);
1301 obd->obd_no_recov = 1;
1302 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_osc_exp, sizeof ioc_data,
1305 /* Really, we'd like to wait until there are no requests outstanding,
1306 * and then continue. For now, we just invalidate the requests,
1307 * schedule, and hope.
1314 int ll_remount_fs(struct super_block *sb, int *flags, char *data)
1316 struct ll_sb_info *sbi = ll_s2sbi(sb);
1320 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
1321 read_only = *flags & MS_RDONLY;
1322 err = obd_set_info(sbi->ll_mdc_exp, strlen("read-only"),
1323 "read-only", sizeof(read_only), &read_only);
1325 CERROR("Failed to change the read-only flag during "
1326 "remount: %d\n", err);
1331 sb->s_flags |= MS_RDONLY;
1333 sb->s_flags &= ~MS_RDONLY;
1338 int ll_prep_inode(struct obd_export *exp, struct inode **inode,
1339 struct ptlrpc_request *req, int offset,struct super_block *sb)
1341 struct lustre_md md;
1344 rc = mdc_req2lustre_md(req, offset, exp, &md);
1349 ll_update_inode(*inode, md.body, md.lsm);
1352 *inode = ll_iget(sb, md.body->ino, &md);
1353 if (*inode == NULL || is_bad_inode(*inode)) {
1354 /* free the lsm if we allocated one above */
1356 obd_free_memmd(exp, &md.lsm);
1358 CERROR("new_inode -fatal: rc %d\n", rc);
1365 char *llap_origins[] = {
1366 [LLAP_ORIGIN_UNKNOWN] = "--",
1367 [LLAP_ORIGIN_READPAGE] = "rp",
1368 [LLAP_ORIGIN_READAHEAD] = "ra",
1369 [LLAP_ORIGIN_COMMIT_WRITE] = "cw",
1370 [LLAP_ORIGIN_WRITEPAGE] = "wp",
1373 struct ll_async_page *llite_pglist_next_llap(struct ll_sb_info *sbi,
1374 struct list_head *list)
1376 struct ll_async_page *llap;
1377 struct list_head *pos;
1379 list_for_each(pos, list) {
1380 if (pos == &sbi->ll_pglist)
1382 llap = list_entry(pos, struct ll_async_page, llap_pglist_item);
1383 if (llap->llap_page == NULL)
1391 EXPORT_SYMBOL(ll_fill_super);
1392 EXPORT_SYMBOL(ll_put_super);
1393 EXPORT_SYMBOL(ll_remount_fs);
1394 EXPORT_SYMBOL(ll_umount_begin);