]> Pileus Git - ~andy/linux/blob - drivers/staging/lustre/lustre/obdclass/llog_lvfs.c
Linux 3.14
[~andy/linux] / drivers / staging / lustre / lustre / obdclass / llog_lvfs.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdclass/llog_lvfs.c
37  *
38  * OST<->MDS recovery logging infrastructure.
39  * Invariants in implementation:
40  * - we do not share logs among different OST<->MDS connections, so that
41  *   if an OST or MDS fails it need only look at log(s) relevant to itself
42  *
43  * Author: Andreas Dilger <adilger@clusterfs.com>
44  */
45
46 #define DEBUG_SUBSYSTEM S_LOG
47
48
49 #include <obd.h>
50 #include <obd_class.h>
51 #include <lustre_log.h>
52 #include <obd_ost.h>
53 #include <linux/list.h>
54 #include <lvfs.h>
55 #include <lustre_fsfilt.h>
56 #include <lustre_disk.h>
57 #include "llog_internal.h"
58
59 #if  defined(LLOG_LVFS)
60
61 static int llog_lvfs_pad(struct obd_device *obd, struct l_file *file,
62                                 int len, int index)
63 {
64         struct llog_rec_hdr rec = { 0 };
65         struct llog_rec_tail tail;
66         int rc;
67
68         LASSERT(len >= LLOG_MIN_REC_SIZE && (len & 0x7) == 0);
69
70         tail.lrt_len = rec.lrh_len = len;
71         tail.lrt_index = rec.lrh_index = index;
72         rec.lrh_type = LLOG_PAD_MAGIC;
73
74         rc = fsfilt_write_record(obd, file, &rec, sizeof(rec), &file->f_pos, 0);
75         if (rc) {
76                 CERROR("error writing padding record: rc %d\n", rc);
77                 goto out;
78         }
79
80         file->f_pos += len - sizeof(rec) - sizeof(tail);
81         rc = fsfilt_write_record(obd, file, &tail, sizeof(tail),&file->f_pos,0);
82         if (rc) {
83                 CERROR("error writing padding record: rc %d\n", rc);
84                 goto out;
85         }
86
87  out:
88         return rc;
89 }
90
91 static int llog_lvfs_write_blob(struct obd_device *obd, struct l_file *file,
92                                 struct llog_rec_hdr *rec, void *buf, loff_t off)
93 {
94         int rc;
95         struct llog_rec_tail end;
96         loff_t saved_off = file->f_pos;
97         int buflen = rec->lrh_len;
98
99         file->f_pos = off;
100
101         if (buflen == 0)
102                 CWARN("0-length record\n");
103
104         if (!buf) {
105                 rc = fsfilt_write_record(obd, file, rec, buflen,&file->f_pos,0);
106                 if (rc) {
107                         CERROR("error writing log record: rc %d\n", rc);
108                         goto out;
109                 }
110                 GOTO(out, rc = 0);
111         }
112
113         /* the buf case */
114         rec->lrh_len = sizeof(*rec) + buflen + sizeof(end);
115         rc = fsfilt_write_record(obd, file, rec, sizeof(*rec), &file->f_pos, 0);
116         if (rc) {
117                 CERROR("error writing log hdr: rc %d\n", rc);
118                 goto out;
119         }
120
121         rc = fsfilt_write_record(obd, file, buf, buflen, &file->f_pos, 0);
122         if (rc) {
123                 CERROR("error writing log buffer: rc %d\n", rc);
124                 goto out;
125         }
126
127         end.lrt_len = rec->lrh_len;
128         end.lrt_index = rec->lrh_index;
129         rc = fsfilt_write_record(obd, file, &end, sizeof(end), &file->f_pos, 0);
130         if (rc) {
131                 CERROR("error writing log tail: rc %d\n", rc);
132                 goto out;
133         }
134
135         rc = 0;
136  out:
137         if (saved_off > file->f_pos)
138                 file->f_pos = saved_off;
139         LASSERT(rc <= 0);
140         return rc;
141 }
142
143 static int llog_lvfs_read_blob(struct obd_device *obd, struct l_file *file,
144                                 void *buf, int size, loff_t off)
145 {
146         loff_t offset = off;
147         int rc;
148
149         rc = fsfilt_read_record(obd, file, buf, size, &offset);
150         if (rc) {
151                 CERROR("error reading log record: rc %d\n", rc);
152                 return rc;
153         }
154         return 0;
155 }
156
157 static int llog_lvfs_read_header(const struct lu_env *env,
158                                  struct llog_handle *handle)
159 {
160         struct obd_device *obd;
161         int rc;
162
163         LASSERT(sizeof(*handle->lgh_hdr) == LLOG_CHUNK_SIZE);
164
165         obd = handle->lgh_ctxt->loc_exp->exp_obd;
166
167         if (i_size_read(handle->lgh_file->f_dentry->d_inode) == 0) {
168                 CDEBUG(D_HA, "not reading header from 0-byte log\n");
169                 return LLOG_EEMPTY;
170         }
171
172         rc = llog_lvfs_read_blob(obd, handle->lgh_file, handle->lgh_hdr,
173                                  LLOG_CHUNK_SIZE, 0);
174         if (rc) {
175                 CERROR("error reading log header from %.*s\n",
176                        handle->lgh_file->f_dentry->d_name.len,
177                        handle->lgh_file->f_dentry->d_name.name);
178         } else {
179                 struct llog_rec_hdr *llh_hdr = &handle->lgh_hdr->llh_hdr;
180
181                 if (LLOG_REC_HDR_NEEDS_SWABBING(llh_hdr))
182                         lustre_swab_llog_hdr(handle->lgh_hdr);
183
184                 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
185                         CERROR("bad log %.*s header magic: %#x (expected %#x)\n",
186                                handle->lgh_file->f_dentry->d_name.len,
187                                handle->lgh_file->f_dentry->d_name.name,
188                                llh_hdr->lrh_type, LLOG_HDR_MAGIC);
189                         rc = -EIO;
190                 } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
191                         CERROR("incorrectly sized log %.*s header: %#x "
192                                "(expected %#x)\n",
193                                handle->lgh_file->f_dentry->d_name.len,
194                                handle->lgh_file->f_dentry->d_name.name,
195                                llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
196                         CERROR("you may need to re-run lconf --write_conf.\n");
197                         rc = -EIO;
198                 }
199         }
200
201         handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
202         handle->lgh_file->f_pos = i_size_read(handle->lgh_file->f_dentry->d_inode);
203
204         return rc;
205 }
206
207 /* returns negative in on error; 0 if success && reccookie == 0; 1 otherwise */
208 /* appends if idx == -1, otherwise overwrites record idx. */
209 static int llog_lvfs_write_rec(const struct lu_env *env,
210                                struct llog_handle *loghandle,
211                                struct llog_rec_hdr *rec,
212                                struct llog_cookie *reccookie, int cookiecount,
213                                void *buf, int idx, struct thandle *th)
214 {
215         struct llog_log_hdr *llh;
216         int reclen = rec->lrh_len, index, rc;
217         struct llog_rec_tail *lrt;
218         struct obd_device *obd;
219         struct file *file;
220         size_t left;
221
222         llh = loghandle->lgh_hdr;
223         file = loghandle->lgh_file;
224         obd = loghandle->lgh_ctxt->loc_exp->exp_obd;
225
226         /* record length should not bigger than LLOG_CHUNK_SIZE */
227         if (buf)
228                 rc = (reclen > LLOG_CHUNK_SIZE - sizeof(struct llog_rec_hdr) -
229                       sizeof(struct llog_rec_tail)) ? -E2BIG : 0;
230         else
231                 rc = (reclen > LLOG_CHUNK_SIZE) ? -E2BIG : 0;
232         if (rc)
233                 return rc;
234
235         if (buf)
236                 /* write_blob adds header and tail to lrh_len. */
237                 reclen = sizeof(*rec) + rec->lrh_len +
238                          sizeof(struct llog_rec_tail);
239
240         if (idx != -1) {
241                 loff_t saved_offset;
242
243                 /* no header: only allowed to insert record 1 */
244                 if (idx != 1 && !i_size_read(file->f_dentry->d_inode)) {
245                         CERROR("idx != -1 in empty log\n");
246                         LBUG();
247                 }
248
249                 if (idx && llh->llh_size && llh->llh_size != rec->lrh_len)
250                         return -EINVAL;
251
252                 if (!ext2_test_bit(idx, llh->llh_bitmap))
253                         CERROR("Modify unset record %u\n", idx);
254                 if (idx != rec->lrh_index)
255                         CERROR("Index mismatch %d %u\n", idx, rec->lrh_index);
256
257                 rc = llog_lvfs_write_blob(obd, file, &llh->llh_hdr, NULL, 0);
258                 /* we are done if we only write the header or on error */
259                 if (rc || idx == 0)
260                         return rc;
261
262                 if (buf) {
263                         /* We assume that caller has set lgh_cur_* */
264                         saved_offset = loghandle->lgh_cur_offset;
265                         CDEBUG(D_OTHER,
266                                "modify record "DOSTID": idx:%d/%u/%d, len:%u "
267                                "offset %llu\n",
268                                POSTID(&loghandle->lgh_id.lgl_oi), idx, rec->lrh_index,
269                                loghandle->lgh_cur_idx, rec->lrh_len,
270                                (long long)(saved_offset - sizeof(*llh)));
271                         if (rec->lrh_index != loghandle->lgh_cur_idx) {
272                                 CERROR("modify idx mismatch %u/%d\n",
273                                        idx, loghandle->lgh_cur_idx);
274                                 return -EFAULT;
275                         }
276                 } else {
277                         /* Assumes constant lrh_len */
278                         saved_offset = sizeof(*llh) + (idx - 1) * reclen;
279                 }
280
281                 rc = llog_lvfs_write_blob(obd, file, rec, buf, saved_offset);
282                 if (rc == 0 && reccookie) {
283                         reccookie->lgc_lgl = loghandle->lgh_id;
284                         reccookie->lgc_index = idx;
285                         rc = 1;
286                 }
287                 return rc;
288         }
289
290         /* Make sure that records don't cross a chunk boundary, so we can
291          * process them page-at-a-time if needed.  If it will cross a chunk
292          * boundary, write in a fake (but referenced) entry to pad the chunk.
293          *
294          * We know that llog_current_log() will return a loghandle that is
295          * big enough to hold reclen, so all we care about is padding here.
296          */
297         left = LLOG_CHUNK_SIZE - (file->f_pos & (LLOG_CHUNK_SIZE - 1));
298
299         /* NOTE: padding is a record, but no bit is set */
300         if (left != 0 && left != reclen &&
301             left < (reclen + LLOG_MIN_REC_SIZE)) {
302                  index = loghandle->lgh_last_idx + 1;
303                  rc = llog_lvfs_pad(obd, file, left, index);
304                  if (rc)
305                          return rc;
306                  loghandle->lgh_last_idx++; /*for pad rec*/
307          }
308          /* if it's the last idx in log file, then return -ENOSPC */
309          if (loghandle->lgh_last_idx >= LLOG_BITMAP_SIZE(llh) - 1)
310                  return -ENOSPC;
311         loghandle->lgh_last_idx++;
312         index = loghandle->lgh_last_idx;
313         LASSERT(index < LLOG_BITMAP_SIZE(llh));
314         rec->lrh_index = index;
315         if (buf == NULL) {
316                 lrt = (struct llog_rec_tail *)
317                         ((char *)rec + rec->lrh_len - sizeof(*lrt));
318                 lrt->lrt_len = rec->lrh_len;
319                 lrt->lrt_index = rec->lrh_index;
320         }
321         /*The caller should make sure only 1 process access the lgh_last_idx,
322          *Otherwise it might hit the assert.*/
323         LASSERT(index < LLOG_BITMAP_SIZE(llh));
324         spin_lock(&loghandle->lgh_hdr_lock);
325         if (ext2_set_bit(index, llh->llh_bitmap)) {
326                 CERROR("argh, index %u already set in log bitmap?\n", index);
327                 spin_unlock(&loghandle->lgh_hdr_lock);
328                 LBUG(); /* should never happen */
329         }
330         llh->llh_count++;
331         spin_unlock(&loghandle->lgh_hdr_lock);
332         llh->llh_tail.lrt_index = index;
333
334         rc = llog_lvfs_write_blob(obd, file, &llh->llh_hdr, NULL, 0);
335         if (rc)
336                 return rc;
337
338         rc = llog_lvfs_write_blob(obd, file, rec, buf, file->f_pos);
339         if (rc)
340                 return rc;
341
342         CDEBUG(D_RPCTRACE, "added record "DOSTID": idx: %u, %u \n",
343                POSTID(&loghandle->lgh_id.lgl_oi), index, rec->lrh_len);
344         if (rc == 0 && reccookie) {
345                 reccookie->lgc_lgl = loghandle->lgh_id;
346                 reccookie->lgc_index = index;
347                 if ((rec->lrh_type == MDS_UNLINK_REC) ||
348                     (rec->lrh_type == MDS_SETATTR64_REC))
349                         reccookie->lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
350                 else if (rec->lrh_type == OST_SZ_REC)
351                         reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
352                 else
353                         reccookie->lgc_subsys = -1;
354                 rc = 1;
355         }
356         if (rc == 0 && rec->lrh_type == LLOG_GEN_REC)
357                 rc = 1;
358
359         return rc;
360 }
361
362 /* We can skip reading at least as many log blocks as the number of
363 * minimum sized log records we are skipping.  If it turns out
364 * that we are not far enough along the log (because the
365 * actual records are larger than minimum size) we just skip
366 * some more records. */
367
368 static void llog_skip_over(__u64 *off, int curr, int goal)
369 {
370         if (goal <= curr)
371                 return;
372         *off = (*off + (goal-curr-1) * LLOG_MIN_REC_SIZE) &
373                 ~(LLOG_CHUNK_SIZE - 1);
374 }
375
376
377 /* sets:
378  *  - cur_offset to the furthest point read in the log file
379  *  - cur_idx to the log index preceeding cur_offset
380  * returns -EIO/-EINVAL on error
381  */
382 static int llog_lvfs_next_block(const struct lu_env *env,
383                                 struct llog_handle *loghandle, int *cur_idx,
384                                 int next_idx, __u64 *cur_offset, void *buf,
385                                 int len)
386 {
387         int rc;
388
389         if (len == 0 || len & (LLOG_CHUNK_SIZE - 1))
390                 return -EINVAL;
391
392         CDEBUG(D_OTHER, "looking for log index %u (cur idx %u off "LPU64")\n",
393                next_idx, *cur_idx, *cur_offset);
394
395         while (*cur_offset < i_size_read(loghandle->lgh_file->f_dentry->d_inode)) {
396                 struct llog_rec_hdr *rec, *last_rec;
397                 struct llog_rec_tail *tail;
398                 loff_t ppos;
399                 int llen;
400
401                 llog_skip_over(cur_offset, *cur_idx, next_idx);
402
403                 /* read up to next LLOG_CHUNK_SIZE block */
404                 ppos = *cur_offset;
405                 llen = LLOG_CHUNK_SIZE - (*cur_offset & (LLOG_CHUNK_SIZE - 1));
406                 rc = fsfilt_read_record(loghandle->lgh_ctxt->loc_exp->exp_obd,
407                                         loghandle->lgh_file, buf, llen,
408                                         cur_offset);
409                 if (rc < 0) {
410                         CERROR("Cant read llog block at log id "DOSTID
411                                "/%u offset "LPU64"\n",
412                                POSTID(&loghandle->lgh_id.lgl_oi),
413                                loghandle->lgh_id.lgl_ogen,
414                                *cur_offset);
415                         return rc;
416                 }
417
418                 /* put number of bytes read into rc to make code simpler */
419                 rc = *cur_offset - ppos;
420                 if (rc < len) {
421                         /* signal the end of the valid buffer to llog_process */
422                         memset(buf + rc, 0, len - rc);
423                 }
424
425                 if (rc == 0) /* end of file, nothing to do */
426                         return 0;
427
428                 if (rc < sizeof(*tail)) {
429                         CERROR("Invalid llog block at log id "DOSTID"/%u offset"
430                                LPU64"\n", POSTID(&loghandle->lgh_id.lgl_oi),
431                                loghandle->lgh_id.lgl_ogen, *cur_offset);
432                         return -EINVAL;
433                 }
434
435                 rec = buf;
436                 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
437                         lustre_swab_llog_rec(rec);
438
439                 tail = (struct llog_rec_tail *)(buf + rc -
440                                                 sizeof(struct llog_rec_tail));
441
442                 /* get the last record in block */
443                 last_rec = (struct llog_rec_hdr *)(buf + rc -
444                                                    le32_to_cpu(tail->lrt_len));
445
446                 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
447                         lustre_swab_llog_rec(last_rec);
448                 LASSERT(last_rec->lrh_index == tail->lrt_index);
449
450                 *cur_idx = tail->lrt_index;
451
452                 /* this shouldn't happen */
453                 if (tail->lrt_index == 0) {
454                         CERROR("Invalid llog tail at log id "DOSTID"/%u offset "
455                                LPU64"\n", POSTID(&loghandle->lgh_id.lgl_oi),
456                                loghandle->lgh_id.lgl_ogen, *cur_offset);
457                         return -EINVAL;
458                 }
459                 if (tail->lrt_index < next_idx)
460                         continue;
461
462                 /* sanity check that the start of the new buffer is no farther
463                  * than the record that we wanted.  This shouldn't happen. */
464                 if (rec->lrh_index > next_idx) {
465                         CERROR("missed desired record? %u > %u\n",
466                                rec->lrh_index, next_idx);
467                         return -ENOENT;
468                 }
469                 return 0;
470         }
471         return -EIO;
472 }
473
474 static int llog_lvfs_prev_block(const struct lu_env *env,
475                                 struct llog_handle *loghandle,
476                                 int prev_idx, void *buf, int len)
477 {
478         __u64 cur_offset;
479         int rc;
480
481         if (len == 0 || len & (LLOG_CHUNK_SIZE - 1))
482                 return -EINVAL;
483
484         CDEBUG(D_OTHER, "looking for log index %u\n", prev_idx);
485
486         cur_offset = LLOG_CHUNK_SIZE;
487         llog_skip_over(&cur_offset, 0, prev_idx);
488
489         while (cur_offset < i_size_read(loghandle->lgh_file->f_dentry->d_inode)) {
490                 struct llog_rec_hdr *rec, *last_rec;
491                 struct llog_rec_tail *tail;
492                 loff_t ppos = cur_offset;
493
494                 rc = fsfilt_read_record(loghandle->lgh_ctxt->loc_exp->exp_obd,
495                                         loghandle->lgh_file, buf, len,
496                                         &cur_offset);
497                 if (rc < 0) {
498                         CERROR("Cant read llog block at log id "DOSTID
499                                "/%u offset "LPU64"\n",
500                                POSTID(&loghandle->lgh_id.lgl_oi),
501                                loghandle->lgh_id.lgl_ogen,
502                                cur_offset);
503                         return rc;
504                 }
505
506                 /* put number of bytes read into rc to make code simpler */
507                 rc = cur_offset - ppos;
508
509                 if (rc == 0) /* end of file, nothing to do */
510                         return 0;
511
512                 if (rc < sizeof(*tail)) {
513                         CERROR("Invalid llog block at log id "DOSTID"/%u offset"
514                                LPU64"\n", POSTID(&loghandle->lgh_id.lgl_oi),
515                                loghandle->lgh_id.lgl_ogen, cur_offset);
516                         return -EINVAL;
517                 }
518
519                 rec = buf;
520                 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
521                         lustre_swab_llog_rec(rec);
522
523                 tail = (struct llog_rec_tail *)(buf + rc -
524                                                 sizeof(struct llog_rec_tail));
525
526                 /* get the last record in block */
527                 last_rec = (struct llog_rec_hdr *)(buf + rc -
528                                                    le32_to_cpu(tail->lrt_len));
529
530                 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
531                         lustre_swab_llog_rec(last_rec);
532                 LASSERT(last_rec->lrh_index == tail->lrt_index);
533
534                 /* this shouldn't happen */
535                 if (tail->lrt_index == 0) {
536                         CERROR("Invalid llog tail at log id "DOSTID"/%u offset"
537                                LPU64"\n", POSTID(&loghandle->lgh_id.lgl_oi),
538                                loghandle->lgh_id.lgl_ogen, cur_offset);
539                         return -EINVAL;
540                 }
541                 if (tail->lrt_index < prev_idx)
542                         continue;
543
544                 /* sanity check that the start of the new buffer is no farther
545                  * than the record that we wanted.  This shouldn't happen. */
546                 if (rec->lrh_index > prev_idx) {
547                         CERROR("missed desired record? %u > %u\n",
548                                rec->lrh_index, prev_idx);
549                         return -ENOENT;
550                 }
551                 return 0;
552         }
553         return -EIO;
554 }
555
556 static struct file *llog_filp_open(char *dir, char *name, int flags, int mode)
557 {
558         char *logname;
559         struct file *filp;
560         int len;
561
562         OBD_ALLOC(logname, PATH_MAX);
563         if (logname == NULL)
564                 return ERR_PTR(-ENOMEM);
565
566         len = snprintf(logname, PATH_MAX, "%s/%s", dir, name);
567         if (len >= PATH_MAX - 1) {
568                 filp = ERR_PTR(-ENAMETOOLONG);
569         } else {
570                 filp = l_filp_open(logname, flags, mode);
571                 if (IS_ERR(filp) && PTR_ERR(filp) != -ENOENT)
572                         CERROR("logfile creation %s: %ld\n", logname,
573                                PTR_ERR(filp));
574         }
575         OBD_FREE(logname, PATH_MAX);
576         return filp;
577 }
578
579 static int llog_lvfs_open(const struct lu_env *env,  struct llog_handle *handle,
580                           struct llog_logid *logid, char *name,
581                           enum llog_open_param open_param)
582 {
583         struct llog_ctxt        *ctxt = handle->lgh_ctxt;
584         struct l_dentry         *dchild = NULL;
585         struct obd_device       *obd;
586         int                      rc = 0;
587
588         LASSERT(ctxt);
589         LASSERT(ctxt->loc_exp);
590         LASSERT(ctxt->loc_exp->exp_obd);
591         obd = ctxt->loc_exp->exp_obd;
592
593         LASSERT(handle);
594         if (logid != NULL) {
595                 dchild = obd_lvfs_fid2dentry(ctxt->loc_exp, &logid->lgl_oi,
596                                              logid->lgl_ogen);
597                 if (IS_ERR(dchild)) {
598                         rc = PTR_ERR(dchild);
599                         CERROR("%s: error looking up logfile #"DOSTID "#%08x:"
600                                " rc = %d\n", ctxt->loc_obd->obd_name,
601                                POSTID(&logid->lgl_oi), logid->lgl_ogen, rc);
602                         GOTO(out, rc);
603                 }
604                 if (dchild->d_inode == NULL) {
605                         l_dput(dchild);
606                         rc = -ENOENT;
607                         CERROR("%s: nonexistent llog #"DOSTID"#%08x:"
608                                "rc = %d\n", ctxt->loc_obd->obd_name,
609                                POSTID(&logid->lgl_oi), logid->lgl_ogen, rc);
610                         GOTO(out, rc);
611                 }
612                 handle->lgh_file = l_dentry_open(&obd->obd_lvfs_ctxt, dchild,
613                                                  O_RDWR | O_LARGEFILE);
614                 l_dput(dchild);
615                 if (IS_ERR(handle->lgh_file)) {
616                         rc = PTR_ERR(handle->lgh_file);
617                         handle->lgh_file = NULL;
618                         CERROR("%s: error opening llog #"DOSTID"#%08x:"
619                                "rc = %d\n", ctxt->loc_obd->obd_name,
620                                POSTID(&logid->lgl_oi), logid->lgl_ogen, rc);
621                         GOTO(out, rc);
622                 }
623                 handle->lgh_id = *logid;
624         } else if (name) {
625                 handle->lgh_file = llog_filp_open(MOUNT_CONFIGS_DIR, name,
626                                                   O_RDWR | O_LARGEFILE, 0644);
627                 if (IS_ERR(handle->lgh_file)) {
628                         rc = PTR_ERR(handle->lgh_file);
629                         handle->lgh_file = NULL;
630                         if (rc == -ENOENT && open_param == LLOG_OPEN_NEW) {
631                                 OBD_ALLOC(handle->lgh_name, strlen(name) + 1);
632                                 if (handle->lgh_name)
633                                         strcpy(handle->lgh_name, name);
634                                 else
635                                         GOTO(out, rc = -ENOMEM);
636                                 rc = 0;
637                         } else {
638                                 GOTO(out, rc);
639                         }
640                 } else {
641                         lustre_build_llog_lvfs_oid(&handle->lgh_id,
642                             handle->lgh_file->f_dentry->d_inode->i_ino,
643                             handle->lgh_file->f_dentry->d_inode->i_generation);
644                 }
645         } else {
646                 LASSERTF(open_param == LLOG_OPEN_NEW, "%#x\n", open_param);
647                 handle->lgh_file = NULL;
648         }
649
650         /* No new llog is expected but doesn't exist */
651         if (open_param != LLOG_OPEN_NEW && handle->lgh_file == NULL)
652                 GOTO(out_name, rc = -ENOENT);
653
654         return 0;
655 out_name:
656         if (handle->lgh_name != NULL)
657                 OBD_FREE(handle->lgh_name, strlen(name) + 1);
658 out:
659         return rc;
660 }
661
662 static int llog_lvfs_exist(struct llog_handle *handle)
663 {
664         return (handle->lgh_file != NULL);
665 }
666
667 /* This is a callback from the llog_* functions.
668  * Assumes caller has already pushed us into the kernel context. */
669 static int llog_lvfs_create(const struct lu_env *env,
670                             struct llog_handle *handle,
671                             struct thandle *th)
672 {
673         struct llog_ctxt        *ctxt = handle->lgh_ctxt;
674         struct obd_device       *obd;
675         struct l_dentry         *dchild = NULL;
676         struct file             *file;
677         struct obdo             *oa = NULL;
678         int                      rc = 0;
679         int                      open_flags = O_RDWR | O_CREAT | O_LARGEFILE;
680
681         LASSERT(ctxt);
682         LASSERT(ctxt->loc_exp);
683         obd = ctxt->loc_exp->exp_obd;
684         LASSERT(handle->lgh_file == NULL);
685
686         if (handle->lgh_name) {
687                 file = llog_filp_open(MOUNT_CONFIGS_DIR, handle->lgh_name,
688                                       open_flags, 0644);
689                 if (IS_ERR(file))
690                         return PTR_ERR(file);
691
692                 lustre_build_llog_lvfs_oid(&handle->lgh_id,
693                                 file->f_dentry->d_inode->i_ino,
694                                 file->f_dentry->d_inode->i_generation);
695                 handle->lgh_file = file;
696         } else {
697                 OBDO_ALLOC(oa);
698                 if (oa == NULL)
699                         return -ENOMEM;
700
701                 ostid_set_seq_llog(&oa->o_oi);
702                 oa->o_valid = OBD_MD_FLGENER | OBD_MD_FLGROUP;
703
704                 rc = obd_create(NULL, ctxt->loc_exp, oa, NULL, NULL);
705                 if (rc)
706                         GOTO(out, rc);
707
708                 /* FIXME: rationalize the misuse of o_generation in
709                  *      this API along with mds_obd_{create,destroy}.
710                  *      Hopefully it is only an internal API issue. */
711 #define o_generation o_parent_oid
712                 dchild = obd_lvfs_fid2dentry(ctxt->loc_exp, &oa->o_oi,
713                                              oa->o_generation);
714                 if (IS_ERR(dchild))
715                         GOTO(out, rc = PTR_ERR(dchild));
716
717                 file = l_dentry_open(&obd->obd_lvfs_ctxt, dchild, open_flags);
718                 l_dput(dchild);
719                 if (IS_ERR(file))
720                         GOTO(out, rc = PTR_ERR(file));
721                 handle->lgh_id.lgl_oi = oa->o_oi;
722                 handle->lgh_id.lgl_ogen = oa->o_generation;
723                 handle->lgh_file = file;
724 out:
725                 OBDO_FREE(oa);
726         }
727         return rc;
728 }
729
730 static int llog_lvfs_close(const struct lu_env *env,
731                            struct llog_handle *handle)
732 {
733         int rc;
734
735         if (handle->lgh_file == NULL)
736                 return 0;
737         rc = filp_close(handle->lgh_file, 0);
738         if (rc)
739                 CERROR("%s: error closing llog #"DOSTID"#%08x: "
740                        "rc = %d\n", handle->lgh_ctxt->loc_obd->obd_name,
741                        POSTID(&handle->lgh_id.lgl_oi),
742                        handle->lgh_id.lgl_ogen, rc);
743         handle->lgh_file = NULL;
744         if (handle->lgh_name) {
745                 OBD_FREE(handle->lgh_name, strlen(handle->lgh_name) + 1);
746                 handle->lgh_name = NULL;
747         }
748         return rc;
749 }
750
751 static int llog_lvfs_destroy(const struct lu_env *env,
752                              struct llog_handle *handle)
753 {
754         struct dentry *fdentry;
755         struct obdo *oa;
756         struct obd_device *obd = handle->lgh_ctxt->loc_exp->exp_obd;
757         char *dir;
758         void *th;
759         struct inode *inode;
760         int rc, rc1;
761
762         dir = MOUNT_CONFIGS_DIR;
763
764         LASSERT(handle->lgh_file);
765         fdentry = handle->lgh_file->f_dentry;
766         inode = fdentry->d_parent->d_inode;
767         if (strcmp(fdentry->d_parent->d_name.name, dir) == 0) {
768                 struct lvfs_run_ctxt saved;
769                 struct vfsmount *mnt = mntget(handle->lgh_file->f_vfsmnt);
770
771                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
772                 dget(fdentry);
773                 rc = llog_lvfs_close(env, handle);
774                 if (rc == 0) {
775                         mutex_lock_nested(&inode->i_mutex, I_MUTEX_PARENT);
776                         rc = ll_vfs_unlink(inode, fdentry, mnt);
777                         mutex_unlock(&inode->i_mutex);
778                 }
779                 mntput(mnt);
780
781                 dput(fdentry);
782                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
783                 return rc;
784         }
785
786         OBDO_ALLOC(oa);
787         if (oa == NULL)
788                 return -ENOMEM;
789
790         oa->o_oi = handle->lgh_id.lgl_oi;
791         oa->o_generation = handle->lgh_id.lgl_ogen;
792 #undef o_generation
793         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP | OBD_MD_FLGENER;
794
795         rc = llog_lvfs_close(env, handle);
796         if (rc)
797                 GOTO(out, rc);
798
799         th = fsfilt_start_log(obd, inode, FSFILT_OP_UNLINK, NULL, 1);
800         if (IS_ERR(th)) {
801                 CERROR("fsfilt_start failed: %ld\n", PTR_ERR(th));
802                 GOTO(out, rc = PTR_ERR(th));
803         }
804
805         rc = obd_destroy(NULL, handle->lgh_ctxt->loc_exp, oa,
806                          NULL, NULL, NULL, NULL);
807
808         rc1 = fsfilt_commit(obd, inode, th, 0);
809         if (rc == 0 && rc1 != 0)
810                 rc = rc1;
811  out:
812         OBDO_FREE(oa);
813         return rc;
814 }
815
816 static int llog_lvfs_declare_create(const struct lu_env *env,
817                                     struct llog_handle *res,
818                                     struct thandle *th)
819 {
820         return 0;
821 }
822
823 static int llog_lvfs_declare_write_rec(const struct lu_env *env,
824                                        struct llog_handle *loghandle,
825                                        struct llog_rec_hdr *rec,
826                                        int idx, struct thandle *th)
827 {
828         return 0;
829 }
830
831 struct llog_operations llog_lvfs_ops = {
832         .lop_write_rec          = llog_lvfs_write_rec,
833         .lop_next_block         = llog_lvfs_next_block,
834         .lop_prev_block         = llog_lvfs_prev_block,
835         .lop_read_header        = llog_lvfs_read_header,
836         .lop_create             = llog_lvfs_create,
837         .lop_destroy            = llog_lvfs_destroy,
838         .lop_close              = llog_lvfs_close,
839         .lop_open               = llog_lvfs_open,
840         .lop_exist              = llog_lvfs_exist,
841         .lop_declare_create     = llog_lvfs_declare_create,
842         .lop_declare_write_rec  = llog_lvfs_declare_write_rec,
843 };
844 EXPORT_SYMBOL(llog_lvfs_ops);
845 #else /* !__KERNEL__ */
846 struct llog_operations llog_lvfs_ops = {};
847 #endif