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);
64 generate_random_uuid(uuid);
65 class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
69 void lustre_free_sbi(struct super_block *sb)
71 struct ll_sb_info *sbi = ll_s2sbi(sb);
75 OBD_FREE(sbi, sizeof(*sbi));
80 int lustre_init_ea_size(struct ll_sb_info *sbi)
85 /* OSC may have larger ea size */
86 valsize = sizeof(desc);
87 rc = obd_get_info(sbi->ll_osc_exp, strlen("lovdesc") + 1, "lovdesc",
90 obd_init_ea_size(sbi->ll_mdc_exp,
91 obd_size_diskmd(sbi->ll_osc_exp, NULL),
92 desc.ld_tgt_count*sizeof(struct llog_cookie));
94 /* declare ourself as real client. not connection
96 * FIXME: remove fake valsize, mdsize --bzzz */
97 rc = obd_set_info(sbi->ll_mdc_exp, strlen("client"),
98 "client", valsize, &desc);
103 int lustre_common_fill_super(struct super_block *sb, char *mdc, char *osc)
105 struct inode *root = 0;
106 struct ll_sb_info *sbi = ll_s2sbi(sb);
107 struct obd_device *obd;
108 struct ll_fid rootfid;
109 struct obd_statfs osfs;
110 struct ptlrpc_request *request = NULL;
111 struct lustre_handle osc_conn = {0, };
112 struct lustre_handle mdc_conn = {0, };
117 obd = class_name2obd(mdc);
119 CERROR("MDC %s: not setup or attached\n", mdc);
123 if (proc_lustre_fs_root) {
124 err = lprocfs_register_mountpoint(proc_lustre_fs_root, sb,
127 CERROR("could not register mount in /proc/lustre");
130 err = obd_connect(&mdc_conn, obd, &sbi->ll_sb_uuid);
132 CERROR("An MDS (mdc %s) is performing recovery, of which this"
133 " client is not a part. Please wait for recovery to "
134 "complete, abort, or time out.\n", mdc);
137 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
140 sbi->ll_mdc_exp = class_conn2export(&mdc_conn);
142 err = obd_statfs(obd, &osfs, jiffies - HZ);
146 LASSERT(osfs.os_bsize);
147 sb->s_blocksize = osfs.os_bsize;
148 sb->s_blocksize_bits = log2(osfs.os_bsize);
149 sb->s_magic = LL_SUPER_MAGIC;
150 sb->s_maxbytes = PAGE_CACHE_MAXBYTES;
152 devno = get_uuid2int(sbi->ll_mdc_exp->exp_obd->obd_uuid.uuid,
153 strlen(sbi->ll_mdc_exp->exp_obd->obd_uuid.uuid));
157 obd = class_name2obd(osc);
159 CERROR("OSC %s: not setup or attached\n", osc);
163 err = obd_connect(&osc_conn, obd, &sbi->ll_sb_uuid);
165 CERROR("An OST (osc %s) is performing recovery, of which this"
166 " client is not a part. Please wait for recovery to "
167 "complete, abort, or time out.\n", osc);
170 CERROR("cannot connect to %s: rc = %d\n", osc, err);
173 sbi->ll_osc_exp = class_conn2export(&osc_conn);
175 lustre_init_ea_size(sbi);
177 err = md_getstatus(sbi->ll_mdc_exp, &rootfid);
179 CERROR("cannot mds_connect: rc = %d\n", err);
182 CDEBUG(D_SUPER, "rootfid "LPU64"\n", rootfid.id);
183 sbi->ll_rootino = rootfid.id;
185 sb->s_op = &lustre_super_operations;
188 * XXX: move this to after cbd setup? */
189 err = md_getattr(sbi->ll_mdc_exp, &rootfid,
190 OBD_MD_FLNOTOBD | OBD_MD_FLBLOCKS, 0, &request);
192 CERROR("md_getattr failed for root: rc = %d\n", err);
196 err = mdc_req2lustre_md(sbi->ll_mdc_exp, request, 0,
197 sbi->ll_osc_exp, &md);
199 CERROR("failed to understand root inode md: rc = %d\n",err);
200 ptlrpc_req_finished (request);
204 LASSERT(sbi->ll_rootino != 0);
205 root = ll_iget(sb, sbi->ll_rootino, &md);
207 ptlrpc_req_finished(request);
209 if (root == NULL || is_bad_inode(root)) {
210 /* XXX might need iput() for bad inode */
211 CERROR("lustre_lite: bad iget4 for root\n");
212 GOTO(out_root, err = -EBADF);
215 err = ll_close_thread_start(&sbi->ll_lcq);
217 CERROR("cannot start close thread: rc %d\n", err);
221 /* making vm readahead 0 for 2.4.x. In the case of 2.6.x,
222 backing dev info assigned to inode mapping is used for
223 determining maximal readahead. */
224 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
225 /* bug 2805 - set VM readahead to zero */
226 vm_max_readahead = vm_min_readahead = 0;
229 sb->s_root = d_alloc_root(root);
236 obd_disconnect(sbi->ll_osc_exp, 0);
238 obd_disconnect(sbi->ll_mdc_exp, 0);
240 lprocfs_unregister_mountpoint(sbi);
244 void lustre_common_put_super(struct super_block *sb)
246 struct ll_sb_info *sbi = ll_s2sbi(sb);
247 struct hlist_node *tmp, *next;
250 ll_close_thread_shutdown(sbi->ll_lcq);
252 list_del(&sbi->ll_conn_chain);
253 obd_disconnect(sbi->ll_osc_exp, 0);
255 lprocfs_unregister_mountpoint(sbi);
256 if (sbi->ll_proc_root) {
257 lprocfs_remove(sbi->ll_proc_root);
258 sbi->ll_proc_root = NULL;
261 obd_disconnect(sbi->ll_mdc_exp, 0);
263 // We do this to get rid of orphaned dentries. That is not really trw.
264 spin_lock(&dcache_lock);
265 hlist_for_each_safe(tmp, next, &sbi->ll_orphan_dentry_list) {
266 struct dentry *dentry = hlist_entry(tmp, struct dentry, d_hash);
267 CWARN("orphan dentry %*s (%p) at unmount\n",
268 dentry->d_name.len, dentry->d_name.name, dentry);
269 shrink_dcache_parent(dentry);
271 spin_unlock(&dcache_lock);
276 char *ll_read_opt(const char *opt, char *data)
282 CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
283 if (strncmp(opt, data, strlen(opt)))
285 if ((value = strchr(data, '=')) == NULL)
289 OBD_ALLOC(retval, strlen(value) + 1);
291 CERROR("out of memory!\n");
295 memcpy(retval, value, strlen(value)+1);
296 CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
300 int ll_set_opt(const char *opt, char *data, int fl)
304 CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
305 if (strncmp(opt, data, strlen(opt)))
311 void ll_options(char *options, char **ost, char **mdc, int *flags)
314 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
315 char *opt_ptr = options;
324 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
325 for (this_char = strtok (options, ",");
327 this_char = strtok (NULL, ",")) {
329 while ((this_char = strsep (&opt_ptr, ",")) != NULL) {
331 CDEBUG(D_SUPER, "this_char %s\n", this_char);
332 if (!*ost && (*ost = ll_read_opt("osc", this_char)))
334 if (!*mdc && (*mdc = ll_read_opt("mdc", this_char)))
336 if (!(*flags & LL_SBI_NOLCK) &&
337 ((*flags) = (*flags) |
338 ll_set_opt("nolock", this_char,
345 void ll_lli_init(struct ll_inode_info *lli)
347 sema_init(&lli->lli_open_sem, 1);
349 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
350 spin_lock_init(&lli->lli_lock);
351 INIT_LIST_HEAD(&lli->lli_pending_write_llaps);
354 int ll_fill_super(struct super_block *sb, void *data, int silent)
356 struct ll_sb_info *sbi;
362 CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
364 sbi = lustre_init_sbi(sb);
368 sbi->ll_flags |= LL_SBI_READAHEAD;
369 ll_options(data, &osc, &mdc, &sbi->ll_flags);
373 GOTO(out, err = -EINVAL);
378 GOTO(out, err = -EINVAL);
381 err = lustre_common_fill_super(sb, mdc, osc);
387 OBD_FREE(mdc, strlen(mdc) + 1);
389 OBD_FREE(osc, strlen(osc) + 1);
392 } /* ll_read_super */
394 int lustre_process_log(struct lustre_mount_data *lmd, char * profile,
395 struct config_llog_instance *cfg, int allow_recov)
397 struct lustre_cfg lcfg;
398 struct portals_cfg pcfg;
399 char * peer = "MDS_PEER_UUID";
400 struct obd_device *obd;
401 struct lustre_handle mdc_conn = {0, };
402 struct obd_export *exp;
403 char * name = "mdc_dev";
405 struct obd_uuid mdc_uuid;
406 struct llog_ctxt *ctxt;
411 if (lmd_bad_magic(lmd))
414 generate_random_uuid(uuid);
415 class_uuid_unparse(uuid, &mdc_uuid);
417 if (lmd->lmd_local_nid) {
418 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
419 pcfg.pcfg_nal = lmd->lmd_nal;
420 pcfg.pcfg_nid = lmd->lmd_local_nid;
421 err = libcfs_nal_cmd(&pcfg);
426 if (lmd->lmd_nal == SOCKNAL) {
427 PCFG_INIT(pcfg, NAL_CMD_ADD_AUTOCONN);
428 pcfg.pcfg_nal = lmd->lmd_nal;
429 pcfg.pcfg_nid = lmd->lmd_server_nid;
430 pcfg.pcfg_id = lmd->lmd_server_ipaddr;
431 pcfg.pcfg_misc = lmd->lmd_port;
432 pcfg.pcfg_size = 8388608;
433 pcfg.pcfg_flags = 0x4; /*share*/
434 err = libcfs_nal_cmd(&pcfg);
439 LCFG_INIT(lcfg, LCFG_ADD_UUID, name);
440 lcfg.lcfg_nid = lmd->lmd_server_nid;
441 lcfg.lcfg_inllen1 = strlen(peer) + 1;
442 lcfg.lcfg_inlbuf1 = peer;
443 lcfg.lcfg_nal = lmd->lmd_nal;
444 err = class_process_config(&lcfg);
446 GOTO(out_del_conn, err);
448 LCFG_INIT(lcfg, LCFG_ATTACH, name);
449 lcfg.lcfg_inlbuf1 = "mdc";
450 lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
451 lcfg.lcfg_inlbuf2 = mdc_uuid.uuid;
452 lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
453 err = class_process_config(&lcfg);
455 GOTO(out_del_uuid, err);
457 LCFG_INIT(lcfg, LCFG_SETUP, name);
458 lcfg.lcfg_inlbuf1 = lmd->lmd_mds;
459 lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
460 lcfg.lcfg_inlbuf2 = peer;
461 lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
462 err = class_process_config(&lcfg);
464 GOTO(out_detach, err);
466 obd = class_name2obd(name);
468 GOTO(out_cleanup, err = -EINVAL);
470 /* Disable initial recovery on this import */
471 err = obd_set_info(obd->obd_self_export,
472 strlen("initial_recov"), "initial_recov",
473 sizeof(allow_recov), &allow_recov);
475 GOTO(out_cleanup, err);
477 err = obd_connect(&mdc_conn, obd, &mdc_uuid);
479 CERROR("cannot connect to %s: rc = %d\n", lmd->lmd_mds, err);
480 GOTO(out_cleanup, err);
483 exp = class_conn2export(&mdc_conn);
485 ctxt = llog_get_context(&exp->exp_obd->obd_llogs, LLOG_CONFIG_REPL_CTXT);
487 rc = class_config_parse_llog(ctxt, profile, cfg);
490 * For debugging, it's useful to just dump the log
492 rc = class_config_dump_llog(ctxt, profile, cfg);
495 CERROR("class_config_parse_llog failed: rc = %d\n", rc);
498 err = obd_disconnect(exp, 0);
501 LCFG_INIT(lcfg, LCFG_CLEANUP, name);
502 err = class_process_config(&lcfg);
507 LCFG_INIT(lcfg, LCFG_DETACH, name);
508 err = class_process_config(&lcfg);
513 LCFG_INIT(lcfg, LCFG_DEL_UUID, name);
514 lcfg.lcfg_inllen1 = strlen(peer) + 1;
515 lcfg.lcfg_inlbuf1 = peer;
516 err = class_process_config(&lcfg);
519 if (lmd->lmd_nal == SOCKNAL) {
520 PCFG_INIT(pcfg, NAL_CMD_DEL_AUTOCONN);
521 pcfg.pcfg_nal = lmd->lmd_nal;
522 pcfg.pcfg_nid = lmd->lmd_server_nid;
523 pcfg.pcfg_id = lmd->lmd_server_ipaddr;
524 pcfg.pcfg_flags = 1; /*share*/
525 err = libcfs_nal_cmd(&pcfg);
536 int lustre_fill_super(struct super_block *sb, void *data, int silent)
538 struct lustre_mount_data * lmd = data;
539 struct ll_sb_info *sbi;
545 CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
546 if (lmd_bad_magic(lmd))
549 sbi = lustre_init_sbi(sb);
553 sbi->ll_flags |= LL_SBI_READAHEAD;
555 if (lmd->lmd_profile) {
556 struct lustre_profile *lprof;
557 struct config_llog_instance cfg;
560 if (lmd->lmd_mds[0] == '\0') {
561 CERROR("no mds name\n");
562 GOTO(out_free, err = -EINVAL);
565 OBD_ALLOC(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
566 if (sbi->ll_lmd == NULL)
567 GOTO(out_free, err = -ENOMEM);
568 memcpy(sbi->ll_lmd, lmd, sizeof(*lmd));
570 /* generate a string unique to this super, let's try
571 the address of the super itself.*/
572 len = (sizeof(sb) * 2) + 1;
573 OBD_ALLOC(sbi->ll_instance, len);
574 if (sbi->ll_instance == NULL)
575 GOTO(out_free, err = -ENOMEM);
576 sprintf(sbi->ll_instance, "%p", sb);
578 cfg.cfg_instance = sbi->ll_instance;
579 cfg.cfg_uuid = sbi->ll_sb_uuid;
580 cfg.cfg_local_nid = lmd->lmd_local_nid;
581 err = lustre_process_log(lmd, lmd->lmd_profile, &cfg, 0);
583 CERROR("Unable to process log: %s\n", lmd->lmd_profile);
588 lprof = class_get_profile(lmd->lmd_profile);
590 CERROR("No profile found: %s\n", lmd->lmd_profile);
591 GOTO(out_free, err = -EINVAL);
594 OBD_FREE(osc, strlen(osc) + 1);
595 OBD_ALLOC(osc, strlen(lprof->lp_osc) +
596 strlen(sbi->ll_instance) + 2);
597 sprintf(osc, "%s-%s", lprof->lp_osc, sbi->ll_instance);
600 OBD_FREE(mdc, strlen(mdc) + 1);
601 OBD_ALLOC(mdc, strlen(lprof->lp_mdc) +
602 strlen(sbi->ll_instance) + 2);
603 sprintf(mdc, "%s-%s", lprof->lp_mdc, sbi->ll_instance);
608 GOTO(out_free, err = -EINVAL);
613 GOTO(out_free, err = -EINVAL);
616 err = lustre_common_fill_super(sb, mdc, osc);
623 OBD_FREE(mdc, strlen(mdc) + 1);
625 OBD_FREE(osc, strlen(osc) + 1);
631 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
634 if (sbi->ll_instance != NULL) {
636 struct config_llog_instance cfg;
638 cfg.cfg_instance = sbi->ll_instance;
639 cfg.cfg_uuid = sbi->ll_sb_uuid;
641 OBD_ALLOC(cln_prof, len);
642 sprintf(cln_prof, "%s-clean", sbi->ll_lmd->lmd_profile);
644 err = lustre_process_log(sbi->ll_lmd, cln_prof, &cfg,
647 CERROR("Unable to process log: %s\n", cln_prof);
648 OBD_FREE(cln_prof, len);
649 OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance)+ 1);
651 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
656 } /* lustre_fill_super */
658 static void lustre_manual_cleanup(struct ll_sb_info *sbi)
660 struct lustre_cfg lcfg;
661 struct obd_device *obd;
664 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) != NULL)
668 LCFG_INIT(lcfg, LCFG_CLEANUP, obd->obd_name);
669 err = class_process_config(&lcfg);
671 CERROR("cleanup failed: %s\n", obd->obd_name);
675 LCFG_INIT(lcfg, LCFG_DETACH, obd->obd_name);
676 err = class_process_config(&lcfg);
678 CERROR("detach failed: %s\n", obd->obd_name);
683 if (sbi->ll_lmd != NULL)
684 class_del_profile(sbi->ll_lmd->lmd_profile);
687 void lustre_put_super(struct super_block *sb)
689 struct obd_device *obd;
690 struct ll_sb_info *sbi = ll_s2sbi(sb);
691 int force_umount = 0;
694 CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
695 obd = class_exp2obd(sbi->ll_mdc_exp);
697 force_umount = obd->obd_no_recov;
700 lustre_common_put_super(sb);
702 if (sbi->ll_lmd != NULL) {
704 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
706 struct config_llog_instance cfg;
709 CERROR("force umount, doing manual cleanup\n");
710 lustre_manual_cleanup(sbi);
714 cfg.cfg_instance = sbi->ll_instance;
715 cfg.cfg_uuid = sbi->ll_sb_uuid;
717 OBD_ALLOC(cln_prof, len);
718 sprintf(cln_prof, "%s-clean", sbi->ll_lmd->lmd_profile);
720 err = lustre_process_log(sbi->ll_lmd, cln_prof, &cfg, 0);
722 CERROR("Unable to process log: %s, doing manual cleanup"
724 lustre_manual_cleanup(sbi);
727 OBD_FREE(cln_prof, len);
729 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
730 OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance) + 1);
736 } /* lustre_put_super */
739 struct inode *ll_inode_from_lock(struct ldlm_lock *lock)
742 l_lock(&lock->l_resource->lr_namespace->ns_lock);
743 if (lock->l_ast_data)
744 inode = igrab(lock->l_ast_data);
747 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
751 int null_if_equal(struct ldlm_lock *lock, void *data)
753 if (data == lock->l_ast_data)
754 lock->l_ast_data = NULL;
756 if (lock->l_req_mode != lock->l_granted_mode)
757 return LDLM_ITER_STOP;
759 return LDLM_ITER_CONTINUE;
762 void ll_clear_inode(struct inode *inode)
765 struct ll_inode_info *lli = ll_i2info(inode);
766 struct ll_sb_info *sbi = ll_i2sbi(inode);
769 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
770 inode->i_generation, inode);
772 ll_inode2fid(&fid, inode);
773 clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &(ll_i2info(inode)->lli_flags));
774 md_change_cbdata(sbi->ll_mdc_exp, &fid, null_if_equal, inode);
777 obd_change_cbdata(sbi->ll_osc_exp, lli->lli_smd,
778 null_if_equal, inode);
781 obd_free_memmd(sbi->ll_osc_exp, &lli->lli_smd);
786 /* FIXME: change cbdata for mea here */
787 obd_free_memmd(sbi->ll_mdc_exp,
788 (struct lov_stripe_md **) &lli->lli_mea);
792 if (lli->lli_symlink_name) {
793 OBD_FREE(lli->lli_symlink_name,
794 strlen(lli->lli_symlink_name) + 1);
795 lli->lli_symlink_name = NULL;
801 /* If this inode has objects allocated to it (lsm != NULL), then the OST
802 * object(s) determine the file size and mtime. Otherwise, the MDS will
803 * keep these values until such a time that objects are allocated for it.
804 * We do the MDS operations first, as it is checking permissions for us.
805 * We don't to the MDS RPC if there is nothing that we want to store there,
806 * otherwise there is no harm in updating mtime/atime on the MDS if we are
807 * going to do an RPC anyways.
809 * If we are doing a truncate, we will send the mtime and ctime updates
810 * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
811 * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
814 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
816 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
817 struct ll_sb_info *sbi = ll_i2sbi(inode);
818 struct ptlrpc_request *request = NULL;
819 struct mdc_op_data op_data;
820 int ia_valid = attr->ia_valid;
824 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", inode->i_ino);
825 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_SETATTR);
827 if (ia_valid & ATTR_SIZE) {
828 if (attr->ia_size > ll_file_maxbytes(inode)) {
829 CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
830 attr->ia_size, ll_file_maxbytes(inode));
834 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
837 /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
838 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
839 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
843 /* We mark all of the fields "set" so MDS/OST does not re-set them */
844 if (attr->ia_valid & ATTR_CTIME) {
845 attr->ia_ctime = CURRENT_TIME;
846 attr->ia_valid |= ATTR_CTIME_SET;
848 if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
849 attr->ia_atime = CURRENT_TIME;
850 attr->ia_valid |= ATTR_ATIME_SET;
852 if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
853 attr->ia_mtime = CURRENT_TIME;
854 attr->ia_valid |= ATTR_MTIME_SET;
857 if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
858 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
859 LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
860 LTIME_S(CURRENT_TIME));
862 attr->ia_valid &= ~ATTR_SIZE;
864 /* If only OST attributes being set on objects, don't do MDS RPC.
865 * In that case, we need to check permissions and update the local
866 * inode ourselves so we can call obdo_from_inode() always. */
867 if (ia_valid & (lsm ? ~(ATTR_SIZE | ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
869 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
871 rc = md_setattr(sbi->ll_mdc_exp, &op_data,
872 attr, NULL, 0, NULL, 0, &request);
874 ptlrpc_req_finished(request);
875 if (rc != -EPERM && rc != -EACCES)
876 CERROR("md_setattr fails: rc = %d\n", rc);
880 rc = mdc_req2lustre_md(sbi->ll_mdc_exp, request, 0,
881 sbi->ll_osc_exp, &md);
883 ptlrpc_req_finished(request);
887 /* Won't invoke vmtruncate as we already cleared ATTR_SIZE,
888 * but needed to set timestamps backwards on utime. */
889 inode_setattr(inode, attr);
890 ll_update_inode(inode, &md);
891 ptlrpc_req_finished(request);
893 if (!lsm || !S_ISREG(inode->i_mode)) {
894 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
898 /* The OST doesn't check permissions, but the alternative is
899 * a gratuitous RPC to the MDS. We already rely on the client
900 * to do read/write/truncate permission checks, so is mtime OK?
902 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
903 /* from sys_utime() */
904 if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
905 if (current->fsuid != inode->i_uid &&
906 (rc=ll_permission(inode,MAY_WRITE,NULL))!=0)
909 /* from inode_change_ok() */
910 if (current->fsuid != inode->i_uid &&
911 !capable(CAP_FOWNER))
916 /* Won't invoke vmtruncate, as we already cleared ATTR_SIZE */
917 inode_setattr(inode, attr);
920 /* We really need to get our PW lock before we change inode->i_size.
921 * If we don't we can race with other i_size updaters on our node, like
922 * ll_file_read. We can also race with i_size propogation to other
923 * nodes through dirtying and writeback of final cached pages. This
924 * last one is especially bad for racing o_append users on other
926 if (ia_valid & ATTR_SIZE) {
927 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
929 struct lustre_handle lockh = { 0 };
930 int err, ast_flags = 0;
931 /* XXX when we fix the AST intents to pass the discard-range
932 * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
934 if (attr->ia_size == 0)
935 ast_flags = LDLM_AST_DISCARD_DATA;
937 /* bug 1639: avoid write/truncate i_sem/DLM deadlock */
938 LASSERT(atomic_read(&inode->i_sem.count) <= 0);
940 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW, &policy, &lockh,
946 rc = vmtruncate(inode, attr->ia_size);
948 /* We need to drop the semaphore here, because this unlock may
949 * result in a cancellation, which will need the i_sem */
951 /* unlock now as we don't mind others file lockers racing with
952 * the mds updates below? */
953 err = ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
956 CERROR("ll_extent_unlock failed: %d\n", err);
960 } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
963 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
964 inode->i_ino, LTIME_S(attr->ia_mtime));
965 oa.o_id = lsm->lsm_object_id;
966 oa.o_gr = lsm->lsm_object_gr;
967 oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
968 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
969 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
970 rc = obd_setattr(sbi->ll_osc_exp, &oa, lsm, NULL);
972 CERROR("obd_setattr fails: rc=%d\n", rc);
977 int ll_setattr(struct dentry *de, struct iattr *attr)
979 LBUG(); /* code is unused, but leave this in case of VFS changes */
983 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
984 unsigned long max_age)
986 struct ll_sb_info *sbi = ll_s2sbi(sb);
987 struct obd_statfs obd_osfs;
991 rc = obd_statfs(class_exp2obd(sbi->ll_mdc_exp), osfs, max_age);
993 CERROR("mdc_statfs fails: rc = %d\n", rc);
997 osfs->os_type = sb->s_magic;
999 CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1000 osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1002 rc = obd_statfs(class_exp2obd(sbi->ll_osc_exp), &obd_osfs, max_age);
1004 CERROR("obd_statfs fails: rc = %d\n", rc);
1008 CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1009 obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1012 osfs->os_blocks = obd_osfs.os_blocks;
1013 osfs->os_bfree = obd_osfs.os_bfree;
1014 osfs->os_bavail = obd_osfs.os_bavail;
1016 /* If we don't have as many objects free on the OST as inodes
1017 * on the MDS, we reduce the total number of inodes to
1018 * compensate, so that the "inodes in use" number is correct.
1020 if (obd_osfs.os_ffree < osfs->os_ffree) {
1021 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1023 osfs->os_ffree = obd_osfs.os_ffree;
1029 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
1031 struct obd_statfs osfs;
1034 CDEBUG(D_VFSTRACE, "VFS Op:\n");
1035 lprocfs_counter_incr(ll_s2sbi(sb)->ll_stats, LPROC_LL_STAFS);
1037 /* For now we will always get up-to-date statfs values, but in the
1038 * future we may allow some amount of caching on the client (e.g.
1039 * from QOS or lprocfs updates). */
1040 rc = ll_statfs_internal(sb, &osfs, jiffies - 1);
1044 statfs_unpack(sfs, &osfs);
1046 if (sizeof(sfs->f_blocks) == 4) {
1047 while (osfs.os_blocks > ~0UL) {
1050 osfs.os_blocks >>= 1;
1051 osfs.os_bfree >>= 1;
1052 osfs.os_bavail >>= 1;
1056 sfs->f_blocks = osfs.os_blocks;
1057 sfs->f_bfree = osfs.os_bfree;
1058 sfs->f_bavail = osfs.os_bavail;
1063 void dump_lsm(int level, struct lov_stripe_md *lsm)
1065 CDEBUG(level, "objid "LPX64", maxbytes "LPX64", magic 0x%08X, "
1066 "stripe_size %u, stripe_count %u\n",
1067 lsm->lsm_object_id, lsm->lsm_maxbytes, lsm->lsm_magic,
1068 lsm->lsm_stripe_size, lsm->lsm_stripe_count);
1071 void ll_update_inode(struct inode *inode, struct lustre_md *md)
1073 struct ll_inode_info *lli = ll_i2info(inode);
1074 struct mds_body *body = md->body;
1075 struct lov_stripe_md *lsm = md->lsm;
1076 struct mea *mea = md->mea;
1079 LASSERT((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
1080 LASSERT((mea != NULL) == ((body->valid & OBD_MD_FLDIREA) != 0));
1082 LASSERT(lsm->lsm_object_gr > 0);
1083 if (lli->lli_smd == NULL) {
1085 lli->lli_maxbytes = lsm->lsm_maxbytes;
1086 if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1087 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1089 if (memcmp(lli->lli_smd, lsm, sizeof(*lsm))) {
1090 CERROR("lsm mismatch for inode %ld\n",
1092 CERROR("lli_smd:\n");
1093 dump_lsm(D_ERROR, lli->lli_smd);
1095 dump_lsm(D_ERROR, lsm);
1099 /* bug 2844 - limit i_blksize for broken user-space apps */
1100 LASSERTF(lsm->lsm_xfersize != 0, "%lu\n", lsm->lsm_xfersize);
1101 inode->i_blksize = min(lsm->lsm_xfersize, LL_MAX_BLKSIZE);
1102 if (lli->lli_smd != lsm)
1103 obd_free_memmd(ll_i2obdexp(inode), &lsm);
1107 if (lli->lli_mea == NULL) {
1110 if (memcmp(lli->lli_mea, mea, body->eadatasize)) {
1111 CERROR("mea mismatch for inode %lu\n",
1116 if (lli->lli_mea != mea)
1117 obd_free_memmd(ll_i2mdcexp(inode),
1118 (struct lov_stripe_md **) &mea);
1122 if (body->valid & OBD_MD_FLID)
1123 inode->i_ino = body->ino;
1124 if (body->valid & OBD_MD_FLATIME)
1125 LTIME_S(inode->i_atime) = body->atime;
1126 if (body->valid & OBD_MD_FLMTIME &&
1127 body->mtime > LTIME_S(inode->i_mtime)) {
1128 CDEBUG(D_INODE, "setting ino %lu mtime from %lu to %u\n",
1129 inode->i_ino, LTIME_S(inode->i_mtime), body->mtime);
1130 LTIME_S(inode->i_mtime) = body->mtime;
1132 if (body->valid & OBD_MD_FLCTIME &&
1133 body->ctime > LTIME_S(inode->i_ctime))
1134 LTIME_S(inode->i_ctime) = body->ctime;
1135 if (body->valid & OBD_MD_FLMODE)
1136 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
1137 if (body->valid & OBD_MD_FLTYPE)
1138 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
1139 if (body->valid & OBD_MD_FLUID)
1140 inode->i_uid = body->uid;
1141 if (body->valid & OBD_MD_FLGID)
1142 inode->i_gid = body->gid;
1143 if (body->valid & OBD_MD_FLFLAGS)
1144 inode->i_flags = body->flags;
1145 if (body->valid & OBD_MD_FLNLINK)
1146 inode->i_nlink = body->nlink;
1147 if (body->valid & OBD_MD_FLGENER)
1148 inode->i_generation = body->generation;
1149 if (body->valid & OBD_MD_FLRDEV)
1150 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1151 inode->i_rdev = body->rdev;
1153 inode->i_rdev = old_decode_dev(body->rdev);
1155 if (body->valid & OBD_MD_FLSIZE)
1156 inode->i_size = body->size;
1157 if (body->valid & OBD_MD_FLBLOCKS)
1158 inode->i_blocks = body->blocks;
1160 if (body->valid & OBD_MD_FLSIZE)
1161 set_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
1163 lli->lli_mds = body->mds;
1164 inode->i_dev = (kdev_t) body->mds;
1165 LASSERT(body->mds < 1000);
1168 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
1169 static struct backing_dev_info ll_backing_dev_info = {
1170 .ra_pages = 0, /* No readahead */
1171 .memory_backed = 0, /* Does contribute to dirty memory */
1175 void ll_read_inode2(struct inode *inode, void *opaque)
1177 struct lustre_md *md = opaque;
1178 struct ll_inode_info *lli = ll_i2info(inode);
1181 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1182 inode->i_generation, inode);
1186 LASSERT(!lli->lli_smd);
1188 /* Core attributes from the MDS first. This is a new inode, and
1189 * the VFS doesn't zero times in the core inode so we have to do
1190 * it ourselves. They will be overwritten by either MDS or OST
1191 * attributes - we just need to make sure they aren't newer. */
1192 LTIME_S(inode->i_mtime) = 0;
1193 LTIME_S(inode->i_atime) = 0;
1194 LTIME_S(inode->i_ctime) = 0;
1197 ll_update_inode(inode, md);
1199 /* OIDEBUG(inode); */
1201 if (S_ISREG(inode->i_mode)) {
1202 inode->i_op = &ll_file_inode_operations;
1203 inode->i_fop = &ll_file_operations;
1204 inode->i_mapping->a_ops = &ll_aops;
1206 } else if (S_ISDIR(inode->i_mode)) {
1207 inode->i_op = &ll_dir_inode_operations;
1208 inode->i_fop = &ll_dir_operations;
1209 inode->i_mapping->a_ops = &ll_dir_aops;
1211 } else if (S_ISLNK(inode->i_mode)) {
1212 inode->i_op = &ll_fast_symlink_inode_operations;
1215 inode->i_op = &ll_special_inode_operations;
1217 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1218 init_special_inode(inode, inode->i_mode,
1219 kdev_t_to_nr(inode->i_rdev));
1221 /* initializing backing dev info. */
1222 inode->i_mapping->backing_dev_info = &ll_backing_dev_info;
1224 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1226 lli->ll_save_ifop = inode->i_fop;
1228 if (S_ISCHR(inode->i_mode))
1229 inode->i_fop = &ll_special_chr_inode_fops;
1230 else if (S_ISBLK(inode->i_mode))
1231 inode->i_fop = &ll_special_blk_inode_fops;
1232 else if (S_ISFIFO(inode->i_mode))
1233 inode->i_fop = &ll_special_fifo_inode_fops;
1234 else if (S_ISSOCK(inode->i_mode))
1235 inode->i_fop = &ll_special_sock_inode_fops;
1237 CWARN("saved %p, replaced with %p\n", lli->ll_save_ifop,
1240 if (lli->ll_save_ifop->owner) {
1241 CWARN("%p has owner %p\n", lli->ll_save_ifop,
1242 lli->ll_save_ifop->owner);
1248 int ll_iocontrol(struct inode *inode, struct file *file,
1249 unsigned int cmd, unsigned long arg)
1251 struct ll_sb_info *sbi = ll_i2sbi(inode);
1252 struct ptlrpc_request *req = NULL;
1257 case EXT3_IOC_GETFLAGS: {
1259 unsigned long valid = OBD_MD_FLFLAGS;
1260 struct mds_body *body;
1262 ll_inode2fid(&fid, inode);
1263 rc = md_getattr(sbi->ll_mdc_exp, &fid, valid, 0, &req);
1265 CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1269 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1271 if (body->flags & S_APPEND)
1272 flags |= EXT3_APPEND_FL;
1273 if (body->flags & S_IMMUTABLE)
1274 flags |= EXT3_IMMUTABLE_FL;
1275 if (body->flags & S_NOATIME)
1276 flags |= EXT3_NOATIME_FL;
1278 ptlrpc_req_finished (req);
1280 RETURN(put_user(flags, (int *)arg));
1282 case EXT3_IOC_SETFLAGS: {
1283 struct mdc_op_data op_data;
1286 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1288 if (get_user(flags, (int *)arg))
1295 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
1297 memset(&attr, 0x0, sizeof(attr));
1298 attr.ia_attr_flags = flags;
1299 attr.ia_valid |= ATTR_ATTR_FLAG;
1301 rc = md_setattr(sbi->ll_mdc_exp, &op_data,
1302 &attr, NULL, 0, NULL, 0, &req);
1304 ptlrpc_req_finished(req);
1305 if (rc != -EPERM && rc != -EACCES)
1306 CERROR("md_setattr fails: rc = %d\n", rc);
1310 ptlrpc_req_finished(req);
1312 oa->o_id = lsm->lsm_object_id;
1313 oa->o_gr = lsm->lsm_object_gr;
1314 oa->o_flags = flags;
1315 oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
1317 rc = obd_setattr(sbi->ll_osc_exp, oa, lsm, NULL);
1320 if (rc != -EPERM && rc != -EACCES)
1321 CERROR("md_setattr fails: rc = %d\n", rc);
1325 if (flags & EXT3_APPEND_FL)
1326 inode->i_flags |= S_APPEND;
1328 inode->i_flags &= ~S_APPEND;
1329 if (flags & EXT3_IMMUTABLE_FL)
1330 inode->i_flags |= S_IMMUTABLE;
1332 inode->i_flags &= ~S_IMMUTABLE;
1333 if (flags & EXT3_NOATIME_FL)
1334 inode->i_flags |= S_NOATIME;
1336 inode->i_flags &= ~S_NOATIME;
1347 void ll_umount_begin(struct super_block *sb)
1349 struct ll_sb_info *sbi = ll_s2sbi(sb);
1350 struct obd_device *obd;
1351 struct obd_ioctl_data ioc_data = { 0 };
1353 CDEBUG(D_VFSTRACE, "VFS Op:\n");
1355 obd = class_exp2obd(sbi->ll_mdc_exp);
1357 CERROR("Invalid MDC connection handle "LPX64"\n",
1358 sbi->ll_mdc_exp->exp_handle.h_cookie);
1362 obd->obd_no_recov = 1;
1363 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_mdc_exp, sizeof ioc_data,
1366 obd = class_exp2obd(sbi->ll_osc_exp);
1368 CERROR("Invalid LOV connection handle "LPX64"\n",
1369 sbi->ll_osc_exp->exp_handle.h_cookie);
1374 obd->obd_no_recov = 1;
1375 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_osc_exp, sizeof ioc_data,
1378 /* Really, we'd like to wait until there are no requests outstanding,
1379 * and then continue. For now, we just invalidate the requests,
1380 * schedule, and hope.
1387 int ll_prep_inode(struct obd_export *osc_exp, struct obd_export *mdc_exp,
1388 struct inode **inode, struct ptlrpc_request *req,
1389 int offset, struct super_block *sb)
1391 struct lustre_md md;
1394 rc = mdc_req2lustre_md(mdc_exp, req, offset, osc_exp, &md);
1399 ll_update_inode(*inode, &md);
1402 *inode = ll_iget(sb, md.body->ino, &md);
1403 if (*inode == NULL || is_bad_inode(*inode)) {
1404 /* free the lsm if we allocated one above */
1406 obd_free_memmd(osc_exp, &md.lsm);
1408 obd_free_memmd(mdc_exp,
1409 (struct lov_stripe_md**)&md.mea);
1411 CERROR("new_inode -fatal: rc %d\n", rc);