Whamcloud - gitweb
r=adilger,phil
[fs/lustre-release.git] / lustre / llite / llite_lib.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Light Super operations
5  *
6  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
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.
13  *
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.
18  *
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.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LLITE
25
26 #include <linux/module.h>
27 #include <linux/random.h>
28 #include <linux/version.h>
29
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"
35
36 kmem_cache_t *ll_file_data_slab;
37
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;
41
42 #ifndef log2
43 #define log2(n) ffz(~(n))
44 #endif
45
46 struct ll_sb_info *lustre_init_sbi(struct super_block *sb)
47 {
48         struct ll_sb_info *sbi = NULL;
49         class_uuid_t uuid;
50         ENTRY;
51
52         OBD_ALLOC(sbi, sizeof(*sbi));
53         if (!sbi)
54                 RETURN(NULL);
55
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);
61         ll_s2sbi(sb) = sbi;
62
63         generate_random_uuid(uuid);
64         class_uuid_unparse(uuid, &sbi->ll_sb_uuid);
65         RETURN(sbi);
66 }
67
68 void lustre_free_sbi(struct super_block *sb)
69 {
70         struct ll_sb_info *sbi = ll_s2sbi(sb);
71         ENTRY;
72
73         if (sbi != NULL)
74                 OBD_FREE(sbi, sizeof(*sbi));
75         ll_s2sbi(sb) = NULL;
76         EXIT;
77 }
78
79 int lustre_common_fill_super(struct super_block *sb, char *mdc, char *osc)
80 {
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, };
89         struct lustre_md md;
90         kdev_t devno;
91         int err;
92
93         obd = class_name2obd(mdc);
94         if (!obd) {
95                 CERROR("MDC %s: not setup or attached\n", mdc);
96                 RETURN(-EINVAL);
97         }
98
99         if (proc_lustre_fs_root) {
100                 err = lprocfs_register_mountpoint(proc_lustre_fs_root, sb,
101                                                   osc, mdc);
102                 if (err < 0)
103                         CERROR("could not register mount in /proc/lustre");
104         }
105
106         mdc_init_ea_size(obd, osc);
107
108         err = obd_connect(&mdc_conn, obd, &sbi->ll_sb_uuid);
109         if (err == -EBUSY) {
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);
113                 GOTO(out, err);
114         } else if (err) {
115                 CERROR("cannot connect to %s: rc = %d\n", mdc, err);
116                 GOTO(out, err);
117         }
118         sbi->ll_mdc_exp = class_conn2export(&mdc_conn);
119
120         err = obd_statfs(obd, &osfs, jiffies - HZ);
121         if (err)
122                 GOTO(out_mdc, err);
123
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;
129
130         devno = get_uuid2int(sbi2mdc(sbi)->cl_import->imp_target_uuid.uuid,
131                              strlen(sbi2mdc(sbi)->cl_import->imp_target_uuid.uuid));
132         sb->s_dev = devno;
133
134         obd = class_name2obd(osc);
135         if (!obd) {
136                 CERROR("OSC %s: not setup or attached\n", osc);
137                 GOTO(out_mdc, err);
138         }
139
140         err = obd_connect(&osc_conn, obd, &sbi->ll_sb_uuid);
141         if (err == -EBUSY) {
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);
145                 GOTO(out, err);
146         } else if (err) {
147                 CERROR("cannot connect to %s: rc = %d\n", osc, err);
148                 GOTO(out_mdc, err);
149         }
150         sbi->ll_osc_exp = class_conn2export(&osc_conn);
151
152         err = mdc_getstatus(sbi->ll_mdc_exp, &rootfid);
153         if (err) {
154                 CERROR("cannot mds_connect: rc = %d\n", err);
155                 GOTO(out_osc, err);
156         }
157         CDEBUG(D_SUPER, "rootfid "LPU64"\n", rootfid.id);
158         sbi->ll_rootino = rootfid.id;
159
160         sb->s_op = &lustre_super_operations;
161
162         /* make root inode
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);
166         if (err) {
167                 CERROR("mdc_getattr failed for root: rc = %d\n", err);
168                 GOTO(out_osc, err);
169         }
170
171         err = mdc_req2lustre_md(request, 0, sbi->ll_osc_exp, &md);
172         if (err) {
173                 CERROR("failed to understand root inode md: rc = %d\n",err);
174                 ptlrpc_req_finished (request);
175                 GOTO(out_osc, err);
176         }
177
178         LASSERT(sbi->ll_rootino != 0);
179         root = ll_iget(sb, sbi->ll_rootino, &md);
180
181         ptlrpc_req_finished(request);
182
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);
187         }
188
189         err = ll_close_thread_start(&sbi->ll_lcq);
190         if (err) {
191                 CERROR("cannot start close thread: rc %d\n", err);
192                 GOTO(out_root, err);
193         }
194
195         /* bug 2805 - set VM readahead to zero */
196         vm_max_readahead = vm_min_readahead = 0;
197         sb->s_root = d_alloc_root(root);
198         RETURN(err);
199
200 out_root:
201         if (root)
202                 iput(root);
203 out_osc:
204         obd_disconnect(sbi->ll_osc_exp, 0);
205 out_mdc:
206         obd_disconnect(sbi->ll_mdc_exp, 0);
207 out:
208         lprocfs_unregister_mountpoint(sbi);
209         RETURN(err);
210 }
211
212 void lustre_common_put_super(struct super_block *sb)
213 {
214         struct ll_sb_info *sbi = ll_s2sbi(sb);
215         struct hlist_node *tmp, *next;
216         ENTRY;
217
218         ll_close_thread_shutdown(sbi->ll_lcq);
219
220         list_del(&sbi->ll_conn_chain);
221         obd_disconnect(sbi->ll_osc_exp, 0);
222
223         lprocfs_unregister_mountpoint(sbi);
224         if (sbi->ll_proc_root) {
225                 lprocfs_remove(sbi->ll_proc_root);
226                 sbi->ll_proc_root = NULL;
227         }
228
229         obd_disconnect(sbi->ll_mdc_exp, 0);
230
231         // We do this to get rid of orphaned dentries. That is not really trw.
232         spin_lock(&dcache_lock);
233         hlist_for_each_safe(tmp, next, &sbi->ll_orphan_dentry_list) {
234                 struct dentry *dentry = hlist_entry(tmp, struct dentry, d_hash);
235                 shrink_dcache_parent(dentry);
236         }
237         spin_unlock(&dcache_lock);
238         EXIT;
239 }
240
241
242 char *ll_read_opt(const char *opt, char *data)
243 {
244         char *value;
245         char *retval;
246         ENTRY;
247
248         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
249         if (strncmp(opt, data, strlen(opt)))
250                 RETURN(NULL);
251         if ((value = strchr(data, '=')) == NULL)
252                 RETURN(NULL);
253
254         value++;
255         OBD_ALLOC(retval, strlen(value) + 1);
256         if (!retval) {
257                 CERROR("out of memory!\n");
258                 RETURN(NULL);
259         }
260
261         memcpy(retval, value, strlen(value)+1);
262         CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
263         RETURN(retval);
264 }
265
266 int ll_set_opt(const char *opt, char *data, int fl)
267 {
268         ENTRY;
269
270         CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
271         if (strncmp(opt, data, strlen(opt)))
272                 RETURN(0);
273         else
274                 RETURN(fl);
275 }
276
277 void ll_options(char *options, char **ost, char **mdc, int *flags)
278 {
279         char *this_char;
280 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
281         char *opt_ptr = options;
282 #endif
283         ENTRY;
284
285         if (!options) {
286                 EXIT;
287                 return;
288         }
289
290 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
291         for (this_char = strtok (options, ",");
292              this_char != NULL;
293              this_char = strtok (NULL, ",")) {
294 #else
295         while ((this_char = strsep (&opt_ptr, ",")) != NULL) {
296 #endif
297                 CDEBUG(D_SUPER, "this_char %s\n", this_char);
298                 if (!*ost && (*ost = ll_read_opt("osc", this_char)))
299                         continue;
300                 if (!*mdc && (*mdc = ll_read_opt("mdc", this_char)))
301                         continue;
302                 if (!(*flags & LL_SBI_NOLCK) &&
303                     ((*flags) = (*flags) |
304                                 ll_set_opt("nolock", this_char,
305                                            LL_SBI_NOLCK)))
306                         continue;
307         }
308         EXIT;
309 }
310
311 void ll_lli_init(struct ll_inode_info *lli)
312 {
313         sema_init(&lli->lli_open_sem, 1);
314         lli->lli_flags = 0;
315         lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
316         spin_lock_init(&lli->lli_lock);
317         INIT_LIST_HEAD(&lli->lli_pending_write_llaps);
318 }
319
320 int ll_fill_super(struct super_block *sb, void *data, int silent)
321 {
322         struct ll_sb_info *sbi;
323         char *osc = NULL;
324         char *mdc = NULL;
325         int err;
326         ENTRY;
327
328         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
329
330         sbi = lustre_init_sbi(sb);
331         if (!sbi)
332                 RETURN(-ENOMEM);
333
334         sbi->ll_flags |= LL_SBI_READAHEAD;
335         ll_options(data, &osc, &mdc, &sbi->ll_flags);
336
337         if (!osc) {
338                 CERROR("no osc\n");
339                 GOTO(out, err = -EINVAL);
340         }
341
342         if (!mdc) {
343                 CERROR("no mdc\n");
344                 GOTO(out, err = -EINVAL);
345         }
346
347         err = lustre_common_fill_super(sb, mdc, osc);
348 out:
349         if (err)
350                 lustre_free_sbi(sb);
351
352         if (mdc)
353                 OBD_FREE(mdc, strlen(mdc) + 1);
354         if (osc)
355                 OBD_FREE(osc, strlen(osc) + 1);
356
357         RETURN(err);
358 } /* ll_read_super */
359
360 void ll_put_super(struct super_block *sb)
361 {
362         ENTRY;
363
364         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
365
366         lustre_common_put_super(sb);
367
368         lustre_free_sbi(sb);
369
370         EXIT;
371 } /* ll_put_super */
372
373 int lustre_process_log(struct lustre_mount_data *lmd, char * profile,
374                        struct config_llog_instance *cfg, int allow_recov)
375 {
376         struct lustre_cfg lcfg;
377         struct portals_cfg pcfg;
378         char * peer = "MDS_PEER_UUID";
379         struct obd_device *obd;
380         struct lustre_handle mdc_conn = {0, };
381         struct obd_export *exp;
382         char * name = "mdc_dev";
383         class_uuid_t uuid;
384         struct obd_uuid mdc_uuid;
385         struct llog_ctxt *ctxt;
386         int rc = 0;
387         int err;
388         ENTRY;
389
390         if (lmd_bad_magic(lmd))
391                 RETURN(-EINVAL);
392
393         generate_random_uuid(uuid);
394         class_uuid_unparse(uuid, &mdc_uuid);
395
396         if (lmd->lmd_local_nid) {
397                 PCFG_INIT(pcfg, NAL_CMD_REGISTER_MYNID);
398                 pcfg.pcfg_nal = lmd->lmd_nal;
399                 pcfg.pcfg_nid = lmd->lmd_local_nid;
400                 err = kportal_nal_cmd(&pcfg);
401                 if (err <0)
402                         GOTO(out, err);
403         }
404
405         if (lmd->lmd_nal == SOCKNAL) {
406                 PCFG_INIT(pcfg, NAL_CMD_ADD_AUTOCONN);
407                 pcfg.pcfg_nal     = lmd->lmd_nal;
408                 pcfg.pcfg_nid     = lmd->lmd_server_nid;
409                 pcfg.pcfg_id      = lmd->lmd_server_ipaddr;
410                 pcfg.pcfg_misc    = lmd->lmd_port;
411                 pcfg.pcfg_size    = 8388608;
412                 pcfg.pcfg_flags   = 0x4; /*share*/
413                 err = kportal_nal_cmd(&pcfg);
414                 if (err <0)
415                         GOTO(out, err);
416         }
417
418         LCFG_INIT(lcfg, LCFG_ADD_UUID, name);
419         lcfg.lcfg_nid = lmd->lmd_server_nid;
420         lcfg.lcfg_inllen1 = strlen(peer) + 1;
421         lcfg.lcfg_inlbuf1 = peer;
422         lcfg.lcfg_nal = lmd->lmd_nal;
423         err = class_process_config(&lcfg);
424         if (err < 0)
425                 GOTO(out_del_conn, err);
426
427         LCFG_INIT(lcfg, LCFG_ATTACH, name);
428         lcfg.lcfg_inlbuf1 = "mdc";
429         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
430         lcfg.lcfg_inlbuf2 = mdc_uuid.uuid;
431         lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
432         err = class_process_config(&lcfg);
433         if (err < 0)
434                 GOTO(out_del_uuid, err);
435
436         LCFG_INIT(lcfg, LCFG_SETUP, name);
437         lcfg.lcfg_inlbuf1 = lmd->lmd_mds;
438         lcfg.lcfg_inllen1 = strlen(lcfg.lcfg_inlbuf1) + 1;
439         lcfg.lcfg_inlbuf2 = peer;
440         lcfg.lcfg_inllen2 = strlen(lcfg.lcfg_inlbuf2) + 1;
441         err = class_process_config(&lcfg);
442         if (err < 0)
443                 GOTO(out_detach, err);
444
445         obd = class_name2obd(name);
446         if (obd == NULL)
447                 GOTO(out_cleanup, err = -EINVAL);
448
449         /* Disable initial recovery on this import */
450         err = obd_set_info(obd->obd_self_export,
451                            strlen("initial_recov"), "initial_recov",
452                            sizeof(allow_recov), &allow_recov);
453         if (err)
454                 GOTO(out_cleanup, err);
455
456         err = obd_connect(&mdc_conn, obd, &mdc_uuid);
457         if (err) {
458                 CERROR("cannot connect to %s: rc = %d\n", lmd->lmd_mds, err);
459                 GOTO(out_cleanup, err);
460         }
461
462         exp = class_conn2export(&mdc_conn);
463
464         ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
465 #if 1
466         rc = class_config_parse_llog(ctxt, profile, cfg);
467 #else
468         /*
469          * For debugging, it's useful to just dump the log
470          */
471         rc = class_config_dump_llog(ctxt, profile, cfg);
472 #endif
473         if (rc) {
474                 CERROR("class_config_parse_llog failed: rc = %d\n", rc);
475         }
476
477         err = obd_disconnect(exp, 0);
478
479 out_cleanup:
480         LCFG_INIT(lcfg, LCFG_CLEANUP, name);
481         err = class_process_config(&lcfg);
482         if (err < 0)
483                 GOTO(out, err);
484
485 out_detach:
486         LCFG_INIT(lcfg, LCFG_DETACH, name);
487         err = class_process_config(&lcfg);
488         if (err < 0)
489                 GOTO(out, err);
490
491 out_del_uuid:
492         LCFG_INIT(lcfg, LCFG_DEL_UUID, name);
493         lcfg.lcfg_inllen1 = strlen(peer) + 1;
494         lcfg.lcfg_inlbuf1 = peer;
495         err = class_process_config(&lcfg);
496
497 out_del_conn:
498         if (lmd->lmd_nal == SOCKNAL) {
499                 PCFG_INIT(pcfg, NAL_CMD_DEL_AUTOCONN);
500                 pcfg.pcfg_nal     = lmd->lmd_nal;
501                 pcfg.pcfg_nid     = lmd->lmd_server_nid;
502                 pcfg.pcfg_id      = lmd->lmd_server_ipaddr;
503                 pcfg.pcfg_flags   = 1; /*share*/
504                 err = kportal_nal_cmd(&pcfg);
505                 if (err <0)
506                         GOTO(out, err);
507         }
508 out:
509         if (rc == 0)
510                 rc = err;
511
512         RETURN(rc);
513 }
514
515 int lustre_fill_super(struct super_block *sb, void *data, int silent)
516 {
517         struct lustre_mount_data * lmd = data;
518         struct ll_sb_info *sbi;
519         char *osc = NULL;
520         char *mdc = NULL;
521         int err;
522         ENTRY;
523
524         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
525         if (lmd_bad_magic(lmd))
526                 RETURN(-EINVAL);
527
528         sbi = lustre_init_sbi(sb);
529         if (!sbi)
530                 RETURN(-ENOMEM);
531
532         sbi->ll_flags |= LL_SBI_READAHEAD;
533
534         if (lmd->lmd_profile) {
535                 struct lustre_profile *lprof;
536                 struct config_llog_instance cfg;
537                 int len;
538
539                 if (!lmd->lmd_mds) {
540                         CERROR("no mds name\n");
541                         GOTO(out_free, err = -EINVAL);
542                 }
543
544                 OBD_ALLOC(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
545                 if (sbi->ll_lmd == NULL)
546                         GOTO(out_free, err = -ENOMEM);
547                 memcpy(sbi->ll_lmd, lmd, sizeof(*lmd));
548
549                 /* generate a string unique to this super, let's try
550                  the address of the super itself.*/
551                 len = (sizeof(sb) * 2) + 1;
552                 OBD_ALLOC(sbi->ll_instance, len);
553                 if (sbi->ll_instance == NULL)
554                         GOTO(out_free, err = -ENOMEM);
555                 sprintf(sbi->ll_instance, "%p", sb);
556
557                 cfg.cfg_instance = sbi->ll_instance;
558                 cfg.cfg_uuid = sbi->ll_sb_uuid;
559                 cfg.cfg_local_nid = lmd->lmd_local_nid;
560                 err = lustre_process_log(lmd, lmd->lmd_profile, &cfg, 0);
561                 if (err < 0) {
562                         CERROR("Unable to process log: %s\n", lmd->lmd_profile);
563
564                         GOTO(out_free, err);
565                 }
566
567                 lprof = class_get_profile(lmd->lmd_profile);
568                 if (lprof == NULL) {
569                         CERROR("No profile found: %s\n", lmd->lmd_profile);
570                         GOTO(out_free, err = -EINVAL);
571                 }
572                 if (osc)
573                         OBD_FREE(osc, strlen(osc) + 1);
574                 OBD_ALLOC(osc, strlen(lprof->lp_osc) +
575                           strlen(sbi->ll_instance) + 2);
576                 sprintf(osc, "%s-%s", lprof->lp_osc, sbi->ll_instance);
577
578                 if (mdc)
579                         OBD_FREE(mdc, strlen(mdc) + 1);
580                 OBD_ALLOC(mdc, strlen(lprof->lp_mdc) +
581                           strlen(sbi->ll_instance) + 2);
582                 sprintf(mdc, "%s-%s", lprof->lp_mdc, sbi->ll_instance);
583         }
584
585         if (!osc) {
586                 CERROR("no osc\n");
587                 GOTO(out_free, err = -EINVAL);
588         }
589
590         if (!mdc) {
591                 CERROR("no mdc\n");
592                 GOTO(out_free, err = -EINVAL);
593         }
594
595         err = lustre_common_fill_super(sb, mdc, osc);
596
597         if (err)
598                 GOTO(out_free, err);
599
600 out_dev:
601         if (mdc)
602                 OBD_FREE(mdc, strlen(mdc) + 1);
603         if (osc)
604                 OBD_FREE(osc, strlen(osc) + 1);
605
606         RETURN(err);
607
608 out_free:
609         if (sbi->ll_lmd) {
610                 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
611                 int err;
612
613                 if (sbi->ll_instance != NULL) {
614                         char * cln_prof;
615                         struct config_llog_instance cfg;
616
617                         cfg.cfg_instance = sbi->ll_instance;
618                         cfg.cfg_uuid = sbi->ll_sb_uuid;
619
620                         OBD_ALLOC(cln_prof, len);
621                         sprintf(cln_prof, "%s-clean", sbi->ll_lmd->lmd_profile);
622
623                         err = lustre_process_log(sbi->ll_lmd, cln_prof, &cfg,
624                                                  0);
625                         if (err < 0)
626                                 CERROR("Unable to process log: %s\n", cln_prof);
627                         OBD_FREE(cln_prof, len);
628                         OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance)+ 1);
629                 }
630                 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
631         }
632         lustre_free_sbi(sb);
633
634         goto out_dev;
635 } /* lustre_fill_super */
636
637 static void lustre_manual_cleanup(struct ll_sb_info *sbi)
638 {
639         struct lustre_cfg lcfg;
640         struct obd_device *obd;
641         int next = 0;
642
643         while ((obd = class_devices_in_group(&sbi->ll_sb_uuid, &next)) != NULL)
644         {
645                 int err;
646
647                 LCFG_INIT(lcfg, LCFG_CLEANUP, obd->obd_name);
648                 err = class_process_config(&lcfg);
649                 if (err) {
650                         CERROR("cleanup failed: %s\n", obd->obd_name);
651                         //continue;
652                 }
653
654                 LCFG_INIT(lcfg, LCFG_DETACH, obd->obd_name);
655                 err = class_process_config(&lcfg);
656                 if (err) {
657                         CERROR("detach failed: %s\n", obd->obd_name);
658                         //continue;
659                 }
660         }
661
662         if (sbi->ll_lmd != NULL)
663                 class_del_profile(sbi->ll_lmd->lmd_profile);
664 }
665
666 void lustre_put_super(struct super_block *sb)
667 {
668         struct obd_device *obd;
669         struct ll_sb_info *sbi = ll_s2sbi(sb);
670         int force_umount = 0;
671         ENTRY;
672
673         CDEBUG(D_VFSTRACE, "VFS Op: sb %p\n", sb);
674         obd = class_exp2obd(sbi->ll_mdc_exp);
675         if (obd)
676                 force_umount = obd->obd_no_recov;
677         obd = NULL;
678
679         lustre_common_put_super(sb);
680
681         if (sbi->ll_lmd != NULL) {
682                 char * cln_prof;
683                 int len = strlen(sbi->ll_lmd->lmd_profile) + sizeof("-clean")+1;
684                 int err;
685                 struct config_llog_instance cfg;
686
687                 if (force_umount) {
688                         CERROR("force umount, doing manual cleanup\n");
689                         lustre_manual_cleanup(sbi);
690                         GOTO(free_lmd, 0);
691                 }
692
693                 cfg.cfg_instance = sbi->ll_instance;
694                 cfg.cfg_uuid = sbi->ll_sb_uuid;
695
696                 OBD_ALLOC(cln_prof, len);
697                 sprintf(cln_prof, "%s-clean", sbi->ll_lmd->lmd_profile);
698
699                 err = lustre_process_log(sbi->ll_lmd, cln_prof, &cfg, 0);
700                 if (err < 0) {
701                         CERROR("Unable to process log: %s, doing manual cleanup"
702                                "\n", cln_prof);
703                         lustre_manual_cleanup(sbi);
704                 }
705
706                 OBD_FREE(cln_prof, len);
707         free_lmd:
708                 OBD_FREE(sbi->ll_lmd, sizeof(*sbi->ll_lmd));
709                 OBD_FREE(sbi->ll_instance, strlen(sbi->ll_instance) + 1);
710         }
711
712         lustre_free_sbi(sb);
713
714         EXIT;
715 } /* lustre_put_super */
716
717
718 struct inode *ll_inode_from_lock(struct ldlm_lock *lock)
719 {
720         struct inode *inode;
721         l_lock(&lock->l_resource->lr_namespace->ns_lock);
722         if (lock->l_ast_data)
723                 inode = igrab(lock->l_ast_data);
724         else
725                 inode = NULL;
726         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
727         return inode;
728 }
729
730 static int null_if_equal(struct ldlm_lock *lock, void *data)
731 {
732         if (data == lock->l_ast_data)
733                 lock->l_ast_data = NULL;
734
735         if (lock->l_req_mode != lock->l_granted_mode)
736                 return LDLM_ITER_STOP;
737
738         return LDLM_ITER_CONTINUE;
739 }
740
741 void ll_clear_inode(struct inode *inode)
742 {
743         struct ll_fid fid;
744         struct ll_inode_info *lli = ll_i2info(inode);
745         struct ll_sb_info *sbi = ll_i2sbi(inode);
746         ENTRY;
747
748         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
749                inode->i_generation, inode);
750
751         ll_inode2fid(&fid, inode);
752         clear_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &(ll_i2info(inode)->lli_flags));
753         mdc_change_cbdata(sbi->ll_mdc_exp, &fid, null_if_equal, inode);
754
755         if (lli->lli_smd)
756                 obd_change_cbdata(sbi->ll_osc_exp, lli->lli_smd,
757                                   null_if_equal, inode);
758
759         if (lli->lli_smd) {
760                 obd_free_memmd(sbi->ll_osc_exp, &lli->lli_smd);
761                 lli->lli_smd = NULL;
762         }
763
764         if (lli->lli_symlink_name) {
765                 OBD_FREE(lli->lli_symlink_name,
766                          strlen(lli->lli_symlink_name) + 1);
767                 lli->lli_symlink_name = NULL;
768         }
769
770         EXIT;
771 }
772
773 /* If this inode has objects allocated to it (lsm != NULL), then the OST
774  * object(s) determine the file size and mtime.  Otherwise, the MDS will
775  * keep these values until such a time that objects are allocated for it.
776  * We do the MDS operations first, as it is checking permissions for us.
777  * We don't to the MDS RPC if there is nothing that we want to store there,
778  * otherwise there is no harm in updating mtime/atime on the MDS if we are
779  * going to do an RPC anyways.
780  *
781  * If we are doing a truncate, we will send the mtime and ctime updates
782  * to the OST with the punch RPC, otherwise we do an explicit setattr RPC.
783  * I don't believe it is possible to get e.g. ATTR_MTIME_SET and ATTR_SIZE
784  * at the same time.
785  */
786 int ll_setattr_raw(struct inode *inode, struct iattr *attr)
787 {
788         struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
789         struct ll_sb_info *sbi = ll_i2sbi(inode);
790         struct ptlrpc_request *request = NULL;
791         struct mdc_op_data op_data;
792         int ia_valid = attr->ia_valid;
793         int rc = 0;
794         ENTRY;
795
796         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", inode->i_ino);
797         lprocfs_counter_incr(ll_i2sbi(inode)->ll_stats, LPROC_LL_SETATTR);
798
799         if (ia_valid & ATTR_SIZE) {
800                 if (attr->ia_size > ll_file_maxbytes(inode)) {
801                         CDEBUG(D_INODE, "file too large %llu > "LPU64"\n",
802                                attr->ia_size, ll_file_maxbytes(inode));
803                         RETURN(-EFBIG);
804                 }
805
806                 attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
807         }
808
809         /* POSIX: check before ATTR_*TIME_SET set (from inode_change_ok) */
810         if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET)) {
811                 if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER))
812                         RETURN(-EPERM);
813         }
814
815         /* We mark all of the fields "set" so MDS/OST does not re-set them */
816         if (attr->ia_valid & ATTR_CTIME) {
817                 attr->ia_ctime = CURRENT_TIME;
818                 attr->ia_valid |= ATTR_CTIME_SET;
819         }
820         if (!(ia_valid & ATTR_ATIME_SET) && (attr->ia_valid & ATTR_ATIME)) {
821                 attr->ia_atime = CURRENT_TIME;
822                 attr->ia_valid |= ATTR_ATIME_SET;
823         }
824         if (!(ia_valid & ATTR_MTIME_SET) && (attr->ia_valid & ATTR_MTIME)) {
825                 attr->ia_mtime = CURRENT_TIME;
826                 attr->ia_valid |= ATTR_MTIME_SET;
827         }
828
829         if (attr->ia_valid & (ATTR_MTIME | ATTR_CTIME))
830                 CDEBUG(D_INODE, "setting mtime %lu, ctime %lu, now = %lu\n",
831                        LTIME_S(attr->ia_mtime), LTIME_S(attr->ia_ctime),
832                        LTIME_S(CURRENT_TIME));
833         if (lsm)
834                 attr->ia_valid &= ~ATTR_SIZE;
835
836         /* If only OST attributes being set on objects, don't do MDS RPC.
837          * In that case, we need to check permissions and update the local
838          * inode ourselves so we can call obdo_from_inode() always. */
839         if (ia_valid & (lsm ? ~(ATTR_SIZE | ATTR_FROM_OPEN | ATTR_RAW) : ~0)) {
840                 struct lustre_md md;
841                 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
842
843                 rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
844                                  attr, NULL, 0, NULL, 0, &request);
845
846                 if (rc) {
847                         ptlrpc_req_finished(request);
848                         if (rc != -EPERM && rc != -EACCES)
849                                 CERROR("mdc_setattr fails: rc = %d\n", rc);
850                         RETURN(rc);
851                 }
852
853                 rc = mdc_req2lustre_md(request, 0, sbi->ll_osc_exp, &md);
854                 if (rc) {
855                         ptlrpc_req_finished(request);
856                         RETURN(rc);
857                 }
858
859                 /* Won't invoke vmtruncate as we already cleared ATTR_SIZE,
860                  * but needed to set timestamps backwards on utime. */
861                 inode_setattr(inode, attr);
862                 ll_update_inode(inode, md.body, md.lsm);
863                 ptlrpc_req_finished(request);
864
865                 if (!lsm || !S_ISREG(inode->i_mode)) {
866                         CDEBUG(D_INODE, "no lsm: not setting attrs on OST\n");
867                         RETURN(0);
868                 }
869         } else {
870                 /* The OST doesn't check permissions, but the alternative is
871                  * a gratuitous RPC to the MDS.  We already rely on the client
872                  * to do read/write/truncate permission checks, so is mtime OK?
873                  */
874                 if (ia_valid & (ATTR_MTIME | ATTR_ATIME)) {
875                         /* from sys_utime() */
876                         if (!(ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET))) {
877                                 if (current->fsuid != inode->i_uid &&
878                                     (rc=ll_permission(inode,MAY_WRITE,NULL))!=0)
879                                         RETURN(rc);
880                         } else {
881                                 /* from inode_change_ok() */
882                                 if (current->fsuid != inode->i_uid &&
883                                     !capable(CAP_FOWNER))
884                                         RETURN(-EPERM);
885                         }
886                 }
887
888                 /* Won't invoke vmtruncate, as we already cleared ATTR_SIZE */
889                 inode_setattr(inode, attr);
890         }
891
892         /* We really need to get our PW lock before we change inode->i_size.
893          * If we don't we can race with other i_size updaters on our node, like
894          * ll_file_read.  We can also race with i_size propogation to other
895          * nodes through dirtying and writeback of final cached pages.  This
896          * last one is especially bad for racing o_append users on other
897          * nodes. */
898         if (ia_valid & ATTR_SIZE) {
899                 ldlm_policy_data_t policy = { .l_extent = {attr->ia_size,
900                                                            OBD_OBJECT_EOF } };
901                 struct lustre_handle lockh = { 0 };
902                 int err, ast_flags = 0;
903                 /* XXX when we fix the AST intents to pass the discard-range
904                  * XXX extent, make ast_flags always LDLM_AST_DISCARD_DATA
905                  * XXX here. */
906                 if (attr->ia_size == 0)
907                         ast_flags = LDLM_AST_DISCARD_DATA;
908
909                 /* bug 1639: avoid write/truncate i_sem/DLM deadlock */
910                 LASSERT(atomic_read(&inode->i_sem.count) <= 0);
911                 up(&inode->i_sem);
912                 rc = ll_extent_lock(NULL, inode, lsm, LCK_PW, &policy, &lockh,
913                                     ast_flags);
914                 down(&inode->i_sem);
915                 if (rc != ELDLM_OK)
916                         RETURN(rc);
917
918                 rc = vmtruncate(inode, attr->ia_size);
919
920                 /* We need to drop the semaphore here, because this unlock may
921                  * result in a cancellation, which will need the i_sem */
922                 up(&inode->i_sem);
923                 /* unlock now as we don't mind others file lockers racing with
924                  * the mds updates below? */
925                 err = ll_extent_unlock(NULL, inode, lsm, LCK_PW, &lockh);
926                 down(&inode->i_sem);
927                 if (err) {
928                         CERROR("ll_extent_unlock failed: %d\n", err);
929                         if (!rc)
930                                 rc = err;
931                 }
932         } else if (ia_valid & (ATTR_MTIME | ATTR_MTIME_SET)) {
933                 struct obdo oa;
934
935                 CDEBUG(D_INODE, "set mtime on OST inode %lu to %lu\n",
936                        inode->i_ino, LTIME_S(attr->ia_mtime));
937                 oa.o_id = lsm->lsm_object_id;
938                 oa.o_valid = OBD_MD_FLID;
939                 obdo_from_inode(&oa, inode, OBD_MD_FLTYPE | OBD_MD_FLATIME |
940                                             OBD_MD_FLMTIME | OBD_MD_FLCTIME);
941                 rc = obd_setattr(sbi->ll_osc_exp, &oa, lsm, NULL);
942                 if (rc)
943                         CERROR("obd_setattr fails: rc=%d\n", rc);
944         }
945         RETURN(rc);
946 }
947
948 int ll_setattr(struct dentry *de, struct iattr *attr)
949 {
950         LBUG(); /* code is unused, but leave this in case of VFS changes */
951         RETURN(-ENOSYS);
952 }
953
954 int ll_statfs_internal(struct super_block *sb, struct obd_statfs *osfs,
955                        unsigned long max_age)
956 {
957         struct ll_sb_info *sbi = ll_s2sbi(sb);
958         struct obd_statfs obd_osfs;
959         int rc;
960         ENTRY;
961
962         rc = obd_statfs(class_exp2obd(sbi->ll_mdc_exp), osfs, max_age);
963         if (rc) {
964                 CERROR("mdc_statfs fails: rc = %d\n", rc);
965                 RETURN(rc);
966         }
967
968         osfs->os_type = sb->s_magic;
969
970         CDEBUG(D_SUPER, "MDC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
971                osfs->os_bavail, osfs->os_blocks, osfs->os_ffree,osfs->os_files);
972
973         rc = obd_statfs(class_exp2obd(sbi->ll_osc_exp), &obd_osfs, max_age);
974         if (rc) {
975                 CERROR("obd_statfs fails: rc = %d\n", rc);
976                 RETURN(rc);
977         }
978
979         CDEBUG(D_SUPER, "OSC blocks "LPU64"/"LPU64" objects "LPU64"/"LPU64"\n",
980                obd_osfs.os_bavail, obd_osfs.os_blocks, obd_osfs.os_ffree,
981                obd_osfs.os_files);
982
983         osfs->os_blocks = obd_osfs.os_blocks;
984         osfs->os_bfree = obd_osfs.os_bfree;
985         osfs->os_bavail = obd_osfs.os_bavail;
986
987         /* If we don't have as many objects free on the OST as inodes
988          * on the MDS, we reduce the total number of inodes to
989          * compensate, so that the "inodes in use" number is correct.
990          */
991         if (obd_osfs.os_ffree < osfs->os_ffree) {
992                 osfs->os_files = (osfs->os_files - osfs->os_ffree) +
993                         obd_osfs.os_ffree;
994                 osfs->os_ffree = obd_osfs.os_ffree;
995         }
996
997         RETURN(rc);
998 }
999
1000 int ll_statfs(struct super_block *sb, struct kstatfs *sfs)
1001 {
1002         struct obd_statfs osfs;
1003         int rc;
1004
1005         CDEBUG(D_VFSTRACE, "VFS Op:\n");
1006         lprocfs_counter_incr(ll_s2sbi(sb)->ll_stats, LPROC_LL_STAFS);
1007
1008         /* For now we will always get up-to-date statfs values, but in the
1009          * future we may allow some amount of caching on the client (e.g.
1010          * from QOS or lprocfs updates). */
1011         rc = ll_statfs_internal(sb, &osfs, jiffies - 1);
1012         if (rc)
1013                 return rc;
1014
1015         statfs_unpack(sfs, &osfs);
1016
1017         if (sizeof(sfs->f_blocks) == 4) {
1018                 while (osfs.os_blocks > ~0UL) {
1019                         sfs->f_bsize <<= 1;
1020
1021                         osfs.os_blocks >>= 1;
1022                         osfs.os_bfree >>= 1;
1023                         osfs.os_bavail >>= 1;
1024                 }
1025         }
1026
1027         sfs->f_blocks = osfs.os_blocks;
1028         sfs->f_bfree = osfs.os_bfree;
1029         sfs->f_bavail = osfs.os_bavail;
1030
1031         return 0;
1032 }
1033
1034 void dump_lsm(int level, struct lov_stripe_md *lsm)
1035 {
1036         CDEBUG(level, "objid "LPX64", maxbytes "LPX64", magic 0x%08X, "
1037                "stripe_size %u, stripe_count %u\n",
1038                lsm->lsm_object_id, lsm->lsm_maxbytes, lsm->lsm_magic,
1039                lsm->lsm_stripe_size, lsm->lsm_stripe_count);
1040 }
1041
1042 void ll_update_inode(struct inode *inode, struct mds_body *body,
1043                      struct lov_stripe_md *lsm)
1044 {
1045         struct ll_inode_info *lli = ll_i2info(inode);
1046
1047         LASSERT ((lsm != NULL) == ((body->valid & OBD_MD_FLEASIZE) != 0));
1048         if (lsm != NULL) {
1049                 if (lli->lli_smd == NULL) {
1050                         lli->lli_smd = lsm;
1051                         lli->lli_maxbytes = lsm->lsm_maxbytes;
1052                         if (lli->lli_maxbytes > PAGE_CACHE_MAXBYTES)
1053                                 lli->lli_maxbytes = PAGE_CACHE_MAXBYTES;
1054                 } else {
1055                         if (memcmp(lli->lli_smd, lsm, sizeof(*lsm))) {
1056                                 CERROR("lsm mismatch for inode %ld\n",
1057                                        inode->i_ino);
1058                                 CERROR("lli_smd:\n");
1059                                 dump_lsm(D_ERROR, lli->lli_smd);
1060                                 CERROR("lsm:\n");
1061                                 dump_lsm(D_ERROR, lsm);
1062                                 LBUG();
1063                         }
1064                 }
1065                 /* bug 2844 - limit i_blksize for broken user-space apps */
1066                 LASSERTF(lsm->lsm_xfersize != 0, "%lu\n", lsm->lsm_xfersize);
1067                 inode->i_blksize = min(lsm->lsm_xfersize, LL_MAX_BLKSIZE);
1068                 if (lli->lli_smd != lsm)
1069                         obd_free_memmd(ll_i2obdexp(inode), &lsm);
1070         }
1071
1072         if (body->valid & OBD_MD_FLID)
1073                 inode->i_ino = body->ino;
1074         if (body->valid & OBD_MD_FLATIME)
1075                 LTIME_S(inode->i_atime) = body->atime;
1076         if (body->valid & OBD_MD_FLMTIME &&
1077             body->mtime > LTIME_S(inode->i_mtime)) {
1078                 CDEBUG(D_INODE, "setting ino %lu mtime from %lu to %u\n",
1079                        inode->i_ino, LTIME_S(inode->i_mtime), body->mtime);
1080                 LTIME_S(inode->i_mtime) = body->mtime;
1081         }
1082         if (body->valid & OBD_MD_FLCTIME &&
1083             body->ctime > LTIME_S(inode->i_ctime))
1084                 LTIME_S(inode->i_ctime) = body->ctime;
1085         if (body->valid & OBD_MD_FLMODE)
1086                 inode->i_mode = (inode->i_mode & S_IFMT)|(body->mode & ~S_IFMT);
1087         if (body->valid & OBD_MD_FLTYPE)
1088                 inode->i_mode = (inode->i_mode & ~S_IFMT)|(body->mode & S_IFMT);
1089         if (body->valid & OBD_MD_FLUID)
1090                 inode->i_uid = body->uid;
1091         if (body->valid & OBD_MD_FLGID)
1092                 inode->i_gid = body->gid;
1093         if (body->valid & OBD_MD_FLFLAGS)
1094                 inode->i_flags = body->flags;
1095         if (body->valid & OBD_MD_FLNLINK)
1096                 inode->i_nlink = body->nlink;
1097         if (body->valid & OBD_MD_FLGENER)
1098                 inode->i_generation = body->generation;
1099         if (body->valid & OBD_MD_FLRDEV)
1100 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1101                 inode->i_rdev = body->rdev;
1102 #else
1103                 inode->i_rdev = old_decode_dev(body->rdev);
1104 #endif
1105         if (body->valid & OBD_MD_FLSIZE)
1106                 inode->i_size = body->size;
1107         if (body->valid & OBD_MD_FLBLOCKS)
1108                 inode->i_blocks = body->blocks;
1109
1110         if (body->valid & OBD_MD_FLSIZE)
1111                 set_bit(LLI_F_HAVE_MDS_SIZE_LOCK, &lli->lli_flags);
1112 }
1113
1114 void ll_read_inode2(struct inode *inode, void *opaque)
1115 {
1116         struct lustre_md *md = opaque;
1117         struct ll_inode_info *lli = ll_i2info(inode);
1118         ENTRY;
1119
1120         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%u(%p)\n", inode->i_ino,
1121                inode->i_generation, inode);
1122
1123         ll_lli_init(lli);
1124
1125         LASSERT(!lli->lli_smd);
1126
1127         /* Core attributes from the MDS first.  This is a new inode, and
1128          * the VFS doesn't zero times in the core inode so we have to do
1129          * it ourselves.  They will be overwritten by either MDS or OST
1130          * attributes - we just need to make sure they aren't newer. */
1131         LTIME_S(inode->i_mtime) = 0;
1132         LTIME_S(inode->i_atime) = 0;
1133         LTIME_S(inode->i_ctime) = 0;
1134         ll_update_inode(inode, md->body, md->lsm);
1135
1136         /* OIDEBUG(inode); */
1137
1138         if (S_ISREG(inode->i_mode)) {
1139                 inode->i_op = &ll_file_inode_operations;
1140                 inode->i_fop = &ll_file_operations;
1141                 inode->i_mapping->a_ops = &ll_aops;
1142                 EXIT;
1143         } else if (S_ISDIR(inode->i_mode)) {
1144                 inode->i_op = &ll_dir_inode_operations;
1145                 inode->i_fop = &ll_dir_operations;
1146                 inode->i_mapping->a_ops = &ll_dir_aops;
1147                 EXIT;
1148         } else if (S_ISLNK(inode->i_mode)) {
1149                 inode->i_op = &ll_fast_symlink_inode_operations;
1150                 EXIT;
1151         } else {
1152                 inode->i_op = &ll_special_inode_operations;
1153 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1154                 init_special_inode(inode, inode->i_mode,
1155                                    kdev_t_to_nr(inode->i_rdev));
1156 #else
1157                 init_special_inode(inode, inode->i_mode, inode->i_rdev);
1158 #endif
1159                 EXIT;
1160         }
1161 }
1162
1163 int ll_iocontrol(struct inode *inode, struct file *file,
1164                  unsigned int cmd, unsigned long arg)
1165 {
1166         struct ll_sb_info *sbi = ll_i2sbi(inode);
1167         struct ptlrpc_request *req = NULL;
1168         int rc, flags = 0;
1169         ENTRY;
1170
1171         switch(cmd) {
1172         case EXT3_IOC_GETFLAGS: {
1173                 struct ll_fid fid;
1174                 unsigned long valid = OBD_MD_FLFLAGS;
1175                 struct mds_body *body;
1176
1177                 ll_inode2fid(&fid, inode);
1178                 rc = mdc_getattr(sbi->ll_mdc_exp, &fid, valid, 0, &req);
1179                 if (rc) {
1180                         CERROR("failure %d inode %lu\n", rc, inode->i_ino);
1181                         RETURN(-abs(rc));
1182                 }
1183
1184                 body = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*body));
1185
1186                 if (body->flags & S_APPEND)
1187                         flags |= EXT3_APPEND_FL;
1188                 if (body->flags & S_IMMUTABLE)
1189                         flags |= EXT3_IMMUTABLE_FL;
1190                 if (body->flags & S_NOATIME)
1191                         flags |= EXT3_NOATIME_FL;
1192
1193                 ptlrpc_req_finished (req);
1194
1195                 RETURN(put_user(flags, (int *)arg));
1196         }
1197         case EXT3_IOC_SETFLAGS: {
1198                 struct mdc_op_data op_data;
1199                 struct iattr attr;
1200                 struct obdo *oa;
1201                 struct lov_stripe_md *lsm = ll_i2info(inode)->lli_smd;
1202
1203                 if (get_user(flags, (int *)arg))
1204                         RETURN(-EFAULT);
1205
1206                 oa = obdo_alloc();
1207                 if (!oa)
1208                         RETURN(-ENOMEM);
1209
1210                 ll_prepare_mdc_op_data(&op_data, inode, NULL, NULL, 0, 0);
1211
1212                 memset(&attr, 0x0, sizeof(attr));
1213                 attr.ia_attr_flags = flags;
1214                 attr.ia_valid |= ATTR_ATTR_FLAG;
1215
1216                 rc = mdc_setattr(sbi->ll_mdc_exp, &op_data,
1217                                  &attr, NULL, 0, NULL, 0, &req);
1218                 if (rc) {
1219                         ptlrpc_req_finished(req);
1220                         if (rc != -EPERM && rc != -EACCES)
1221                                 CERROR("mdc_setattr fails: rc = %d\n", rc);
1222                         obdo_free(oa);
1223                         RETURN(rc);
1224                 }
1225                 ptlrpc_req_finished(req);
1226
1227                 oa->o_id = lsm->lsm_object_id;
1228                 oa->o_flags = flags;
1229                 oa->o_valid = OBD_MD_FLID | OBD_MD_FLFLAGS;
1230
1231                 rc = obd_setattr(sbi->ll_osc_exp, oa, lsm, NULL);
1232                 obdo_free(oa);
1233                 if (rc) {
1234                         if (rc != -EPERM && rc != -EACCES)
1235                                 CERROR("mdc_setattr fails: rc = %d\n", rc);
1236                         RETURN(rc);
1237                 }
1238
1239                 if (flags & EXT3_APPEND_FL)
1240                         inode->i_flags |= S_APPEND;
1241                 else
1242                         inode->i_flags &= ~S_APPEND;
1243                 if (flags & EXT3_IMMUTABLE_FL)
1244                         inode->i_flags |= S_IMMUTABLE;
1245                 else
1246                         inode->i_flags &= ~S_IMMUTABLE;
1247                 if (flags & EXT3_NOATIME_FL)
1248                         inode->i_flags |= S_NOATIME;
1249                 else
1250                         inode->i_flags &= ~S_NOATIME;
1251
1252                 RETURN(0);
1253         }
1254         default:
1255                 RETURN(-ENOSYS);
1256         }
1257
1258         RETURN(0);
1259 }
1260
1261 void ll_umount_begin(struct super_block *sb)
1262 {
1263         struct ll_sb_info *sbi = ll_s2sbi(sb);
1264         struct obd_device *obd;
1265         struct obd_ioctl_data ioc_data = { 0 };
1266         ENTRY;
1267         CDEBUG(D_VFSTRACE, "VFS Op:\n");
1268
1269         obd = class_exp2obd(sbi->ll_mdc_exp);
1270         if (obd == NULL) {
1271                 CERROR("Invalid MDC connection handle "LPX64"\n",
1272                        sbi->ll_mdc_exp->exp_handle.h_cookie);
1273                 EXIT;
1274                 return;
1275         }
1276         obd->obd_no_recov = 1;
1277         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_mdc_exp, sizeof ioc_data,
1278                       &ioc_data, NULL);
1279
1280         obd = class_exp2obd(sbi->ll_osc_exp);
1281         if (obd == NULL) {
1282                 CERROR("Invalid LOV connection handle "LPX64"\n",
1283                        sbi->ll_osc_exp->exp_handle.h_cookie);
1284                 EXIT;
1285                 return;
1286         }
1287
1288         obd->obd_no_recov = 1;
1289         obd_iocontrol(IOC_OSC_SET_ACTIVE, sbi->ll_osc_exp, sizeof ioc_data,
1290                       &ioc_data, NULL);
1291
1292         /* Really, we'd like to wait until there are no requests outstanding,
1293          * and then continue.  For now, we just invalidate the requests,
1294          * schedule, and hope.
1295          */
1296         schedule();
1297
1298         EXIT;
1299 }
1300
1301 int ll_prep_inode(struct obd_export *exp, struct inode **inode,
1302                   struct ptlrpc_request *req, int offset,struct super_block *sb)
1303 {
1304         struct lustre_md md;
1305         int rc = 0;
1306
1307         rc = mdc_req2lustre_md(req, offset, exp, &md);
1308         if (rc)
1309                 RETURN(rc);
1310
1311         if (*inode) {
1312                 ll_update_inode(*inode, md.body, md.lsm);
1313         } else {
1314                 LASSERT(sb);
1315                 *inode = ll_iget(sb, md.body->ino, &md);
1316                 if (*inode == NULL || is_bad_inode(*inode)) {
1317                         /* free the lsm if we allocated one above */
1318                         if (md.lsm != NULL)
1319                                 obd_free_memmd(exp, &md.lsm);
1320                         rc = -ENOMEM;
1321                         CERROR("new_inode -fatal: rc %d\n", rc);
1322                 }
1323         }
1324
1325         RETURN(rc);
1326 }