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