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