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