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/random.h>
28 #include <linux/version.h>
30 #include <linux/lustre_lite.h>
31 #include <linux/lustre_ha.h>
32 #include <linux/lustre_dlm.h>
33 #include <linux/lprocfs_status.h>
34 #include "llite_internal.h"
36 kmem_cache_t *ll_file_data_slab;
38 extern struct address_space_operations ll_aops;
39 extern struct address_space_operations ll_dir_aops;
40 extern struct super_operations ll_super_operations;
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_pglist_lock);
57 INIT_LIST_HEAD(&sbi->ll_pglist);
58 sbi->ll_pglist_gen = 0;
59 INIT_LIST_HEAD(&sbi->ll_conn_chain);
60 INIT_HLIST_HEAD(&sbi->ll_orphan_dentry_list);
63 generate_random_uuid(uuid);
64 class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
68 void lustre_free_sbi(struct super_block *sb)
70 struct ll_sb_info *sbi = ll_s2sbi(sb);
74 OBD_FREE(sbi, sizeof(*sbi));
79 int lustre_common_fill_super(struct super_block *sb, char *mdc, char *osc)
81 struct inode *root = 0;
82 struct ll_sb_info *sbi = ll_s2sbi(sb);
83 struct obd_device *obd;
84 struct ll_fid rootfid;
85 struct obd_statfs osfs;
86 struct ptlrpc_request *request = NULL;
87 struct lustre_handle osc_conn = {0, };
88 struct lustre_handle mdc_conn = {0, };
93 obd = class_name2obd(mdc);
95 CERROR("MDC %s: not setup or attached\n", mdc);
99 if (proc_lustre_fs_root) {
100 err = lprocfs_register_mountpoint(proc_lustre_fs_root, sb,
103 CERROR("could not register mount in /proc/lustre");
106 mdc_init_ea_size(obd, osc);
108 err = obd_connect(&mdc_conn, obd, &sbi->ll_sb_uuid);
110 CERROR("An MDS (mdc %s) is performing recovery, of which this"
111 " client is not a part. Please wait for recovery to "
112 "complete, abort, or time out.\n", mdc);
115 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
118 sbi->ll_mdc_exp = class_conn2export(&mdc_conn);
120 err = obd_statfs(obd, &osfs, jiffies - HZ);
124 LASSERT(osfs.os_bsize);
125 sb->s_blocksize = osfs.os_bsize;
126 sb->s_blocksize_bits = log2(osfs.os_bsize);
127 sb->s_magic = LL_SUPER_MAGIC;
128 sb->s_maxbytes = PAGE_CACHE_MAXBYTES;
130 devno = get_uuid2int(sbi2mdc(sbi)->cl_import->imp_target_uuid.uuid,
131 strlen(sbi2mdc(sbi)->cl_import->imp_target_uuid.uuid));
134 obd = class_name2obd(osc);
136 CERROR("OSC %s: not setup or attached\n", osc);
140 err = obd_connect(&osc_conn, obd, &sbi->ll_sb_uuid);
142 CERROR("An OST (osc %s) is performing recovery, of which this"
143 " client is not a part. Please wait for recovery to "
144 "complete, abort, or time out.\n", osc);
147 CERROR("cannot connect to %s: rc = %d\n", osc, err);
150 sbi->ll_osc_exp = class_conn2export(&osc_conn);
152 err = mdc_getstatus(sbi->ll_mdc_exp, &rootfid);
154 CERROR("cannot mds_connect: rc = %d\n", err);
157 CDEBUG(D_SUPER, "rootfid "LPU64"\n", rootfid.id);
158 sbi->ll_rootino = rootfid.id;
160 sb->s_op = &lustre_super_operations;
163 * XXX: move this to after cbd setup? */
164 err = mdc_getattr(sbi->ll_mdc_exp, &rootfid,
165 OBD_MD_FLNOTOBD|OBD_MD_FLBLOCKS, 0, &request);
167 CERROR("mdc_getattr failed for root: rc = %d\n", err);
171 err = mdc_req2lustre_md(request, 0, sbi->ll_osc_exp, &md);
173 CERROR("failed to understand root inode md: rc = %d\n",err);
174 ptlrpc_req_finished (request);
178 LASSERT(sbi->ll_rootino != 0);
179 root = ll_iget(sb, sbi->ll_rootino, &md);
181 ptlrpc_req_finished(request);
183 if (root == NULL || is_bad_inode(root)) {
184 /* XXX might need iput() for bad inode */
185 CERROR("lustre_lite: bad iget4 for root\n");
186 GOTO(out_root, err = -EBADF);
189 err = ll_close_thread_start(&sbi->ll_lcq);
191 CERROR("cannot start close thread: rc %d\n", err);
195 sb->s_root = d_alloc_root(root);
202 obd_disconnect(sbi->ll_osc_exp, 0);
204 obd_disconnect(sbi->ll_mdc_exp, 0);
206 lprocfs_unregister_mountpoint(sbi);
210 void lustre_common_put_super(struct super_block *sb)
212 struct ll_sb_info *sbi = ll_s2sbi(sb);
213 struct hlist_node *tmp, *next;
216 ll_close_thread_shutdown(sbi->ll_lcq);
218 list_del(&sbi->ll_conn_chain);
219 obd_disconnect(sbi->ll_osc_exp, 0);
221 lprocfs_unregister_mountpoint(sbi);
222 if (sbi->ll_proc_root) {
223 lprocfs_remove(sbi->ll_proc_root);
224 sbi->ll_proc_root = NULL;
227 obd_disconnect(sbi->ll_mdc_exp, 0);
229 // We do this to get rid of orphaned dentries. That is not really trw.
230 spin_lock(&dcache_lock);
231 hlist_for_each_safe(tmp, next, &sbi->ll_orphan_dentry_list) {
232 struct dentry *dentry = hlist_entry(tmp, struct dentry, d_hash);
233 shrink_dcache_parent(dentry);
235 spin_unlock(&dcache_lock);
240 char *ll_read_opt(const char *opt, char *data)
246 CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
247 if (strncmp(opt, data, strlen(opt)))
249 if ((value = strchr(data, '=')) == NULL)
253 OBD_ALLOC(retval, strlen(value) + 1);
255 CERROR("out of memory!\n");
259 memcpy(retval, value, strlen(value)+1);
260 CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
264 int ll_set_opt(const char *opt, char *data, int fl)
268 CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
269 if (strncmp(opt, data, strlen(opt)))
275 void ll_options(char *options, char **ost, char **mdc, int *flags)
278 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
279 char *opt_ptr = options;
288 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
289 for (this_char = strtok (options, ",");
291 this_char = strtok (NULL, ",")) {
293 while ((this_char = strsep (&opt_ptr, ",")) != NULL) {
295 CDEBUG(D_SUPER, "this_char %s\n", this_char);
296 if (!*ost && (*ost = ll_read_opt("osc", this_char)))
298 if (!*mdc && (*mdc = ll_read_opt("mdc", this_char)))
300 if (!(*flags & LL_SBI_NOLCK) &&
301 ((*flags) = (*flags) |
302 ll_set_opt("nolock", this_char,
309 void ll_lli_init(struct ll_inode_info *lli)
311 sema_init(&lli->lli_open_sem, 1);
313 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
314 spin_lock_init(&lli->lli_lock);
315 INIT_LIST_HEAD(&lli->lli_pending_write_llaps);
318 int ll_fill_super(struct super_block *sb, void *data, int silent)
320 struct ll_sb_info *sbi;
326 CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
328 sbi = lustre_init_sbi(sb);
332 sbi->ll_flags |= LL_SBI_READAHEAD;
333 ll_options(data, &osc, &mdc, &sbi->ll_flags);
337 GOTO(out, err = -EINVAL);
342 GOTO(out, err = -EINVAL);
345 err = lustre_common_fill_super(sb, mdc, osc);
351 OBD_FREE(mdc, strlen(mdc) + 1);
353 OBD_FREE(osc, strlen(osc) + 1);
356 } /* ll_read_super */
358 void ll_put_super(struct super_block *sb)
362 CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
364 lustre_common_put_super(sb);
371 int lustre_process_log(struct lustre_mount_data *lmd, char * profile,
372 struct config_llog_instance *cfg)
374 struct lustre_cfg lcfg;
375 struct portals_cfg pcfg;
376 char * peer = "MDS_PEER_UUID";
377 struct obd_device *obd;
378 struct lustre_handle mdc_conn = {0, };
379 struct obd_export *exp;
380 char * name = "mdc_dev";
382 struct obd_uuid mdc_uuid;
383 struct llog_ctxt *ctxt;
388 generate_random_uuid(uuid);
389 class_uuid_unparse(uuid, &mdc_uuid);
391 if (lmd->lmd_local_nid) {
392 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
393 pcfg.pcfg_nal = lmd->lmd_nal;
394 pcfg.pcfg_nid = lmd->lmd_local_nid;
395 err = kportal_nal_cmd(&pcfg);
400 if (lmd->lmd_nal == SOCKNAL) {
401 PCFG_INIT(pcfg, NAL_CMD_ADD_AUTOCONN);
402 pcfg.pcfg_nal = lmd->lmd_nal;
403 pcfg.pcfg_nid = lmd->lmd_server_nid;
404 pcfg.pcfg_id = lmd->lmd_server_ipaddr;
405 pcfg.pcfg_misc = lmd->lmd_port;
406 pcfg.pcfg_size = 8388608;
407 pcfg.pcfg_flags = 0x4; /*share*/
408 err = kportal_nal_cmd(&pcfg);
413 LCFG_INIT(lcfg, LCFG_ADD_UUID, name);
414 lcfg.lcfg_nid = lmd->lmd_server_nid;
415 lcfg.lcfg_inllen1 = strlen(peer) + 1;
416 lcfg.lcfg_inlbuf1 = peer;
417 lcfg.lcfg_nal = lmd->lmd_nal;
418 err = class_process_config(&lcfg);
420 GOTO(out_del_conn, err);
422 LCFG_INIT(lcfg, LCFG_ATTACH, name);
423 lcfg.lcfg_inlbuf1 = "mdc";
424 lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
425 lcfg.lcfg_inlbuf2 = mdc_uuid.uuid;
426 lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
427 err = class_process_config(&lcfg);
429 GOTO(out_del_uuid, err);
431 LCFG_INIT(lcfg, LCFG_SETUP, name);
432 lcfg.lcfg_inlbuf1 = lmd->lmd_mds;
433 lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
434 lcfg.lcfg_inlbuf2 = peer;
435 lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
436 err = class_process_config(&lcfg);
438 GOTO(out_detach, err);
440 obd = class_name2obd(name);
442 GOTO(out_cleanup, err = -EINVAL);
444 err = obd_connect(&mdc_conn, obd, &mdc_uuid);
446 CERROR("cannot connect to %s: rc = %d\n", lmd->lmd_mds, err);
447 GOTO(out_cleanup, err);
450 exp = class_conn2export(&mdc_conn);
452 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
453 rc = class_config_parse_llog(ctxt, profile, cfg);
455 CERROR("class_config_parse_llog failed: rc = %d\n", rc);
458 err = obd_disconnect(exp, 0);
461 LCFG_INIT(lcfg, LCFG_CLEANUP, name);
462 err = class_process_config(&lcfg);
467 LCFG_INIT(lcfg, LCFG_DETACH, name);
468 err = class_process_config(&lcfg);
473 LCFG_INIT(lcfg, LCFG_DEL_UUID, name);
474 lcfg.lcfg_inllen1 = strlen(peer) + 1;
475 lcfg.lcfg_inlbuf1 = peer;
476 err = class_process_config(&lcfg);
479 if (lmd->lmd_nal == SOCKNAL) {
480 PCFG_INIT(pcfg, NAL_CMD_DEL_AUTOCONN);
481 pcfg.pcfg_nal = lmd->lmd_nal;
482 pcfg.pcfg_nid = lmd->lmd_server_nid;
483 pcfg.pcfg_id = lmd->lmd_server_ipaddr;
484 pcfg.pcfg_flags = 1; /*share*/
485 err = kportal_nal_cmd(&pcfg);
496 int lustre_fill_super(struct super_block *sb, void *data, int silent)
498 struct lustre_mount_data * lmd = data;
499 struct ll_sb_info *sbi;
505 CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
506 sbi = lustre_init_sbi(sb);
510 sbi->ll_flags |= LL_SBI_READAHEAD;
512 if (lmd->lmd_profile) {
513 struct lustre_profile *lprof;
514 struct config_llog_instance cfg;
518 CERROR("no mds name\n");
519 GOTO(out_free, err = -EINVAL);
522 OBD_ALLOC(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
523 if (sbi->ll_lmd == NULL)
524 GOTO(out_free, err = -ENOMEM);
525 memcpy(sbi->ll_lmd, lmd, sizeof(*lmd));
527 /* generate a string unique to this super, let's try
528 the address of the super itself.*/
529 len = (sizeof(sb) * 2) + 1;
530 OBD_ALLOC(sbi->ll_instance, len);
531 if (sbi->ll_instance == NULL)
532 GOTO(out_free, err = -ENOMEM);
533 sprintf(sbi->ll_instance, "%p", sb);
535 cfg.cfg_instance = sbi->ll_instance;
536 cfg.cfg_uuid = sbi->ll_sb_uuid;
537 cfg.cfg_local_nid = lmd->lmd_local_nid;
538 err = lustre_process_log(lmd, lmd->lmd_profile, &cfg);
540 CERROR("Unable to process log: %s\n", lmd->lmd_profile);
545 lprof = class_get_profile(lmd->lmd_profile);
547 CERROR("No profile found: %s\n", lmd->lmd_profile);
548 GOTO(out_free, err = -EINVAL);
551 OBD_FREE(osc, strlen(osc) + 1);
552 OBD_ALLOC(osc, strlen(lprof->lp_osc) +
553 strlen(sbi->ll_instance) + 2);
554 sprintf(osc, "%s-%s", lprof->lp_osc, sbi->ll_instance);
557 OBD_FREE(mdc, strlen(mdc) + 1);
558 OBD_ALLOC(mdc, strlen(lprof->lp_mdc) +
559 strlen(sbi->ll_instance) + 2);
560 sprintf(mdc, "%s-%s", lprof->lp_mdc, sbi->ll_instance);
565 GOTO(out_free, err = -EINVAL);
570 GOTO(out_free, err = -EINVAL);
573 err = lustre_common_fill_super(sb, mdc, osc);
580 OBD_FREE(mdc, strlen(mdc) + 1);
582 OBD_FREE(osc, strlen(osc) + 1);
588 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
591 if (sbi->ll_instance != NULL) {
593 struct config_llog_instance cfg;
595 cfg.cfg_instance = sbi->ll_instance;
596 cfg.cfg_uuid = sbi->ll_sb_uuid;
598 OBD_ALLOC(cln_prof, len);
599 sprintf(cln_prof, "%s-clean", sbi->ll_lmd->lmd_profile);
601 err = lustre_process_log(sbi->ll_lmd, cln_prof, &cfg);
603 CERROR("Unable to process log: %s\n", cln_prof);
604 OBD_FREE(cln_prof, len);
605 OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance)+ 1);
607 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
612 } /* lustre_fill_super */
614 void lustre_put_super(struct super_block *sb)
616 struct ll_sb_info *sbi = ll_s2sbi(sb);
619 CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
621 lustre_common_put_super(sb);
623 if (sbi->ll_lmd != NULL) {
625 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
627 struct config_llog_instance cfg;
629 cfg.cfg_instance = sbi->ll_instance;
630 cfg.cfg_uuid = sbi->ll_sb_uuid;
632 OBD_ALLOC(cln_prof, len);
633 sprintf(cln_prof, "%s-clean", sbi->ll_lmd->lmd_profile);
635 err = lustre_process_log(sbi->ll_lmd, cln_prof, &cfg);
637 CERROR("Unable to process log: %s\n", cln_prof);
639 OBD_FREE(cln_prof, len);
640 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
641 OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance) + 1);
647 } /* lustre_put_super */
650 struct inode *ll_inode_from_lock(struct ldlm_lock *lock)
653 l_lock(&lock->l_resource->lr_namespace->ns_lock);
654 if (lock->l_ast_data)
655 inode = igrab(lock->l_ast_data);
658 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
662 static int null_if_equal(struct ldlm_lock *lock, void *data)
664 if (data == lock->l_ast_data)
665 lock->l_ast_data = NULL;
667 if (lock->l_req_mode != lock->l_granted_mode)
668 return LDLM_ITER_STOP;
670 return LDLM_ITER_CONTINUE;
673 void ll_clear_inode(struct inode *inode)
676 struct ll_inode_info *lli = ll_i2info(inode);
677 struct ll_sb_info *sbi = ll_i2sbi(inode);
680 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
681 inode->i_generation, inode);
683 ll_inode2fid(&fid, inode);
684 clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &(ll_i2info(inode)->lli_flags));
685 mdc_change_cbdata(sbi->ll_mdc_exp, &fid, null_if_equal, inode);
688 obd_change_cbdata(sbi->ll_osc_exp, lli->lli_smd,
689 null_if_equal, inode);
692 obd_free_memmd(sbi->ll_osc_exp, &lli->lli_smd);
696 if (lli->lli_symlink_name) {
697 OBD_FREE(lli->lli_symlink_name,
698 strlen(lli->lli_symlink_name) + 1);
699 lli->lli_symlink_name = NULL;
705 /* If this inode has objects allocated to it (lsm != NULL), then the OST
706 * object(s) determine the file size and mtime. Otherwise, the MDS will
707 * keep these values until such a time that objects are allocated for it.
708 * We do the MDS operations first, as it is checking permissions for us.
709 * We don't to the MDS RPC if there is nothing that we want to store there,
710 * otherwise there is no harm in updating mtime/atime on the MDS if we are
711 * going to do an RPC anyways.
713 * If we are doing a truncate, we will send the mtime and ctime updates
714 * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
715 * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
718 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
720 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
721 struct ll_sb_info *sbi = ll_i2sbi(inode);
722 struct ptlrpc_request *request = NULL;
723 struct mdc_op_data op_data;
724 int ia_valid = attr->ia_valid;
728 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", inode->i_ino);
729 lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_SETATTR);
731 if (ia_valid & ATTR_SIZE) {
732 if (attr->ia_size > ll_file_maxbytes(inode)) {
733 CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
734 attr->ia_size, ll_file_maxbytes(inode));
738 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
741 /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
742 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
743 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
747 /* We mark all of the fields "set" so MDS/OST does not re-set them */
748 if (attr->ia_valid & ATTR_CTIME) {
749 attr->ia_ctime = CURRENT_TIME;
750 attr->ia_valid |= ATTR_CTIME_SET;
752 if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
753 attr->ia_atime = CURRENT_TIME;
754 attr->ia_valid |= ATTR_ATIME_SET;
756 if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
757 attr->ia_mtime = CURRENT_TIME;
758 attr->ia_valid |= ATTR_MTIME_SET;
761 if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
762 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
763 LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
764 LTIME_S(CURRENT_TIME));
766 attr->ia_valid &= ~ATTR_SIZE;
768 /* If only OST attributes being set on objects, don't do MDS RPC.
769 * In that case, we need to check permissions and update the local
770 * inode ourselves so we can call obdo_from_inode() always. */
771 if (ia_valid & (lsm ? ~(ATTR_SIZE | ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
773 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
775 rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
776 attr, NULL, 0, NULL, 0, &request);
779 ptlrpc_req_finished(request);
780 if (rc != -EPERM && rc != -EACCES)
781 CERROR("mdc_setattr fails: rc = %d\n", rc);
785 rc = mdc_req2lustre_md(request, 0, sbi->ll_osc_exp, &md);
787 ptlrpc_req_finished(request);
791 /* Won't invoke vmtruncate as we already cleared ATTR_SIZE,
792 * but needed to set timestamps backwards on utime. */
793 inode_setattr(inode, attr);
794 ll_update_inode(inode, md.body, md.lsm);
795 ptlrpc_req_finished(request);
797 if (!lsm || !S_ISREG(inode->i_mode)) {
798 CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
802 /* The OST doesn't check permissions, but the alternative is
803 * a gratuitous RPC to the MDS. We already rely on the client
804 * to do read/write/truncate permission checks, so is mtime OK?
806 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
807 /* from sys_utime() */
808 if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
809 if (current->fsuid != inode->i_uid &&
810 (rc = ll_permission(inode, MAY_WRITE, NULL)) != 0)
813 /* from inode_change_ok() */
814 if (current->fsuid != inode->i_uid &&
815 !capable(CAP_FOWNER))
820 /* Won't invoke vmtruncate, as we already cleared ATTR_SIZE */
821 inode_setattr(inode, attr);
824 /* We really need to get our PW lock before we change inode->i_size.
825 * If we don't we can race with other i_size updaters on our node, like
826 * ll_file_read. We can also race with i_size propogation to other
827 * nodes through dirtying and writeback of final cached pages. This
828 * last one is especially bad for racing o_append users on other
830 if (ia_valid & ATTR_SIZE) {
831 struct ldlm_extent extent = { .start = attr->ia_size,
832 .end = OBD_OBJECT_EOF };
833 struct lustre_handle lockh = { 0 };
834 int err, ast_flags = 0;
835 /* XXX when we fix the AST intents to pass the discard-range
836 * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
838 if (attr->ia_size == 0)
839 ast_flags = LDLM_AST_DISCARD_DATA;
841 /* bug 1639: avoid write/truncate i_sem/DLM deadlock */
842 LASSERT(atomic_read(&inode->i_sem.count) <= 0);
844 rc = ll_extent_lock_no_validate(NULL, inode, lsm, LCK_PW,
845 &extent, &lockh, ast_flags);
850 rc = vmtruncate(inode, attr->ia_size);
852 set_bit(LLI_F_HAVE_OST_SIZE_LOCK,
853 &ll_i2info(inode)->lli_flags);
855 //ll_try_done_writing(inode);
857 /* unlock now as we don't mind others file lockers racing with
858 * the mds updates below? */
859 err = ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
861 CERROR("ll_extent_unlock failed: %d\n", err);
865 } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
868 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
869 inode->i_ino, LTIME_S(attr->ia_mtime));
870 oa.o_id = lsm->lsm_object_id;
871 oa.o_valid = OBD_MD_FLID;
872 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
873 OBD_MD_FLMTIME | OBD_MD_FLCTIME);
874 rc = obd_setattr(sbi->ll_osc_exp, &oa, lsm, NULL);
876 CERROR("obd_setattr fails: rc=%d\n", rc);
881 int ll_setattr(struct dentry *de, struct iattr *attr)
883 LBUG(); /* code is unused, but leave this in case of VFS changes */
887 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
888 unsigned long max_age)
890 struct ll_sb_info *sbi = ll_s2sbi(sb);
891 struct obd_statfs obd_osfs;
895 rc = obd_statfs(class_exp2obd(sbi->ll_mdc_exp), osfs, max_age);
897 CERROR("mdc_statfs fails: rc = %d\n", rc);
901 CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
902 osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
904 rc = obd_statfs(class_exp2obd(sbi->ll_osc_exp), &obd_osfs, max_age);
906 CERROR("obd_statfs fails: rc = %d\n", rc);
910 CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
911 obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
914 osfs->os_blocks = obd_osfs.os_blocks;
915 osfs->os_bfree = obd_osfs.os_bfree;
916 osfs->os_bavail = obd_osfs.os_bavail;
918 /* If we don't have as many objects free on the OST as inodes
919 * on the MDS, we reduce the total number of inodes to
920 * compensate, so that the "inodes in use" number is correct.
922 if (obd_osfs.os_ffree < osfs->os_ffree) {
923 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
925 osfs->os_ffree = obd_osfs.os_ffree;
931 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
933 struct obd_statfs osfs;
936 CDEBUG(D_VFSTRACE, "VFS Op:\n");
937 lprocfs_counter_incr(ll_s2sbi(sb)->ll_stats, LPROC_LL_STAFS);
939 /* For now we will always get up-to-date statfs values, but in the
940 * future we may allow some amount of caching on the client (e.g.
941 * from QOS or lprocfs updates). */
942 rc = ll_statfs_internal(sb, &osfs, jiffies - 1);
946 statfs_unpack(sfs, &osfs);
948 if (sizeof(sfs->f_blocks) == 4) {
949 while (osfs.os_blocks > ~0UL) {
952 osfs.os_blocks >>= 1;
954 osfs.os_bavail >>= 1;
958 sfs->f_blocks = osfs.os_blocks;
959 sfs->f_bfree = osfs.os_bfree;
960 sfs->f_bavail = osfs.os_bavail;
965 void dump_lsm(int level, struct lov_stripe_md *lsm)
967 CDEBUG(level, "objid "LPX64", maxbytes "LPX64", magic 0x%08X, "
968 "stripe_size %u, stripe_count %u\n",
969 lsm->lsm_object_id, lsm->lsm_maxbytes, lsm->lsm_magic,
970 lsm->lsm_stripe_size, lsm->lsm_stripe_count);
973 void ll_update_inode(struct inode *inode, struct mds_body *body,
974 struct lov_stripe_md *lsm)
976 struct ll_inode_info *lli = ll_i2info(inode);
978 LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
980 if (lli->lli_smd == NULL) {
982 lli->lli_maxbytes = lsm->lsm_maxbytes;
983 if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
984 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
986 if (memcmp(lli->lli_smd, lsm, sizeof(*lsm))) {
987 CERROR("lsm mismatch for inode %ld\n",
989 CERROR("lli_smd:\n");
990 dump_lsm(D_ERROR, lli->lli_smd);
992 dump_lsm(D_ERROR, lsm);
996 if (lli->lli_smd != lsm)
997 obd_free_memmd(ll_i2obdexp(inode), &lsm);
1000 if (body->valid & OBD_MD_FLID)
1001 inode->i_ino = body->ino;
1002 if (body->valid & OBD_MD_FLATIME)
1003 LTIME_S(inode->i_atime) = body->atime;
1004 if (body->valid & OBD_MD_FLMTIME &&
1005 body->mtime > LTIME_S(inode->i_mtime)) {
1006 CDEBUG(D_INODE, "setting ino %lu mtime from %lu to %u\n",
1007 inode->i_ino, LTIME_S(inode->i_mtime), body->mtime);
1008 LTIME_S(inode->i_mtime) = body->mtime;
1010 if (body->valid & OBD_MD_FLCTIME &&
1011 body->ctime > LTIME_S(inode->i_ctime))
1012 LTIME_S(inode->i_ctime) = body->ctime;
1013 if (body->valid & OBD_MD_FLMODE)
1014 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
1015 if (body->valid & OBD_MD_FLTYPE)
1016 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
1017 if (body->valid & OBD_MD_FLUID)
1018 inode->i_uid = body->uid;
1019 if (body->valid & OBD_MD_FLGID)
1020 inode->i_gid = body->gid;
1021 if (body->valid & OBD_MD_FLFLAGS)
1022 inode->i_flags = body->flags;
1023 if (body->valid & OBD_MD_FLNLINK)
1024 inode->i_nlink = body->nlink;
1025 if (body->valid & OBD_MD_FLGENER)
1026 inode->i_generation = body->generation;
1027 if (body->valid & OBD_MD_FLRDEV)
1028 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1029 inode->i_rdev = body->rdev;
1031 inode->i_rdev = old_decode_dev(body->rdev);
1033 if (body->valid & OBD_MD_FLSIZE)
1034 inode->i_size = body->size;
1035 if (body->valid & OBD_MD_FLBLOCKS)
1036 inode->i_blocks = body->blocks;
1038 if (body->valid & OBD_MD_FLSIZE)
1039 set_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
1042 void ll_read_inode2(struct inode *inode, void *opaque)
1044 struct lustre_md *md = opaque;
1045 struct ll_inode_info *lli = ll_i2info(inode);
1048 CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1049 inode->i_generation, inode);
1053 LASSERT(!lli->lli_smd);
1055 /* Core attributes from the MDS first. This is a new inode, and
1056 * the VFS doesn't zero times in the core inode so we have to do
1057 * it ourselves. They will be overwritten by either MDS or OST
1058 * attributes - we just need to make sure they aren't newer. */
1059 LTIME_S(inode->i_mtime) = 0;
1060 LTIME_S(inode->i_atime) = 0;
1061 LTIME_S(inode->i_ctime) = 0;
1062 ll_update_inode(inode, md->body, md->lsm);
1064 /* OIDEBUG(inode); */
1066 if (S_ISREG(inode->i_mode)) {
1067 inode->i_op = &ll_file_inode_operations;
1068 inode->i_fop = &ll_file_operations;
1069 inode->i_mapping->a_ops = &ll_aops;
1071 } else if (S_ISDIR(inode->i_mode)) {
1072 inode->i_op = &ll_dir_inode_operations;
1073 inode->i_fop = &ll_dir_operations;
1074 inode->i_mapping->a_ops = &ll_dir_aops;
1076 } else if (S_ISLNK(inode->i_mode)) {
1077 inode->i_op = &ll_fast_symlink_inode_operations;
1080 inode->i_op = &ll_special_inode_operations;
1081 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1082 init_special_inode(inode, inode->i_mode,
1083 kdev_t_to_nr(inode->i_rdev));
1085 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1091 int ll_iocontrol(struct inode *inode, struct file *file,
1092 unsigned int cmd, unsigned long arg)
1094 struct ll_sb_info *sbi = ll_i2sbi(inode);
1095 struct ptlrpc_request *req = NULL;
1100 case EXT3_IOC_GETFLAGS: {
1102 unsigned long valid = OBD_MD_FLFLAGS;
1103 struct mds_body *body;
1105 ll_inode2fid(&fid, inode);
1106 rc = mdc_getattr(sbi->ll_mdc_exp, &fid, valid, 0, &req);
1108 CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1112 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1114 if (body->flags & S_APPEND)
1115 flags |= EXT3_APPEND_FL;
1116 if (body->flags & S_IMMUTABLE)
1117 flags |= EXT3_IMMUTABLE_FL;
1118 if (body->flags & S_NOATIME)
1119 flags |= EXT3_NOATIME_FL;
1121 ptlrpc_req_finished (req);
1123 RETURN(put_user(flags, (int *)arg));
1125 case EXT3_IOC_SETFLAGS: {
1126 struct mdc_op_data op_data;
1129 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1131 if (get_user(flags, (int *)arg))
1138 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
1140 memset(&attr, 0x0, sizeof(attr));
1141 attr.ia_attr_flags = flags;
1142 attr.ia_valid |= ATTR_ATTR_FLAG;
1144 rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
1145 &attr, NULL, 0, NULL, 0, &req);
1147 ptlrpc_req_finished(req);
1148 if (rc != -EPERM && rc != -EACCES)
1149 CERROR("mdc_setattr fails: rc = %d\n", rc);
1153 ptlrpc_req_finished(req);
1155 oa->o_id = lsm->lsm_object_id;
1156 oa->o_flags = flags;
1157 oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS;
1159 rc = obd_setattr(sbi->ll_osc_exp, oa, lsm, NULL);
1162 if (rc != -EPERM && rc != -EACCES)
1163 CERROR("mdc_setattr fails: rc = %d\n", rc);
1167 if (flags & EXT3_APPEND_FL)
1168 inode->i_flags |= S_APPEND;
1170 inode->i_flags &= ~S_APPEND;
1171 if (flags & EXT3_IMMUTABLE_FL)
1172 inode->i_flags |= S_IMMUTABLE;
1174 inode->i_flags &= ~S_IMMUTABLE;
1175 if (flags & EXT3_NOATIME_FL)
1176 inode->i_flags |= S_NOATIME;
1178 inode->i_flags &= ~S_NOATIME;
1189 void ll_umount_begin(struct super_block *sb)
1191 struct ll_sb_info *sbi = ll_s2sbi(sb);
1192 struct obd_device *obd;
1193 struct obd_ioctl_data ioc_data = { 0 };
1195 CDEBUG(D_VFSTRACE, "VFS Op:\n");
1197 obd = class_exp2obd(sbi->ll_mdc_exp);
1199 CERROR("Invalid MDC connection handle "LPX64"\n",
1200 sbi->ll_mdc_exp->exp_handle.h_cookie);
1204 obd->obd_no_recov = 1;
1205 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_mdc_exp, sizeof ioc_data,
1208 obd = class_exp2obd(sbi->ll_osc_exp);
1210 CERROR("Invalid LOV connection handle "LPX64"\n",
1211 sbi->ll_osc_exp->exp_handle.h_cookie);
1216 obd->obd_no_recov = 1;
1217 obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_osc_exp, sizeof ioc_data,
1220 /* Really, we'd like to wait until there are no requests outstanding,
1221 * and then continue. For now, we just invalidate the requests,
1222 * schedule, and hope.
1229 int ll_prep_inode(struct obd_export *exp, struct inode **inode,
1230 struct ptlrpc_request *req, int offset,struct super_block *sb)
1232 struct lustre_md md;
1235 rc = mdc_req2lustre_md(req, offset, exp, &md);
1240 ll_update_inode(*inode, md.body, md.lsm);
1243 *inode = ll_iget(sb, md.body->ino, &md);
1244 if (*inode == NULL || is_bad_inode(*inode)) {
1245 /* free the lsm if we allocated one above */
1247 obd_free_memmd(exp, &md.lsm);
1249 CERROR("new_inode -fatal: rc %d\n", rc);