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;
41 extern struct super_operations ll_super_operations;
44 #define log2(n) ffz(~(n))
47 struct ll_sb_info *lustre_init_sbi(struct super_block *sb)
49 struct ll_sb_info *sbi = NULL;
53 OBD_ALLOC(sbi, sizeof(*sbi));
57 spin_lock_init(&sbi->ll_pglist_lock);
58 INIT_LIST_HEAD(&sbi->ll_pglist);
59 sbi->ll_pglist_gen = 0;
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(request, 0, sbi->ll_osc_exp,
197 sbi->ll_mdc_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 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
222 #warning "Please fix this"
224 /* bug 2805 - set VM readahead to zero */
225 vm_max_readahead = vm_min_readahead = 0;
227 sb->s_root = d_alloc_root(root);
234 obd_disconnect(sbi->ll_osc_exp, 0);
236 obd_disconnect(sbi->ll_mdc_exp, 0);
238 lprocfs_unregister_mountpoint(sbi);
242 void lustre_common_put_super(struct super_block *sb)
244 struct ll_sb_info *sbi = ll_s2sbi(sb);
245 struct hlist_node *tmp, *next;
248 ll_close_thread_shutdown(sbi->ll_lcq);
250 list_del(&sbi->ll_conn_chain);
251 obd_disconnect(sbi->ll_osc_exp, 0);
253 lprocfs_unregister_mountpoint(sbi);
254 if (sbi->ll_proc_root) {
255 lprocfs_remove(sbi->ll_proc_root);
256 sbi->ll_proc_root = NULL;
259 obd_disconnect(sbi->ll_mdc_exp, 0);
261 // We do this to get rid of orphaned dentries. That is not really trw.
262 spin_lock(&dcache_lock);
263 hlist_for_each_safe(tmp, next, &sbi->ll_orphan_dentry_list) {
264 struct dentry *dentry = hlist_entry(tmp, struct dentry, d_hash);
265 shrink_dcache_parent(dentry);
267 spin_unlock(&dcache_lock);
272 char *ll_read_opt(const char *opt, char *data)
278 CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
279 if (strncmp(opt, data, strlen(opt)))
281 if ((value = strchr(data, '=')) == NULL)
285 OBD_ALLOC(retval, strlen(value) + 1);
287 CERROR("out of memory!\n");
291 memcpy(retval, value, strlen(value)+1);
292 CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
296 int ll_set_opt(const char *opt, char *data, int fl)
300 CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
301 if (strncmp(opt, data, strlen(opt)))
307 void ll_options(char *options, char **ost, char **mdc, int *flags)
310 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
311 char *opt_ptr = options;
320 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
321 for (this_char = strtok (options, ",");
323 this_char = strtok (NULL, ",")) {
325 while ((this_char = strsep (&opt_ptr, ",")) != NULL) {
327 CDEBUG(D_SUPER, "this_char %s\n", this_char);
328 if (!*ost && (*ost = ll_read_opt("osc", this_char)))
330 if (!*mdc && (*mdc = ll_read_opt("mdc", this_char)))
332 if (!(*flags & LL_SBI_NOLCK) &&
333 ((*flags) = (*flags) |
334 ll_set_opt("nolock", this_char,
341 void ll_lli_init(struct ll_inode_info *lli)
343 sema_init(&lli->lli_open_sem, 1);
345 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
346 spin_lock_init(&lli->lli_lock);
347 INIT_LIST_HEAD(&lli->lli_pending_write_llaps);
350 int ll_fill_super(struct super_block *sb, void *data, int silent)
352 struct ll_sb_info *sbi;
358 CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
360 sbi = lustre_init_sbi(sb);
364 sbi->ll_flags |= LL_SBI_READAHEAD;
365 ll_options(data, &osc, &mdc, &sbi->ll_flags);
369 GOTO(out, err = -EINVAL);
374 GOTO(out, err = -EINVAL);
377 err = lustre_common_fill_super(sb, mdc, osc);
383 OBD_FREE(mdc, strlen(mdc) + 1);
385 OBD_FREE(osc, strlen(osc) + 1);
388 } /* ll_read_super */
390 void ll_put_super(struct super_block *sb)
394 CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
396 lustre_common_put_super(sb);
403 int lustre_process_log(struct lustre_mount_data *lmd, char * profile,
404 struct config_llog_instance *cfg, int allow_recov)
406 struct lustre_cfg lcfg;
407 struct portals_cfg pcfg;
408 char * peer = "MDS_PEER_UUID";
409 struct obd_device *obd;
410 struct lustre_handle mdc_conn = {0, };
411 struct obd_export *exp;
412 char * name = "mdc_dev";
414 struct obd_uuid mdc_uuid;
415 struct llog_ctxt *ctxt;
420 if (lmd_bad_magic(lmd))
423 generate_random_uuid(uuid);
424 class_uuid_unparse(uuid, &mdc_uuid);
426 if (lmd->lmd_local_nid) {
427 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
428 pcfg.pcfg_nal = lmd->lmd_nal;
429 pcfg.pcfg_nid = lmd->lmd_local_nid;
430 err = kportal_nal_cmd(&pcfg);
435 if (lmd->lmd_nal == SOCKNAL) {
436 PCFG_INIT(pcfg, NAL_CMD_ADD_AUTOCONN);
437 pcfg.pcfg_nal = lmd->lmd_nal;
438 pcfg.pcfg_nid = lmd->lmd_server_nid;
439 pcfg.pcfg_id = lmd->lmd_server_ipaddr;
440 pcfg.pcfg_misc = lmd->lmd_port;
441 pcfg.pcfg_size = 8388608;
442 pcfg.pcfg_flags = 0x4; /*share*/
443 err = kportal_nal_cmd(&pcfg);
448 LCFG_INIT(lcfg, LCFG_ADD_UUID, name);
449 lcfg.lcfg_nid = lmd->lmd_server_nid;
450 lcfg.lcfg_inllen1 = strlen(peer) + 1;
451 lcfg.lcfg_inlbuf1 = peer;
452 lcfg.lcfg_nal = lmd->lmd_nal;
453 err = class_process_config(&lcfg);
455 GOTO(out_del_conn, err);
457 LCFG_INIT(lcfg, LCFG_ATTACH, name);
458 lcfg.lcfg_inlbuf1 = "mdc";
459 lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
460 lcfg.lcfg_inlbuf2 = mdc_uuid.uuid;
461 lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
462 err = class_process_config(&lcfg);
464 GOTO(out_del_uuid, err);
466 LCFG_INIT(lcfg, LCFG_SETUP, name);
467 lcfg.lcfg_inlbuf1 = lmd->lmd_mds;
468 lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
469 lcfg.lcfg_inlbuf2 = peer;
470 lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
471 err = class_process_config(&lcfg);
473 GOTO(out_detach, err);
475 obd = class_name2obd(name);
477 GOTO(out_cleanup, err = -EINVAL);
479 /* Disable initial recovery on this import */
480 err = obd_set_info(obd->obd_self_export,
481 strlen("initial_recov"), "initial_recov",
482 sizeof(allow_recov), &allow_recov);
484 GOTO(out_cleanup, err);
486 err = obd_connect(&mdc_conn, obd, &mdc_uuid);
488 CERROR("cannot connect to %s: rc = %d\n", lmd->lmd_mds, err);
489 GOTO(out_cleanup, err);
492 exp = class_conn2export(&mdc_conn);
494 ctxt = llog_get_context(&exp->exp_obd->obd_llogs, LLOG_CONFIG_REPL_CTXT);
496 rc = class_config_parse_llog(ctxt, profile, cfg);
499 * For debugging, it's useful to just dump the log
501 rc = class_config_dump_llog(ctxt, profile, cfg);
504 CERROR("class_config_parse_llog failed: rc = %d\n", rc);
507 err = obd_disconnect(exp, 0);
510 LCFG_INIT(lcfg, LCFG_CLEANUP, name);
511 err = class_process_config(&lcfg);
516 LCFG_INIT(lcfg, LCFG_DETACH, name);
517 err = class_process_config(&lcfg);
522 LCFG_INIT(lcfg, LCFG_DEL_UUID, name);
523 lcfg.lcfg_inllen1 = strlen(peer) + 1;
524 lcfg.lcfg_inlbuf1 = peer;
525 err = class_process_config(&lcfg);
528 if (lmd->lmd_nal == SOCKNAL) {
529 PCFG_INIT(pcfg, NAL_CMD_DEL_AUTOCONN);
530 pcfg.pcfg_nal = lmd->lmd_nal;
531 pcfg.pcfg_nid = lmd->lmd_server_nid;
532 pcfg.pcfg_id = lmd->lmd_server_ipaddr;
533 pcfg.pcfg_flags = 1; /*share*/
534 err = kportal_nal_cmd(&pcfg);
545 int lustre_fill_super(struct super_block *sb, void *data, int silent)
547 struct lustre_mount_data * lmd = data;
548 struct ll_sb_info *sbi;
554 CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
555 if (lmd_bad_magic(lmd))
558 sbi = lustre_init_sbi(sb);
562 sbi->ll_flags |= LL_SBI_READAHEAD;
564 if (lmd->lmd_profile) {
565 struct lustre_profile *lprof;
566 struct config_llog_instance cfg;
570 CERROR("no mds name\n");
571 GOTO(out_free, err = -EINVAL);
574 OBD_ALLOC(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
575 if (sbi->ll_lmd == NULL)
576 GOTO(out_free, err = -ENOMEM);
577 memcpy(sbi->ll_lmd, lmd, sizeof(*lmd));
579 /* generate a string unique to this super, let's try
580 the address of the super itself.*/
581 len = (sizeof(sb) * 2) + 1;
582 OBD_ALLOC(sbi->ll_instance, len);
583 if (sbi->ll_instance == NULL)
584 GOTO(out_free, err = -ENOMEM);
585 sprintf(sbi->ll_instance, "%p", sb);
587 cfg.cfg_instance = sbi->ll_instance;
588 cfg.cfg_uuid = sbi->ll_sb_uuid;
589 cfg.cfg_local_nid = lmd->lmd_local_nid;
590 err = lustre_process_log(lmd, lmd->lmd_profile, &cfg, 0);
592 CERROR("Unable to process log: %s\n", lmd->lmd_profile);
597 lprof = class_get_profile(lmd->lmd_profile);
599 CERROR("No profile found: %s\n", lmd->lmd_profile);
600 GOTO(out_free, err = -EINVAL);
603 OBD_FREE(osc, strlen(osc) + 1);
604 OBD_ALLOC(osc, strlen(lprof->lp_osc) +
605 strlen(sbi->ll_instance) + 2);
606 sprintf(osc, "%s-%s", lprof->lp_osc, sbi->ll_instance);
609 OBD_FREE(mdc, strlen(mdc) + 1);
610 OBD_ALLOC(mdc, strlen(lprof->lp_mdc) +
611 strlen(sbi->ll_instance) + 2);
612 sprintf(mdc, "%s-%s", lprof->lp_mdc, sbi->ll_instance);
617 GOTO(out_free, err = -EINVAL);
622 GOTO(out_free, err = -EINVAL);
625 err = lustre_common_fill_super(sb, mdc, osc);
632 OBD_FREE(mdc, strlen(mdc) + 1);
634 OBD_FREE(osc, strlen(osc) + 1);
640 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
643 if (sbi->ll_instance != NULL) {
645 struct config_llog_instance cfg;
647 cfg.cfg_instance = sbi->ll_instance;
648 cfg.cfg_uuid = sbi->ll_sb_uuid;
650 OBD_ALLOC(cln_prof, len);
651 sprintf(cln_prof, "%s-clean", sbi->ll_lmd->lmd_profile);
653 err = lustre_process_log(sbi->ll_lmd, cln_prof, &cfg,
656 CERROR("Unable to process log: %s\n", cln_prof);
657 OBD_FREE(cln_prof, len);
658 OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance)+ 1);
660 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
665 } /* lustre_fill_super */
667 static void lustre_manual_cleanup(struct ll_sb_info *sbi)
669 struct lustre_cfg lcfg;
670 struct obd_device *obd;
673 while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) != NULL)
677 LCFG_INIT(lcfg, LCFG_CLEANUP, obd->obd_name);
678 err = class_process_config(&lcfg);
680 CERROR("cleanup failed: %s\n", obd->obd_name);
684 LCFG_INIT(lcfg, LCFG_DETACH, obd->obd_name);
685 err = class_process_config(&lcfg);
687 CERROR("detach failed: %s\n", obd->obd_name);
692 if (sbi->ll_lmd != NULL)
693 class_del_profile(sbi->ll_lmd->lmd_profile);
696 void lustre_put_super(struct super_block *sb)
698 struct obd_device *obd;
699 struct ll_sb_info *sbi = ll_s2sbi(sb);
700 int force_umount = 0;
703 CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
704 obd = class_exp2obd(sbi->ll_mdc_exp);
706 force_umount = obd->obd_no_recov;
709 lustre_common_put_super(sb);
711 if (sbi->ll_lmd != NULL) {
713 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
715 struct config_llog_instance cfg;
718 CERROR("force umount, doing manual cleanup\n");
719 lustre_manual_cleanup(sbi);
723 cfg.cfg_instance = sbi->ll_instance;
724 cfg.cfg_uuid = sbi->ll_sb_uuid;
726 OBD_ALLOC(cln_prof, len);
727 sprintf(cln_prof, "%s-clean", sbi->ll_lmd->lmd_profile);
729 err = lustre_process_log(sbi->ll_lmd, cln_prof, &cfg, 0);
731 CERROR("Unable to process log: %s, doing manual cleanup"
733 lustre_manual_cleanup(sbi);
736 OBD_FREE(cln_prof, len);
738 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
739 OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance) + 1);
745 } /* lustre_put_super */
748 struct inode *ll_inode_from_lock(struct ldlm_lock *lock)
751 l_lock(&lock->l_resource->lr_namespace->ns_lock);
752 if (lock->l_ast_data)
753 inode = igrab(lock->l_ast_data);
756 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
760 int null_if_equal(struct ldlm_lock *lock, void *data)
762 if (data == lock->l_ast_data)
763 lock->l_ast_data = NULL;
765 if (lock->l_req_mode != lock->l_granted_mode)
766 return LDLM_ITER_STOP;
768 return LDLM_ITER_CONTINUE;
771 void ll_clear_inode(struct inode *inode)
774 struct ll_inode_info *lli = ll_i2info(inode);
775 struct ll_sb_info *sbi = ll_i2sbi(inode);
778 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
779 inode->i_generation, inode);
781 ll_inode2fid(&fid, inode);
782 clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &(ll_i2info(inode)->lli_flags));
783 md_change_cbdata(sbi->ll_mdc_exp, &fid, null_if_equal, inode);
786 obd_change_cbdata(sbi->ll_osc_exp, lli->lli_smd,
787 null_if_equal, inode);
790 obd_free_memmd(sbi->ll_osc_exp, &lli->lli_smd);
795 /* FIXME: change cbdata for mea here */
796 obd_free_memmd(sbi->ll_mdc_exp,
797 (struct lov_stripe_md **) &lli->lli_mea);
801 if (lli->lli_symlink_name) {
802 OBD_FREE(lli->lli_symlink_name,
803 strlen(lli->lli_symlink_name) + 1);
804 lli->lli_symlink_name = NULL;
810 /* If this inode has objects allocated to it (lsm != NULL), then the OST
811 * object(s) determine the file size and mtime. Otherwise, the MDS will
812 * keep these values until such a time that objects are allocated for it.
813 * We do the MDS operations first, as it is checking permissions for us.
814 * We don't to the MDS RPC if there is nothing that we want to store there,
815 * otherwise there is no harm in updating mtime/atime on the MDS if we are
816 * going to do an RPC anyways.
818 * If we are doing a truncate, we will send the mtime and ctime updates
819 * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
820 * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
823 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
825 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
826 struct ll_sb_info *sbi = ll_i2sbi(inode);
827 struct ptlrpc_request *request = NULL;
828 struct mdc_op_data op_data;
829 int ia_valid = attr->ia_valid;
833 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", inode->i_ino);
834 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_SETATTR);
836 if (ia_valid & ATTR_SIZE) {
837 if (attr->ia_size > ll_file_maxbytes(inode)) {
838 CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
839 attr->ia_size, ll_file_maxbytes(inode));
843 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
846 /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
847 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
848 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
852 /* We mark all of the fields "set" so MDS/OST does not re-set them */
853 if (attr->ia_valid & ATTR_CTIME) {
854 attr->ia_ctime = CURRENT_TIME;
855 attr->ia_valid |= ATTR_CTIME_SET;
857 if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
858 attr->ia_atime = CURRENT_TIME;
859 attr->ia_valid |= ATTR_ATIME_SET;
861 if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
862 attr->ia_mtime = CURRENT_TIME;
863 attr->ia_valid |= ATTR_MTIME_SET;
866 if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
867 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
868 LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
869 LTIME_S(CURRENT_TIME));
871 attr->ia_valid &= ~ATTR_SIZE;
873 /* If only OST attributes being set on objects, don't do MDS RPC.
874 * In that case, we need to check permissions and update the local
875 * inode ourselves so we can call obdo_from_inode() always. */
876 if (ia_valid & (lsm ? ~(ATTR_SIZE | ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
878 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
880 rc = md_setattr(sbi->ll_mdc_exp, &op_data,
881 attr, NULL, 0, NULL, 0, &request);
884 ptlrpc_req_finished(request);
885 if (rc != -EPERM && rc != -EACCES)
886 CERROR("md_setattr fails: rc = %d\n", rc);
890 rc = mdc_req2lustre_md(request, 0, sbi->ll_osc_exp,
891 sbi->ll_mdc_exp, &md);
893 ptlrpc_req_finished(request);
897 /* Won't invoke vmtruncate as we already cleared ATTR_SIZE,
898 * but needed to set timestamps backwards on utime. */
899 inode_setattr(inode, attr);
900 ll_update_inode(inode, &md);
901 ptlrpc_req_finished(request);
903 if (!lsm || !S_ISREG(inode->i_mode)) {
904 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
908 /* The OST doesn't check permissions, but the alternative is
909 * a gratuitous RPC to the MDS. We already rely on the client
910 * to do read/write/truncate permission checks, so is mtime OK?
912 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
913 /* from sys_utime() */
914 if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
915 if (current->fsuid != inode->i_uid &&
916 (rc=ll_permission(inode,MAY_WRITE,NULL))!=0)
919 /* from inode_change_ok() */
920 if (current->fsuid != inode->i_uid &&
921 !capable(CAP_FOWNER))
926 /* Won't invoke vmtruncate, as we already cleared ATTR_SIZE */
927 inode_setattr(inode, attr);
930 /* We really need to get our PW lock before we change inode->i_size.
931 * If we don't we can race with other i_size updaters on our node, like
932 * ll_file_read. We can also race with i_size propogation to other
933 * nodes through dirtying and writeback of final cached pages. This
934 * last one is especially bad for racing o_append users on other
936 if (ia_valid & ATTR_SIZE) {
937 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
939 struct lustre_handle lockh = { 0 };
940 int err, ast_flags = 0;
941 /* XXX when we fix the AST intents to pass the discard-range
942 * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
944 if (attr->ia_size == 0)
945 ast_flags = LDLM_AST_DISCARD_DATA;
947 /* bug 1639: avoid write/truncate i_sem/DLM deadlock */
948 LASSERT(atomic_read(&inode->i_sem.count) <= 0);
950 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW, &policy, &lockh,
956 rc = vmtruncate(inode, attr->ia_size);
958 /* We need to drop the semaphore here, because this unlock may
959 * result in a cancellation, which will need the i_sem */
961 /* unlock now as we don't mind others file lockers racing with
962 * the mds updates below? */
963 err = ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
966 CERROR("ll_extent_unlock failed: %d\n", err);
970 } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
973 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
974 inode->i_ino, LTIME_S(attr->ia_mtime));
975 oa.o_id = lsm->lsm_object_id;
976 oa.o_gr = lsm->lsm_object_gr;
977 oa.o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
978 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
979 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
980 rc = obd_setattr(sbi->ll_osc_exp, &oa, lsm, NULL);
982 CERROR("obd_setattr fails: rc=%d\n", rc);
987 int ll_setattr(struct dentry *de, struct iattr *attr)
989 LBUG(); /* code is unused, but leave this in case of VFS changes */
993 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
994 unsigned long max_age)
996 struct ll_sb_info *sbi = ll_s2sbi(sb);
997 struct obd_statfs obd_osfs;
1001 rc = obd_statfs(class_exp2obd(sbi->ll_mdc_exp), osfs, max_age);
1003 CERROR("mdc_statfs fails: rc = %d\n", rc);
1007 osfs->os_type = sb->s_magic;
1009 CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1010 osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
1012 rc = obd_statfs(class_exp2obd(sbi->ll_osc_exp), &obd_osfs, max_age);
1014 CERROR("obd_statfs fails: rc = %d\n", rc);
1018 CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
1019 obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
1022 osfs->os_blocks = obd_osfs.os_blocks;
1023 osfs->os_bfree = obd_osfs.os_bfree;
1024 osfs->os_bavail = obd_osfs.os_bavail;
1026 /* If we don't have as many objects free on the OST as inodes
1027 * on the MDS, we reduce the total number of inodes to
1028 * compensate, so that the "inodes in use" number is correct.
1030 if (obd_osfs.os_ffree < osfs->os_ffree) {
1031 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
1033 osfs->os_ffree = obd_osfs.os_ffree;
1039 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
1041 struct obd_statfs osfs;
1044 CDEBUG(D_VFSTRACE, "VFS Op:\n");
1045 lprocfs_counter_incr(ll_s2sbi(sb)->ll_stats, LPROC_LL_STAFS);
1047 /* For now we will always get up-to-date statfs values, but in the
1048 * future we may allow some amount of caching on the client (e.g.
1049 * from QOS or lprocfs updates). */
1050 rc = ll_statfs_internal(sb, &osfs, jiffies - 1);
1054 statfs_unpack(sfs, &osfs);
1056 if (sizeof(sfs->f_blocks) == 4) {
1057 while (osfs.os_blocks > ~0UL) {
1060 osfs.os_blocks >>= 1;
1061 osfs.os_bfree >>= 1;
1062 osfs.os_bavail >>= 1;
1066 sfs->f_blocks = osfs.os_blocks;
1067 sfs->f_bfree = osfs.os_bfree;
1068 sfs->f_bavail = osfs.os_bavail;
1073 void dump_lsm(int level, struct lov_stripe_md *lsm)
1075 CDEBUG(level, "objid "LPX64", maxbytes "LPX64", magic 0x%08X, "
1076 "stripe_size %u, stripe_count %u\n",
1077 lsm->lsm_object_id, lsm->lsm_maxbytes, lsm->lsm_magic,
1078 lsm->lsm_stripe_size, lsm->lsm_stripe_count);
1081 void ll_update_inode(struct inode *inode, struct lustre_md *md)
1083 struct ll_inode_info *lli = ll_i2info(inode);
1084 struct mds_body *body = md->body;
1085 struct lov_stripe_md *lsm = md->lsm;
1086 struct mea *mea = md->mea;
1089 LASSERT((((__u32) lsm | (__u32) mea) != 0) ==
1090 ((body->valid & OBD_MD_FLEASIZE) != 0));
1092 LASSERT(lsm->lsm_object_gr > 0);
1093 if (lli->lli_smd == NULL) {
1095 lli->lli_maxbytes = lsm->lsm_maxbytes;
1096 if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1097 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1099 if (memcmp(lli->lli_smd, lsm, sizeof(*lsm))) {
1100 CERROR("lsm mismatch for inode %ld\n",
1102 CERROR("lli_smd:\n");
1103 dump_lsm(D_ERROR, lli->lli_smd);
1105 dump_lsm(D_ERROR, lsm);
1109 /* bug 2844 - limit i_blksize for broken user-space apps */
1110 LASSERTF(lsm->lsm_xfersize != 0, "%lu\n", lsm->lsm_xfersize);
1111 inode->i_blksize = min(lsm->lsm_xfersize, LL_MAX_BLKSIZE);
1112 if (lli->lli_smd != lsm)
1113 obd_free_memmd(ll_i2obdexp(inode), &lsm);
1117 if (lli->lli_mea == NULL) {
1120 if (memcmp(lli->lli_mea, mea, body->eadatasize)) {
1121 CERROR("mea mismatch for inode %lu\n",
1126 if (lli->lli_mea != mea)
1127 obd_free_memmd(ll_i2mdcexp(inode),
1128 (struct lov_stripe_md **) &mea);
1132 if (body->valid & OBD_MD_FLID)
1133 inode->i_ino = body->ino;
1134 if (body->valid & OBD_MD_FLATIME)
1135 LTIME_S(inode->i_atime) = body->atime;
1136 if (body->valid & OBD_MD_FLMTIME &&
1137 body->mtime > LTIME_S(inode->i_mtime)) {
1138 CDEBUG(D_INODE, "setting ino %lu mtime from %lu to %u\n",
1139 inode->i_ino, LTIME_S(inode->i_mtime), body->mtime);
1140 LTIME_S(inode->i_mtime) = body->mtime;
1142 if (body->valid & OBD_MD_FLCTIME &&
1143 body->ctime > LTIME_S(inode->i_ctime))
1144 LTIME_S(inode->i_ctime) = body->ctime;
1145 if (body->valid & OBD_MD_FLMODE)
1146 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
1147 if (body->valid & OBD_MD_FLTYPE)
1148 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
1149 if (body->valid & OBD_MD_FLUID)
1150 inode->i_uid = body->uid;
1151 if (body->valid & OBD_MD_FLGID)
1152 inode->i_gid = body->gid;
1153 if (body->valid & OBD_MD_FLFLAGS)
1154 inode->i_flags = body->flags;
1155 if (body->valid & OBD_MD_FLNLINK)
1156 inode->i_nlink = body->nlink;
1157 if (body->valid & OBD_MD_FLGENER)
1158 inode->i_generation = body->generation;
1159 if (body->valid & OBD_MD_FLRDEV)
1160 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1161 inode->i_rdev = body->rdev;
1163 inode->i_rdev = old_decode_dev(body->rdev);
1165 if (body->valid & OBD_MD_FLSIZE)
1166 inode->i_size = body->size;
1167 if (body->valid & OBD_MD_FLBLOCKS)
1168 inode->i_blocks = body->blocks;
1170 if (body->valid & OBD_MD_FLSIZE)
1171 set_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
1173 lli->lli_mds = body->mds;
1174 inode->i_dev = (kdev_t) body->mds;
1175 LASSERT(body->mds < 1000);
1178 void ll_read_inode2(struct inode *inode, void *opaque)
1180 struct lustre_md *md = opaque;
1181 struct ll_inode_info *lli = ll_i2info(inode);
1184 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1185 inode->i_generation, inode);
1189 LASSERT(!lli->lli_smd);
1191 /* Core attributes from the MDS first. This is a new inode, and
1192 * the VFS doesn't zero times in the core inode so we have to do
1193 * it ourselves. They will be overwritten by either MDS or OST
1194 * attributes - we just need to make sure they aren't newer. */
1195 LTIME_S(inode->i_mtime) = 0;
1196 LTIME_S(inode->i_atime) = 0;
1197 LTIME_S(inode->i_ctime) = 0;
1198 ll_update_inode(inode, md);
1200 /* OIDEBUG(inode); */
1202 if (S_ISREG(inode->i_mode)) {
1203 inode->i_op = &ll_file_inode_operations;
1204 inode->i_fop = &ll_file_operations;
1205 inode->i_mapping->a_ops = &ll_aops;
1207 } else if (S_ISDIR(inode->i_mode)) {
1208 inode->i_op = &ll_dir_inode_operations;
1209 inode->i_fop = &ll_dir_operations;
1210 inode->i_mapping->a_ops = &ll_dir_aops;
1212 } else if (S_ISLNK(inode->i_mode)) {
1213 inode->i_op = &ll_fast_symlink_inode_operations;
1216 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 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1227 int ll_iocontrol(struct inode *inode, struct file *file,
1228 unsigned int cmd, unsigned long arg)
1230 struct ll_sb_info *sbi = ll_i2sbi(inode);
1231 struct ptlrpc_request *req = NULL;
1236 case EXT3_IOC_GETFLAGS: {
1238 unsigned long valid = OBD_MD_FLFLAGS;
1239 struct mds_body *body;
1241 ll_inode2fid(&fid, inode);
1242 rc = md_getattr(sbi->ll_mdc_exp, &fid, valid, 0, &req);
1244 CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1248 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1250 if (body->flags & S_APPEND)
1251 flags |= EXT3_APPEND_FL;
1252 if (body->flags & S_IMMUTABLE)
1253 flags |= EXT3_IMMUTABLE_FL;
1254 if (body->flags & S_NOATIME)
1255 flags |= EXT3_NOATIME_FL;
1257 ptlrpc_req_finished (req);
1259 RETURN(put_user(flags, (int *)arg));
1261 case EXT3_IOC_SETFLAGS: {
1262 struct mdc_op_data op_data;
1265 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1267 if (get_user(flags, (int *)arg))
1274 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
1276 memset(&attr, 0x0, sizeof(attr));
1277 attr.ia_attr_flags = flags;
1278 attr.ia_valid |= ATTR_ATTR_FLAG;
1280 rc = md_setattr(sbi->ll_mdc_exp, &op_data,
1281 &attr, NULL, 0, NULL, 0, &req);
1283 ptlrpc_req_finished(req);
1284 if (rc != -EPERM && rc != -EACCES)
1285 CERROR("md_setattr fails: rc = %d\n", rc);
1289 ptlrpc_req_finished(req);
1291 oa->o_id = lsm->lsm_object_id;
1292 oa->o_gr = lsm->lsm_object_gr;
1293 oa->o_flags = flags;
1294 oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS | OBD_MD_FLGROUP;
1296 rc = obd_setattr(sbi->ll_osc_exp, oa, lsm, NULL);
1299 if (rc != -EPERM && rc != -EACCES)
1300 CERROR("obd_setattr fails: rc = %d\n", rc);
1304 if (flags & EXT3_APPEND_FL)
1305 inode->i_flags |= S_APPEND;
1307 inode->i_flags &= ~S_APPEND;
1308 if (flags & EXT3_IMMUTABLE_FL)
1309 inode->i_flags |= S_IMMUTABLE;
1311 inode->i_flags &= ~S_IMMUTABLE;
1312 if (flags & EXT3_NOATIME_FL)
1313 inode->i_flags |= S_NOATIME;
1315 inode->i_flags &= ~S_NOATIME;
1326 void ll_umount_begin(struct super_block *sb)
1328 struct ll_sb_info *sbi = ll_s2sbi(sb);
1329 struct obd_device *obd;
1330 struct obd_ioctl_data ioc_data = { 0 };
1332 CDEBUG(D_VFSTRACE, "VFS Op:\n");
1334 obd = class_exp2obd(sbi->ll_mdc_exp);
1336 CERROR("Invalid MDC connection handle "LPX64"\n",
1337 sbi->ll_mdc_exp->exp_handle.h_cookie);
1341 obd->obd_no_recov = 1;
1342 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_mdc_exp, sizeof ioc_data,
1345 obd = class_exp2obd(sbi->ll_osc_exp);
1347 CERROR("Invalid LOV connection handle "LPX64"\n",
1348 sbi->ll_osc_exp->exp_handle.h_cookie);
1353 obd->obd_no_recov = 1;
1354 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_osc_exp, sizeof ioc_data,
1357 /* Really, we'd like to wait until there are no requests outstanding,
1358 * and then continue. For now, we just invalidate the requests,
1359 * schedule, and hope.
1366 int ll_prep_inode(struct obd_export *osc_exp, struct obd_export *mdc_exp,
1367 struct inode **inode, struct ptlrpc_request *req,
1368 int offset,struct super_block *sb)
1370 struct lustre_md md;
1373 rc = mdc_req2lustre_md(req, offset, osc_exp, mdc_exp, &md);
1378 ll_update_inode(*inode, &md);
1381 *inode = ll_iget(sb, md.body->ino, &md);
1382 if (*inode == NULL || is_bad_inode(*inode)) {
1383 /* free the lsm if we allocated one above */
1385 obd_free_memmd(osc_exp, &md.lsm);
1387 obd_free_memmd(mdc_exp,
1388 (struct lov_stripe_md**)&md.mea);
1390 CERROR("new_inode -fatal: rc %d\n", rc);