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