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