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