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, 2003 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_dlm.h>
34 #include <linux/lprocfs_status.h>
35 #include "llite_internal.h"
37 kmem_cache_t *ll_file_data_slab;
39 extern struct address_space_operations ll_aops;
40 extern struct address_space_operations ll_dir_aops;
43 #define log2(n) ffz(~(n))
46 struct ll_sb_info *lustre_init_sbi(struct super_block *sb)
48 struct ll_sb_info *sbi = NULL;
52 OBD_ALLOC(sbi, sizeof(*sbi));
56 spin_lock_init(&sbi->ll_lock);
57 INIT_LIST_HEAD(&sbi->ll_pglist);
58 sbi->ll_pglist_gen = 0;
59 sbi->ll_max_read_ahead_pages = SBI_DEFAULT_RA_MAX;
60 INIT_LIST_HEAD(&sbi->ll_conn_chain);
61 INIT_HLIST_HEAD(&sbi->ll_orphan_dentry_list);
62 INIT_LIST_HEAD(&sbi->ll_mnt_list);
63 sema_init(&sbi->ll_gns_sem, 1);
64 init_completion(&sbi->ll_gns_completion);
65 sbi->ll_gns_state = LL_GNS_STATE_IDLE;
66 sbi->ll_gns_timer.data = (unsigned long)sbi;
67 sbi->ll_gns_timer.function = ll_gns_timer_callback;
68 init_timer(&sbi->ll_gns_timer);
69 INIT_LIST_HEAD(&sbi->ll_gns_sbi_head);
73 generate_random_uuid(uuid);
74 class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
78 void lustre_free_sbi(struct super_block *sb)
80 struct ll_sb_info *sbi = ll_s2sbi(sb);
84 list_del(&sbi->ll_gns_sbi_head);
85 del_timer(&sbi->ll_gns_timer);
86 OBD_FREE(sbi, sizeof(*sbi));
92 int lustre_init_ea_size(struct ll_sb_info *sbi)
97 /* OSC may have larger ea size */
98 valsize = sizeof(desc);
99 rc = obd_get_info(sbi->ll_osc_exp, strlen("lovdesc") + 1, "lovdesc",
102 obd_init_ea_size(sbi->ll_mdc_exp,
103 obd_size_diskmd(sbi->ll_osc_exp, NULL),
104 desc.ld_tgt_count*sizeof(struct llog_cookie));
106 /* declare ourself as real client. not connection
108 * FIXME: remove fake valsize, mdsize --bzzz */
109 rc = obd_set_info(sbi->ll_mdc_exp, strlen("client"),
110 "client", valsize, &desc);
115 int lustre_common_fill_super(struct super_block *sb, char *mdc, char *osc)
117 struct inode *root = 0;
118 struct ll_sb_info *sbi = ll_s2sbi(sb);
119 struct obd_device *obd;
120 struct ll_fid rootfid;
121 struct obd_statfs osfs;
122 struct ptlrpc_request *request = NULL;
123 struct lustre_handle osc_conn = {0, };
124 struct lustre_handle mdc_conn = {0, };
129 obd = class_name2obd(mdc);
131 CERROR("MDC %s: not setup or attached\n", mdc);
135 if (proc_lustre_fs_root) {
136 err = lprocfs_register_mountpoint(proc_lustre_fs_root, sb,
139 CERROR("could not register mount in /proc/lustre");
142 err = obd_connect(&mdc_conn, obd, &sbi->ll_sb_uuid);
144 CERROR("An MDS (mdc %s) is performing recovery, of which this"
145 " client is not a part. Please wait for recovery to "
146 "complete, abort, or time out.\n", mdc);
149 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
152 sbi->ll_mdc_exp = class_conn2export(&mdc_conn);
154 err = obd_statfs(obd, &osfs, jiffies - HZ);
158 LASSERT(osfs.os_bsize);
159 sb->s_blocksize = osfs.os_bsize;
160 sb->s_blocksize_bits = log2(osfs.os_bsize);
161 sb->s_magic = LL_SUPER_MAGIC;
162 sb->s_maxbytes = PAGE_CACHE_MAXBYTES;
164 devno = get_uuid2int(sbi->ll_mdc_exp->exp_obd->obd_uuid.uuid,
165 strlen(sbi->ll_mdc_exp->exp_obd->obd_uuid.uuid));
169 obd = class_name2obd(osc);
171 CERROR("OSC %s: not setup or attached\n", osc);
175 err = obd_connect(&osc_conn, obd, &sbi->ll_sb_uuid);
177 CERROR("An OST (osc %s) is performing recovery, of which this"
178 " client is not a part. Please wait for recovery to "
179 "complete, abort, or time out.\n", osc);
182 CERROR("cannot connect to %s: rc = %d\n", osc, err);
185 sbi->ll_osc_exp = class_conn2export(&osc_conn);
187 lustre_init_ea_size(sbi);
189 err = md_getstatus(sbi->ll_mdc_exp, &rootfid);
191 CERROR("cannot mds_connect: rc = %d\n", err);
194 CDEBUG(D_SUPER, "rootfid "LPU64"\n", rootfid.id);
195 sbi->ll_rootino = rootfid.id;
197 sb->s_op = &lustre_super_operations;
200 * XXX: move this to after cbd setup? */
201 err = md_getattr(sbi->ll_mdc_exp, &rootfid,
202 OBD_MD_FLNOTOBD | OBD_MD_FLBLOCKS, 0, &request);
204 CERROR("md_getattr failed for root: rc = %d\n", err);
208 err = mdc_req2lustre_md(sbi->ll_mdc_exp, request, 0,
209 sbi->ll_osc_exp, &md);
211 CERROR("failed to understand root inode md: rc = %d\n",err);
212 ptlrpc_req_finished (request);
216 LASSERT(sbi->ll_rootino != 0);
217 root = ll_iget(sb, sbi->ll_rootino, &md);
219 ptlrpc_req_finished(request);
221 if (root == NULL || is_bad_inode(root)) {
222 /* XXX might need iput() for bad inode */
223 CERROR("lustre_lite: bad iget4 for root\n");
224 GOTO(out_root, err = -EBADF);
227 err = ll_close_thread_start(&sbi->ll_lcq);
229 CERROR("cannot start close thread: rc %d\n", err);
233 ll_gns_add_timer(sbi);
235 /* making vm readahead 0 for 2.4.x. In the case of 2.6.x,
236 backing dev info assigned to inode mapping is used for
237 determining maximal readahead. */
238 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
239 /* bug 2805 - set VM readahead to zero */
240 vm_max_readahead = vm_min_readahead = 0;
243 sb->s_root = d_alloc_root(root);
250 obd_disconnect(sbi->ll_osc_exp, 0);
252 obd_disconnect(sbi->ll_mdc_exp, 0);
254 lprocfs_unregister_mountpoint(sbi);
258 void lustre_common_put_super(struct super_block *sb)
260 struct ll_sb_info *sbi = ll_s2sbi(sb);
261 struct hlist_node *tmp, *next;
264 ll_gns_del_timer(sbi);
266 ll_close_thread_shutdown(sbi->ll_lcq);
268 list_del(&sbi->ll_conn_chain);
269 obd_disconnect(sbi->ll_osc_exp, 0);
271 lprocfs_unregister_mountpoint(sbi);
272 if (sbi->ll_proc_root) {
273 lprocfs_remove(sbi->ll_proc_root);
274 sbi->ll_proc_root = NULL;
277 obd_disconnect(sbi->ll_mdc_exp, 0);
279 // We do this to get rid of orphaned dentries. That is not really trw.
280 spin_lock(&dcache_lock);
281 hlist_for_each_safe(tmp, next, &sbi->ll_orphan_dentry_list) {
282 struct dentry *dentry = hlist_entry(tmp, struct dentry, d_hash);
283 CWARN("orphan dentry %*s (%p) at unmount\n",
284 dentry->d_name.len, dentry->d_name.name, dentry);
285 shrink_dcache_parent(dentry);
287 spin_unlock(&dcache_lock);
292 char *ll_read_opt(const char *opt, char *data)
298 CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
299 if (strncmp(opt, data, strlen(opt)))
301 if ((value = strchr(data, '=')) == NULL)
305 OBD_ALLOC(retval, strlen(value) + 1);
307 CERROR("out of memory!\n");
311 memcpy(retval, value, strlen(value)+1);
312 CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
316 int ll_set_opt(const char *opt, char *data, int fl)
320 CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
321 if (strncmp(opt, data, strlen(opt)))
327 void ll_options(char *options, char **ost, char **mdc, int *flags)
330 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
331 char *opt_ptr = options;
340 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
341 for (this_char = strtok (options, ",");
343 this_char = strtok (NULL, ",")) {
345 while ((this_char = strsep (&opt_ptr, ",")) != NULL) {
347 CDEBUG(D_SUPER, "this_char %s\n", this_char);
348 if (!*ost && (*ost = ll_read_opt("osc", this_char)))
350 if (!*mdc && (*mdc = ll_read_opt("mdc", this_char)))
352 if (!(*flags & LL_SBI_NOLCK) &&
353 ((*flags) = (*flags) |
354 ll_set_opt("nolock", this_char,
361 void ll_lli_init(struct ll_inode_info *lli)
363 sema_init(&lli->lli_open_sem, 1);
365 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
366 spin_lock_init(&lli->lli_lock);
367 INIT_LIST_HEAD(&lli->lli_pending_write_llaps);
370 int ll_fill_super(struct super_block *sb, void *data, int silent)
372 struct ll_sb_info *sbi;
378 CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
380 sbi = lustre_init_sbi(sb);
384 sbi->ll_flags |= LL_SBI_READAHEAD;
385 ll_options(data, &osc, &mdc, &sbi->ll_flags);
389 GOTO(out, err = -EINVAL);
394 GOTO(out, err = -EINVAL);
397 err = lustre_common_fill_super(sb, mdc, osc);
403 OBD_FREE(mdc, strlen(mdc) + 1);
405 OBD_FREE(osc, strlen(osc) + 1);
408 } /* ll_read_super */
410 int lustre_process_log(struct lustre_mount_data *lmd, char * profile,
411 struct config_llog_instance *cfg, int allow_recov)
413 struct lustre_cfg lcfg;
414 struct portals_cfg pcfg;
415 char * peer = "MDS_PEER_UUID";
416 struct obd_device *obd;
417 struct lustre_handle mdc_conn = {0, };
418 struct obd_export *exp;
419 char * name = "mdc_dev";
421 struct obd_uuid mdc_uuid;
422 struct llog_ctxt *ctxt;
427 if (lmd_bad_magic(lmd))
430 generate_random_uuid(uuid);
431 class_uuid_unparse(uuid, &mdc_uuid);
433 if (lmd->lmd_local_nid) {
434 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
435 pcfg.pcfg_nal = lmd->lmd_nal;
436 pcfg.pcfg_nid = lmd->lmd_local_nid;
437 err = libcfs_nal_cmd(&pcfg);
442 if (lmd->lmd_nal == SOCKNAL) {
443 PCFG_INIT(pcfg, NAL_CMD_ADD_AUTOCONN);
444 pcfg.pcfg_nal = lmd->lmd_nal;
445 pcfg.pcfg_nid = lmd->lmd_server_nid;
446 pcfg.pcfg_id = lmd->lmd_server_ipaddr;
447 pcfg.pcfg_misc = lmd->lmd_port;
448 pcfg.pcfg_size = 8388608;
449 pcfg.pcfg_flags = 0x4; /*share*/
450 err = libcfs_nal_cmd(&pcfg);
455 LCFG_INIT(lcfg, LCFG_ADD_UUID, name);
456 lcfg.lcfg_nid = lmd->lmd_server_nid;
457 lcfg.lcfg_inllen1 = strlen(peer) + 1;
458 lcfg.lcfg_inlbuf1 = peer;
459 lcfg.lcfg_nal = lmd->lmd_nal;
460 err = class_process_config(&lcfg);
462 GOTO(out_del_conn, err);
464 LCFG_INIT(lcfg, LCFG_ATTACH, name);
465 lcfg.lcfg_inlbuf1 = "mdc";
466 lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
467 lcfg.lcfg_inlbuf2 = mdc_uuid.uuid;
468 lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
469 err = class_process_config(&lcfg);
471 GOTO(out_del_uuid, err);
473 LCFG_INIT(lcfg, LCFG_SETUP, name);
474 lcfg.lcfg_inlbuf1 = lmd->lmd_mds;
475 lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
476 lcfg.lcfg_inlbuf2 = peer;
477 lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
478 err = class_process_config(&lcfg);
480 GOTO(out_detach, err);
482 obd = class_name2obd(name);
484 GOTO(out_cleanup, err = -EINVAL);
486 /* Disable initial recovery on this import */
487 err = obd_set_info(obd->obd_self_export,
488 strlen("initial_recov"), "initial_recov",
489 sizeof(allow_recov), &allow_recov);
491 GOTO(out_cleanup, err);
493 err = obd_connect(&mdc_conn, obd, &mdc_uuid);
495 CERROR("cannot connect to %s: rc = %d\n", lmd->lmd_mds, err);
496 GOTO(out_cleanup, err);
499 exp = class_conn2export(&mdc_conn);
501 ctxt = llog_get_context(&exp->exp_obd->obd_llogs, LLOG_CONFIG_REPL_CTXT);
503 rc = class_config_parse_llog(ctxt, profile, cfg);
506 * For debugging, it's useful to just dump the log
508 rc = class_config_dump_llog(ctxt, profile, cfg);
511 CERROR("class_config_parse_llog failed: rc = %d\n", rc);
514 err = obd_disconnect(exp, 0);
517 LCFG_INIT(lcfg, LCFG_CLEANUP, name);
518 err = class_process_config(&lcfg);
523 LCFG_INIT(lcfg, LCFG_DETACH, name);
524 err = class_process_config(&lcfg);
529 LCFG_INIT(lcfg, LCFG_DEL_UUID, name);
530 lcfg.lcfg_inllen1 = strlen(peer) + 1;
531 lcfg.lcfg_inlbuf1 = peer;
532 err = class_process_config(&lcfg);
535 if (lmd->lmd_nal == SOCKNAL) {
536 PCFG_INIT(pcfg, NAL_CMD_DEL_AUTOCONN);
537 pcfg.pcfg_nal = lmd->lmd_nal;
538 pcfg.pcfg_nid = lmd->lmd_server_nid;
539 pcfg.pcfg_id = lmd->lmd_server_ipaddr;
540 pcfg.pcfg_flags = 1; /*share*/
541 err = libcfs_nal_cmd(&pcfg);
552 int lustre_fill_super(struct super_block *sb, void *data, int silent)
554 struct lustre_mount_data * lmd = data;
555 struct ll_sb_info *sbi;
561 CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
562 if (lmd_bad_magic(lmd))
565 sbi = lustre_init_sbi(sb);
569 sbi->ll_flags |= LL_SBI_READAHEAD;
571 if (lmd->lmd_profile) {
572 struct lustre_profile *lprof;
573 struct config_llog_instance cfg;
576 if (lmd->lmd_mds[0] == '\0') {
577 CERROR("no mds name\n");
578 GOTO(out_free, err = -EINVAL);
581 OBD_ALLOC(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
582 if (sbi->ll_lmd == NULL)
583 GOTO(out_free, err = -ENOMEM);
584 memcpy(sbi->ll_lmd, lmd, sizeof(*lmd));
586 /* generate a string unique to this super, let's try
587 the address of the super itself.*/
588 len = (sizeof(sb) * 2) + 1;
589 OBD_ALLOC(sbi->ll_instance, len);
590 if (sbi->ll_instance == NULL)
591 GOTO(out_free, err = -ENOMEM);
592 sprintf(sbi->ll_instance, "%p", sb);
594 cfg.cfg_instance = sbi->ll_instance;
595 cfg.cfg_uuid = sbi->ll_sb_uuid;
596 cfg.cfg_local_nid = lmd->lmd_local_nid;
597 err = lustre_process_log(lmd, lmd->lmd_profile, &cfg, 0);
599 CERROR("Unable to process log: %s\n", lmd->lmd_profile);
604 lprof = class_get_profile(lmd->lmd_profile);
606 CERROR("No profile found: %s\n", lmd->lmd_profile);
607 GOTO(out_free, err = -EINVAL);
610 OBD_FREE(osc, strlen(osc) + 1);
611 OBD_ALLOC(osc, strlen(lprof->lp_osc) +
612 strlen(sbi->ll_instance) + 2);
613 sprintf(osc, "%s-%s", lprof->lp_osc, sbi->ll_instance);
616 OBD_FREE(mdc, strlen(mdc) + 1);
617 OBD_ALLOC(mdc, strlen(lprof->lp_mdc) +
618 strlen(sbi->ll_instance) + 2);
619 sprintf(mdc, "%s-%s", lprof->lp_mdc, sbi->ll_instance);
624 GOTO(out_free, err = -EINVAL);
629 GOTO(out_free, err = -EINVAL);
632 err = lustre_common_fill_super(sb, mdc, osc);
639 OBD_FREE(mdc, strlen(mdc) + 1);
641 OBD_FREE(osc, strlen(osc) + 1);
647 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
650 if (sbi->ll_instance != NULL) {
652 struct config_llog_instance cfg;
654 cfg.cfg_instance = sbi->ll_instance;
655 cfg.cfg_uuid = sbi->ll_sb_uuid;
657 OBD_ALLOC(cln_prof, len);
658 sprintf(cln_prof, "%s-clean", sbi->ll_lmd->lmd_profile);
660 err = lustre_process_log(sbi->ll_lmd, cln_prof, &cfg,
663 CERROR("Unable to process log: %s\n", cln_prof);
664 OBD_FREE(cln_prof, len);
665 OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance)+ 1);
667 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
672 } /* lustre_fill_super */
674 static void lustre_manual_cleanup(struct ll_sb_info *sbi)
676 struct lustre_cfg lcfg;
677 struct obd_device *obd;
680 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) != NULL)
684 LCFG_INIT(lcfg, LCFG_CLEANUP, obd->obd_name);
685 err = class_process_config(&lcfg);
687 CERROR("cleanup failed: %s\n", obd->obd_name);
691 LCFG_INIT(lcfg, LCFG_DETACH, obd->obd_name);
692 err = class_process_config(&lcfg);
694 CERROR("detach failed: %s\n", obd->obd_name);
699 if (sbi->ll_lmd != NULL)
700 class_del_profile(sbi->ll_lmd->lmd_profile);
703 void lustre_put_super(struct super_block *sb)
705 struct obd_device *obd;
706 struct ll_sb_info *sbi = ll_s2sbi(sb);
707 int force_umount = 0;
710 CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
711 obd = class_exp2obd(sbi->ll_mdc_exp);
713 force_umount = obd->obd_no_recov;
716 lustre_common_put_super(sb);
718 if (sbi->ll_lmd != NULL) {
720 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
722 struct config_llog_instance cfg;
725 CERROR("force umount, doing manual cleanup\n");
726 lustre_manual_cleanup(sbi);
730 cfg.cfg_instance = sbi->ll_instance;
731 cfg.cfg_uuid = sbi->ll_sb_uuid;
733 OBD_ALLOC(cln_prof, len);
734 sprintf(cln_prof, "%s-clean", sbi->ll_lmd->lmd_profile);
736 err = lustre_process_log(sbi->ll_lmd, cln_prof, &cfg, 0);
738 CERROR("Unable to process log: %s, doing manual cleanup"
740 lustre_manual_cleanup(sbi);
743 OBD_FREE(cln_prof, len);
745 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
746 OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance) + 1);
752 } /* lustre_put_super */
755 struct inode *ll_inode_from_lock(struct ldlm_lock *lock)
758 l_lock(&lock->l_resource->lr_namespace->ns_lock);
759 if (lock->l_ast_data)
760 inode = igrab(lock->l_ast_data);
763 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
767 int null_if_equal(struct ldlm_lock *lock, void *data)
769 if (data == lock->l_ast_data)
770 lock->l_ast_data = NULL;
772 if (lock->l_req_mode != lock->l_granted_mode)
773 return LDLM_ITER_STOP;
775 return LDLM_ITER_CONTINUE;
778 void ll_clear_inode(struct inode *inode)
781 struct ll_inode_info *lli = ll_i2info(inode);
782 struct ll_sb_info *sbi = ll_i2sbi(inode);
785 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
786 inode->i_generation, inode);
788 ll_inode2fid(&fid, inode);
789 clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &(ll_i2info(inode)->lli_flags));
790 md_change_cbdata(sbi->ll_mdc_exp, &fid, null_if_equal, inode);
793 obd_change_cbdata(sbi->ll_osc_exp, lli->lli_smd,
794 null_if_equal, inode);
797 obd_free_memmd(sbi->ll_osc_exp, &lli->lli_smd);
802 /* FIXME: change cbdata for mea here */
803 obd_free_memmd(sbi->ll_mdc_exp,
804 (struct lov_stripe_md **) &lli->lli_mea);
808 if (lli->lli_symlink_name) {
809 OBD_FREE(lli->lli_symlink_name,
810 strlen(lli->lli_symlink_name) + 1);
811 lli->lli_symlink_name = NULL;
817 /* If this inode has objects allocated to it (lsm != NULL), then the OST
818 * object(s) determine the file size and mtime. Otherwise, the MDS will
819 * keep these values until such a time that objects are allocated for it.
820 * We do the MDS operations first, as it is checking permissions for us.
821 * We don't to the MDS RPC if there is nothing that we want to store there,
822 * otherwise there is no harm in updating mtime/atime on the MDS if we are
823 * going to do an RPC anyways.
825 * If we are doing a truncate, we will send the mtime and ctime updates
826 * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
827 * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
830 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
832 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
833 struct ll_sb_info *sbi = ll_i2sbi(inode);
834 struct ptlrpc_request *request = NULL;
835 struct mdc_op_data op_data;
836 int ia_valid = attr->ia_valid;
840 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", inode->i_ino);
841 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_SETATTR);
843 if (ia_valid & ATTR_SIZE) {
844 if (attr->ia_size > ll_file_maxbytes(inode)) {
845 CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
846 attr->ia_size, ll_file_maxbytes(inode));
850 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
853 /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
854 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
855 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
859 /* We mark all of the fields "set" so MDS/OST does not re-set them */
860 if (attr->ia_valid & ATTR_CTIME) {
861 attr->ia_ctime = CURRENT_TIME;
862 attr->ia_valid |= ATTR_CTIME_SET;
864 if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
865 attr->ia_atime = CURRENT_TIME;
866 attr->ia_valid |= ATTR_ATIME_SET;
868 if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
869 attr->ia_mtime = CURRENT_TIME;
870 attr->ia_valid |= ATTR_MTIME_SET;
873 if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
874 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
875 LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
876 LTIME_S(CURRENT_TIME));
878 attr->ia_valid &= ~ATTR_SIZE;
880 /* If only OST attributes being set on objects, don't do MDS RPC.
881 * In that case, we need to check permissions and update the local
882 * inode ourselves so we can call obdo_from_inode() always. */
883 if (ia_valid & (lsm ? ~(ATTR_SIZE | ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
885 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
887 rc = md_setattr(sbi->ll_mdc_exp, &op_data,
888 attr, NULL, 0, NULL, 0, &request);
890 ptlrpc_req_finished(request);
891 if (rc != -EPERM && rc != -EACCES)
892 CERROR("md_setattr fails: rc = %d\n", rc);
896 rc = mdc_req2lustre_md(sbi->ll_mdc_exp, request, 0,
897 sbi->ll_osc_exp, &md);
899 ptlrpc_req_finished(request);
903 /* Won't invoke vmtruncate as we already cleared ATTR_SIZE,
904 * but needed to set timestamps backwards on utime. */
905 inode_setattr(inode, attr);
906 ll_update_inode(inode, &md);
907 ptlrpc_req_finished(request);
909 if (!lsm || !S_ISREG(inode->i_mode)) {
910 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
914 /* The OST doesn't check permissions, but the alternative is
915 * a gratuitous RPC to the MDS. We already rely on the client
916 * to do read/write/truncate permission checks, so is mtime OK?
918 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
919 /* from sys_utime() */
920 if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
921 if (current->fsuid != inode->i_uid &&
922 (rc=ll_permission(inode,MAY_WRITE,NULL))!=0)
925 /* from inode_change_ok() */
926 if (current->fsuid != inode->i_uid &&
927 !capable(CAP_FOWNER))
932 /* Won't invoke vmtruncate, as we already cleared ATTR_SIZE */
933 inode_setattr(inode, attr);
936 /* We really need to get our PW lock before we change inode->i_size.
937 * If we don't we can race with other i_size updaters on our node, like
938 * ll_file_read. We can also race with i_size propogation to other
939 * nodes through dirtying and writeback of final cached pages. This
940 * last one is especially bad for racing o_append users on other
942 if (ia_valid & ATTR_SIZE) {
943 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
945 struct lustre_handle lockh = { 0 };
946 int err, ast_flags = 0;
947 /* XXX when we fix the AST intents to pass the discard-range
948 * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
950 if (attr->ia_size == 0)
951 ast_flags = LDLM_AST_DISCARD_DATA;
953 /* bug 1639: avoid write/truncate i_sem/DLM deadlock */
954 LASSERT(atomic_read(&inode->i_sem.count) <= 0);
956 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW, &policy, &lockh,
962 rc = vmtruncate(inode, attr->ia_size);
964 /* We need to drop the semaphore here, because this unlock may
965 * result in a cancellation, which will need the i_sem */
967 /* unlock now as we don't mind others file lockers racing with
968 * the mds updates below? */
969 err = ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
972 CERROR("ll_extent_unlock failed: %d\n", err);
976 } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
979 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
980 inode->i_ino, LTIME_S(attr->ia_mtime));
981 oa.o_id = lsm->lsm_object_id;
982 oa.o_gr = lsm->lsm_object_gr;
983 oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
984 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
985 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
986 rc = obd_setattr(sbi->ll_osc_exp, &oa, lsm, NULL);
988 CERROR("obd_setattr fails: rc=%d\n", rc);
993 int ll_setattr(struct dentry *de, struct iattr *attr)
995 LBUG(); /* code is unused, but leave this in case of VFS changes */
999 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
1000 unsigned long max_age)
1002 struct ll_sb_info *sbi = ll_s2sbi(sb);
1003 struct obd_statfs obd_osfs;
1007 rc = obd_statfs(class_exp2obd(sbi->ll_mdc_exp), osfs, max_age);
1009 CERROR("mdc_statfs fails: rc = %d\n", rc);
1013 osfs->os_type = sb->s_magic;
1015 CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1016 osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1018 rc = obd_statfs(class_exp2obd(sbi->ll_osc_exp), &obd_osfs, max_age);
1020 CERROR("obd_statfs fails: rc = %d\n", rc);
1024 CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1025 obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1028 osfs->os_blocks = obd_osfs.os_blocks;
1029 osfs->os_bfree = obd_osfs.os_bfree;
1030 osfs->os_bavail = obd_osfs.os_bavail;
1032 /* If we don't have as many objects free on the OST as inodes
1033 * on the MDS, we reduce the total number of inodes to
1034 * compensate, so that the "inodes in use" number is correct.
1036 if (obd_osfs.os_ffree < osfs->os_ffree) {
1037 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1039 osfs->os_ffree = obd_osfs.os_ffree;
1045 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
1047 struct obd_statfs osfs;
1050 CDEBUG(D_VFSTRACE, "VFS Op:\n");
1051 lprocfs_counter_incr(ll_s2sbi(sb)->ll_stats, LPROC_LL_STAFS);
1053 /* For now we will always get up-to-date statfs values, but in the
1054 * future we may allow some amount of caching on the client (e.g.
1055 * from QOS or lprocfs updates). */
1056 rc = ll_statfs_internal(sb, &osfs, jiffies - 1);
1060 statfs_unpack(sfs, &osfs);
1062 if (sizeof(sfs->f_blocks) == 4) {
1063 while (osfs.os_blocks > ~0UL) {
1066 osfs.os_blocks >>= 1;
1067 osfs.os_bfree >>= 1;
1068 osfs.os_bavail >>= 1;
1072 sfs->f_blocks = osfs.os_blocks;
1073 sfs->f_bfree = osfs.os_bfree;
1074 sfs->f_bavail = osfs.os_bavail;
1079 void dump_lsm(int level, struct lov_stripe_md *lsm)
1081 CDEBUG(level, "objid "LPX64", maxbytes "LPX64", magic 0x%08X, "
1082 "stripe_size %u, stripe_count %u\n",
1083 lsm->lsm_object_id, lsm->lsm_maxbytes, lsm->lsm_magic,
1084 lsm->lsm_stripe_size, lsm->lsm_stripe_count);
1087 void ll_update_inode(struct inode *inode, struct lustre_md *md)
1089 struct ll_inode_info *lli = ll_i2info(inode);
1090 struct mds_body *body = md->body;
1091 struct lov_stripe_md *lsm = md->lsm;
1092 struct mea *mea = md->mea;
1095 LASSERT((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
1096 LASSERT((mea != NULL) == ((body->valid & OBD_MD_FLDIREA) != 0));
1098 LASSERT(lsm->lsm_object_gr > 0);
1099 if (lli->lli_smd == NULL) {
1101 lli->lli_maxbytes = lsm->lsm_maxbytes;
1102 if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1103 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1105 if (memcmp(lli->lli_smd, lsm, sizeof(*lsm))) {
1106 CERROR("lsm mismatch for inode %ld\n",
1108 CERROR("lli_smd:\n");
1109 dump_lsm(D_ERROR, lli->lli_smd);
1111 dump_lsm(D_ERROR, lsm);
1115 /* bug 2844 - limit i_blksize for broken user-space apps */
1116 LASSERTF(lsm->lsm_xfersize != 0, "%lu\n", lsm->lsm_xfersize);
1117 inode->i_blksize = min(lsm->lsm_xfersize, LL_MAX_BLKSIZE);
1118 if (lli->lli_smd != lsm)
1119 obd_free_memmd(ll_i2obdexp(inode), &lsm);
1123 if (lli->lli_mea == NULL) {
1126 if (memcmp(lli->lli_mea, mea, body->eadatasize)) {
1127 CERROR("mea mismatch for inode %lu\n",
1132 if (lli->lli_mea != mea)
1133 obd_free_memmd(ll_i2mdcexp(inode),
1134 (struct lov_stripe_md **) &mea);
1138 if (body->valid & OBD_MD_FLID)
1139 inode->i_ino = body->ino;
1140 if (body->valid & OBD_MD_FLATIME)
1141 LTIME_S(inode->i_atime) = body->atime;
1142 if (body->valid & OBD_MD_FLMTIME &&
1143 body->mtime > LTIME_S(inode->i_mtime)) {
1144 CDEBUG(D_INODE, "setting ino %lu mtime from %lu to %u\n",
1145 inode->i_ino, LTIME_S(inode->i_mtime), body->mtime);
1146 LTIME_S(inode->i_mtime) = body->mtime;
1148 if (body->valid & OBD_MD_FLCTIME &&
1149 body->ctime > LTIME_S(inode->i_ctime))
1150 LTIME_S(inode->i_ctime) = body->ctime;
1151 if (body->valid & OBD_MD_FLMODE)
1152 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
1153 if (body->valid & OBD_MD_FLTYPE)
1154 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
1155 if (body->valid & OBD_MD_FLUID)
1156 inode->i_uid = body->uid;
1157 if (body->valid & OBD_MD_FLGID)
1158 inode->i_gid = body->gid;
1159 if (body->valid & OBD_MD_FLFLAGS)
1160 inode->i_flags = body->flags;
1161 if (body->valid & OBD_MD_FLNLINK)
1162 inode->i_nlink = body->nlink;
1163 if (body->valid & OBD_MD_FLGENER)
1164 inode->i_generation = body->generation;
1165 if (body->valid & OBD_MD_FLRDEV)
1166 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1167 inode->i_rdev = body->rdev;
1169 inode->i_rdev = old_decode_dev(body->rdev);
1171 if (body->valid & OBD_MD_FLSIZE)
1172 inode->i_size = body->size;
1173 if (body->valid & OBD_MD_FLBLOCKS)
1174 inode->i_blocks = body->blocks;
1176 if (body->valid & OBD_MD_FLSIZE)
1177 set_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
1179 lli->lli_mds = body->mds;
1180 inode->i_dev = (kdev_t) body->mds;
1181 LASSERT(body->mds < 1000);
1184 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
1185 static struct backing_dev_info ll_backing_dev_info = {
1186 .ra_pages = 0, /* No readahead */
1187 .memory_backed = 0, /* Does contribute to dirty memory */
1191 void ll_read_inode2(struct inode *inode, void *opaque)
1193 struct lustre_md *md = opaque;
1194 struct ll_inode_info *lli = ll_i2info(inode);
1197 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1198 inode->i_generation, inode);
1202 LASSERT(!lli->lli_smd);
1204 /* Core attributes from the MDS first. This is a new inode, and
1205 * the VFS doesn't zero times in the core inode so we have to do
1206 * it ourselves. They will be overwritten by either MDS or OST
1207 * attributes - we just need to make sure they aren't newer. */
1208 LTIME_S(inode->i_mtime) = 0;
1209 LTIME_S(inode->i_atime) = 0;
1210 LTIME_S(inode->i_ctime) = 0;
1213 ll_update_inode(inode, md);
1215 /* OIDEBUG(inode); */
1217 if (S_ISREG(inode->i_mode)) {
1218 inode->i_op = &ll_file_inode_operations;
1219 inode->i_fop = &ll_file_operations;
1220 inode->i_mapping->a_ops = &ll_aops;
1222 } else if (S_ISDIR(inode->i_mode)) {
1223 inode->i_op = &ll_dir_inode_operations;
1224 inode->i_fop = &ll_dir_operations;
1225 inode->i_mapping->a_ops = &ll_dir_aops;
1227 } else if (S_ISLNK(inode->i_mode)) {
1228 inode->i_op = &ll_fast_symlink_inode_operations;
1231 inode->i_op = &ll_special_inode_operations;
1233 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1234 init_special_inode(inode, inode->i_mode,
1235 kdev_t_to_nr(inode->i_rdev));
1237 /* initializing backing dev info. */
1238 inode->i_mapping->backing_dev_info = &ll_backing_dev_info;
1240 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1242 lli->ll_save_ifop = inode->i_fop;
1244 if (S_ISCHR(inode->i_mode))
1245 inode->i_fop = &ll_special_chr_inode_fops;
1246 else if (S_ISBLK(inode->i_mode))
1247 inode->i_fop = &ll_special_blk_inode_fops;
1248 else if (S_ISFIFO(inode->i_mode))
1249 inode->i_fop = &ll_special_fifo_inode_fops;
1250 else if (S_ISSOCK(inode->i_mode))
1251 inode->i_fop = &ll_special_sock_inode_fops;
1253 CWARN("saved %p, replaced with %p\n", lli->ll_save_ifop,
1256 if (lli->ll_save_ifop->owner) {
1257 CWARN("%p has owner %p\n", lli->ll_save_ifop,
1258 lli->ll_save_ifop->owner);
1264 int ll_iocontrol(struct inode *inode, struct file *file,
1265 unsigned int cmd, unsigned long arg)
1267 struct ll_sb_info *sbi = ll_i2sbi(inode);
1268 struct ptlrpc_request *req = NULL;
1273 case EXT3_IOC_GETFLAGS: {
1275 unsigned long valid = OBD_MD_FLFLAGS;
1276 struct mds_body *body;
1278 ll_inode2fid(&fid, inode);
1279 rc = md_getattr(sbi->ll_mdc_exp, &fid, valid, 0, &req);
1281 CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1285 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1287 if (body->flags & S_APPEND)
1288 flags |= EXT3_APPEND_FL;
1289 if (body->flags & S_IMMUTABLE)
1290 flags |= EXT3_IMMUTABLE_FL;
1291 if (body->flags & S_NOATIME)
1292 flags |= EXT3_NOATIME_FL;
1294 ptlrpc_req_finished (req);
1296 RETURN(put_user(flags, (int *)arg));
1298 case EXT3_IOC_SETFLAGS: {
1299 struct mdc_op_data op_data;
1302 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1304 if (get_user(flags, (int *)arg))
1311 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
1313 memset(&attr, 0x0, sizeof(attr));
1314 attr.ia_attr_flags = flags;
1315 attr.ia_valid |= ATTR_ATTR_FLAG;
1317 rc = md_setattr(sbi->ll_mdc_exp, &op_data,
1318 &attr, NULL, 0, NULL, 0, &req);
1320 ptlrpc_req_finished(req);
1321 if (rc != -EPERM && rc != -EACCES)
1322 CERROR("md_setattr fails: rc = %d\n", rc);
1326 ptlrpc_req_finished(req);
1328 oa->o_id = lsm->lsm_object_id;
1329 oa->o_gr = lsm->lsm_object_gr;
1330 oa->o_flags = flags;
1331 oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
1333 rc = obd_setattr(sbi->ll_osc_exp, oa, lsm, NULL);
1336 if (rc != -EPERM && rc != -EACCES)
1337 CERROR("md_setattr fails: rc = %d\n", rc);
1341 if (flags & EXT3_APPEND_FL)
1342 inode->i_flags |= S_APPEND;
1344 inode->i_flags &= ~S_APPEND;
1345 if (flags & EXT3_IMMUTABLE_FL)
1346 inode->i_flags |= S_IMMUTABLE;
1348 inode->i_flags &= ~S_IMMUTABLE;
1349 if (flags & EXT3_NOATIME_FL)
1350 inode->i_flags |= S_NOATIME;
1352 inode->i_flags &= ~S_NOATIME;
1363 void ll_umount_begin(struct super_block *sb)
1365 struct ll_sb_info *sbi = ll_s2sbi(sb);
1366 struct obd_device *obd;
1367 struct obd_ioctl_data ioc_data = { 0 };
1369 CDEBUG(D_VFSTRACE, "VFS Op:\n");
1371 obd = class_exp2obd(sbi->ll_mdc_exp);
1373 CERROR("Invalid MDC connection handle "LPX64"\n",
1374 sbi->ll_mdc_exp->exp_handle.h_cookie);
1378 obd->obd_no_recov = 1;
1379 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_mdc_exp, sizeof ioc_data,
1382 obd = class_exp2obd(sbi->ll_osc_exp);
1384 CERROR("Invalid LOV connection handle "LPX64"\n",
1385 sbi->ll_osc_exp->exp_handle.h_cookie);
1390 obd->obd_no_recov = 1;
1391 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_osc_exp, sizeof ioc_data,
1394 /* Really, we'd like to wait until there are no requests outstanding,
1395 * and then continue. For now, we just invalidate the requests,
1396 * schedule, and hope.
1403 int ll_prep_inode(struct obd_export *osc_exp, struct obd_export *mdc_exp,
1404 struct inode **inode, struct ptlrpc_request *req,
1405 int offset, struct super_block *sb)
1407 struct lustre_md md;
1410 rc = mdc_req2lustre_md(mdc_exp, req, offset, osc_exp, &md);
1415 ll_update_inode(*inode, &md);
1418 *inode = ll_iget(sb, md.body->ino, &md);
1419 if (*inode == NULL || is_bad_inode(*inode)) {
1420 /* free the lsm if we allocated one above */
1422 obd_free_memmd(osc_exp, &md.lsm);
1424 obd_free_memmd(mdc_exp,
1425 (struct lov_stripe_md**)&md.mea);
1427 CERROR("new_inode -fatal: rc %d\n", rc);