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