Whamcloud - gitweb
LU-2677 obdfilter: add LMA for all OST objects
[fs/lustre-release.git] / lustre / ofd / ofd_objects.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) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 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/ofd/ofd_objects.c
37  *
38  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
39  * Author: Mikhail Pershin <tappro@whamcloud.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_FILTER
43
44 #include <dt_object.h>
45 #include <lustre/lustre_idl.h>
46
47 #include "ofd_internal.h"
48
49 int ofd_version_get_check(struct ofd_thread_info *info,
50                           struct ofd_object *fo)
51 {
52         dt_obj_version_t curr_version;
53
54         LASSERT(ofd_object_exists(fo));
55         LASSERT(info->fti_exp);
56
57         curr_version = dt_version_get(info->fti_env, ofd_object_child(fo));
58         if ((__s64)curr_version == -EOPNOTSUPP)
59                 RETURN(0);
60         /* VBR: version is checked always because costs nothing */
61         if (info->fti_pre_version != 0 &&
62             info->fti_pre_version != curr_version) {
63                 CDEBUG(D_INODE, "Version mismatch "LPX64" != "LPX64"\n",
64                        info->fti_pre_version, curr_version);
65                 spin_lock(&info->fti_exp->exp_lock);
66                 info->fti_exp->exp_vbr_failed = 1;
67                 spin_unlock(&info->fti_exp->exp_lock);
68                 RETURN (-EOVERFLOW);
69         }
70         info->fti_pre_version = curr_version;
71         RETURN(0);
72 }
73
74 struct ofd_object *ofd_object_find(const struct lu_env *env,
75                                    struct ofd_device *ofd,
76                                    const struct lu_fid *fid)
77 {
78         struct ofd_object *fo;
79         struct lu_object  *o;
80
81         ENTRY;
82
83         o = lu_object_find(env, &ofd->ofd_dt_dev.dd_lu_dev, fid, NULL);
84         if (likely(!IS_ERR(o)))
85                 fo = ofd_obj(o);
86         else
87                 fo = (struct ofd_object *)o; /* return error */
88         RETURN(fo);
89 }
90
91 struct ofd_object *ofd_object_find_or_create(const struct lu_env *env,
92                                              struct ofd_device *ofd,
93                                              const struct lu_fid *fid,
94                                              struct lu_attr *attr)
95 {
96         struct ofd_thread_info  *info = ofd_info(env);
97         struct lu_object        *fo_obj;
98         struct dt_object        *dto;
99
100         ENTRY;
101
102         info->fti_dof.dof_type = dt_mode_to_dft(S_IFREG);
103
104         dto = dt_find_or_create(env, ofd->ofd_osd, fid, &info->fti_dof, attr);
105         if (IS_ERR(dto))
106                 RETURN((struct ofd_object *)dto);
107
108         fo_obj = lu_object_locate(dto->do_lu.lo_header,
109                                   ofd->ofd_dt_dev.dd_lu_dev.ld_type);
110         RETURN(ofd_obj(fo_obj));
111 }
112
113 int ofd_object_ff_check(const struct lu_env *env, struct ofd_object *fo)
114 {
115         int rc = 0;
116
117         ENTRY;
118
119         if (!fo->ofo_ff_exists) {
120                 /*
121                  * This actually means that we don't know whether the object
122                  * has the "fid" EA or not.
123                  */
124                 rc = dt_xattr_get(env, ofd_object_child(fo), &LU_BUF_NULL,
125                                   XATTR_NAME_FID, BYPASS_CAPA);
126                 if (rc >= 0 || rc == -ENODATA) {
127                         /*
128                          * Here we assume that, if the object doesn't have the
129                          * "fid" EA, the caller will add one, unless a fatal
130                          * error (e.g., a memory or disk failure) prevents it
131                          * from doing so.
132                          */
133                         fo->ofo_ff_exists = 1;
134                 }
135                 if (rc > 0)
136                         rc = 0;
137         }
138         RETURN(rc);
139 }
140
141 void ofd_object_put(const struct lu_env *env, struct ofd_object *fo)
142 {
143         lu_object_put(env, &fo->ofo_obj.do_lu);
144 }
145
146 int ofd_precreate_objects(const struct lu_env *env, struct ofd_device *ofd,
147                           obd_id id, struct ofd_seq *oseq, int nr, int sync)
148 {
149         struct ofd_thread_info  *info = ofd_info(env);
150         struct ofd_object       *fo = NULL;
151         struct dt_object        *next;
152         struct thandle          *th;
153         struct ofd_object       **batch;
154         obd_id                   tmp;
155         int                      rc;
156         int                      i;
157         int                      objects = 0;
158         int                      nr_saved = nr;
159
160         ENTRY;
161
162         /* Don't create objects beyond the valid range for this SEQ */
163         if (unlikely(fid_seq_is_mdt0(oseq->os_seq) && (id + nr) >= IDIF_MAX_OID)) {
164                 CERROR("%s:"DOSTID" hit the IDIF_MAX_OID (1<<48)!\n",
165                        ofd_name(ofd), id, oseq->os_seq);
166                 RETURN(rc = -ENOSPC);
167         } else if (unlikely(!fid_seq_is_mdt0(oseq->os_seq) &&
168                             (id + nr) >= OBIF_MAX_OID)) {
169                 CERROR("%s:"DOSTID" hit the OBIF_MAX_OID (1<<32)!\n",
170                        ofd_name(ofd), id, oseq->os_seq);
171                 RETURN(rc = -ENOSPC);
172         }
173
174         OBD_ALLOC(batch, nr_saved * sizeof(struct ofd_object *));
175         if (batch == NULL)
176                 RETURN(-ENOMEM);
177
178         info->fti_attr.la_valid = LA_TYPE | LA_MODE;
179         /*
180          * We mark object SUID+SGID to flag it for accepting UID+GID from
181          * client on first write.  Currently the permission bits on the OST are
182          * never used, so this is OK.
183          */
184         info->fti_attr.la_mode = S_IFREG | S_ISUID | S_ISGID | 0666;
185         info->fti_dof.dof_type = dt_mode_to_dft(S_IFREG);
186
187         /* Initialize a/c/m time so any client timestamp will always
188          * be newer and update the inode. ctime = 0 is also handled
189          * specially in osd_inode_setattr(). See LU-221, LU-1042 */
190         info->fti_attr.la_valid |= LA_ATIME | LA_MTIME | LA_CTIME;
191         info->fti_attr.la_atime = 0;
192         info->fti_attr.la_mtime = 0;
193         info->fti_attr.la_ctime = 0;
194
195         /* prepare objects */
196         for (i = 0; i < nr; i++) {
197                 info->fti_ostid.oi_id = id + i;
198                 info->fti_ostid.oi_seq = oseq->os_seq;
199
200                 rc = fid_ostid_unpack(&info->fti_fid, &info->fti_ostid, 0);
201                 if (rc) {
202                         if (i == 0)
203                                 GOTO(out, rc = PTR_ERR(fo));
204
205                         nr = i;
206                         break;
207                 }
208
209                 fo = ofd_object_find(env, ofd, &info->fti_fid);
210                 if (IS_ERR(fo)) {
211                         if (i == 0)
212                                 GOTO(out, rc = PTR_ERR(fo));
213
214                         nr = i;
215                         break;
216                 }
217
218                 ofd_write_lock(env, fo);
219                 batch[i] = fo;
220         }
221         info->fti_buf.lb_buf = &tmp;
222         info->fti_buf.lb_len = sizeof(tmp);
223         info->fti_off = 0;
224
225         th = ofd_trans_create(env, ofd);
226         if (IS_ERR(th))
227                 GOTO(out, rc = PTR_ERR(th));
228
229         th->th_sync |= sync;
230
231         rc = dt_declare_record_write(env, oseq->os_lastid_obj, sizeof(tmp),
232                                      info->fti_off, th);
233         if (rc)
234                 GOTO(trans_stop, rc);
235
236         for (i = 0; i < nr; i++) {
237                 fo = batch[i];
238                 LASSERT(fo);
239
240                 if (unlikely(ofd_object_exists(fo))) {
241                         /* object may exist being re-created by write replay */
242                         CDEBUG(D_INODE, "object "LPX64"/"LPX64" exists: "
243                                DFID"\n", oseq->os_seq, id,
244                                PFID(&info->fti_fid));
245                         continue;
246                 }
247
248                 next = ofd_object_child(fo);
249                 LASSERT(next != NULL);
250
251                 rc = dt_declare_create(env, next, &info->fti_attr, NULL,
252                                        &info->fti_dof, th);
253                 if (rc) {
254                         nr = i;
255                         break;
256                 }
257         }
258
259         rc = dt_trans_start_local(env, ofd->ofd_osd, th);
260         if (rc)
261                 GOTO(trans_stop, rc);
262
263         CDEBUG(D_OTHER, "create new object "DFID"\n", PFID(&info->fti_fid));
264
265         for (i = 0; i < nr; i++) {
266                 fo = batch[i];
267                 LASSERT(fo);
268
269                 if (likely(!ofd_object_exists(fo))) {
270                         next = ofd_object_child(fo);
271                         LASSERT(next != NULL);
272
273                         rc = dt_create(env, next, &info->fti_attr, NULL,
274                                        &info->fti_dof, th);
275                         if (rc)
276                                 break;
277                         LASSERT(ofd_object_exists(fo));
278                 }
279                 ofd_seq_last_oid_set(oseq, id + i);
280         }
281
282         objects = i;
283         if (objects > 0) {
284                 tmp = cpu_to_le64(ofd_seq_last_oid(oseq));
285                 rc = dt_record_write(env, oseq->os_lastid_obj,
286                                      &info->fti_buf, &info->fti_off, th);
287         }
288 trans_stop:
289         ofd_trans_stop(env, ofd, th, rc);
290 out:
291         for (i = 0; i < nr_saved; i++) {
292                 fo = batch[i];
293                 if (fo) {
294                         ofd_write_unlock(env, fo);
295                         ofd_object_put(env, fo);
296                 }
297         }
298         OBD_FREE(batch, nr_saved * sizeof(struct ofd_object *));
299
300         CDEBUG((objects == 0 && rc == 0) ? D_ERROR : D_OTHER,
301                "created %d/%d objects: %d\n", objects, nr_saved, rc);
302
303         LASSERT(ergo(objects == 0, rc < 0));
304         RETURN(objects > 0 ? objects : rc);
305 }
306
307 /*
308  * If the object still has SUID+SGID bits set (see ofd_precreate_object()) then
309  * we will accept the UID+GID if sent by the client for initializing the
310  * ownership of this object.  We only allow this to happen once (so clear these
311  * bits) and later only allow setattr.
312  */
313 int ofd_attr_handle_ugid(const struct lu_env *env, struct ofd_object *fo,
314                          struct lu_attr *la, int is_setattr)
315 {
316         struct ofd_thread_info  *info = ofd_info(env);
317         struct lu_attr          *ln = &info->fti_attr2;
318         __u32                    mask = 0;
319         int                      rc;
320
321         ENTRY;
322
323         if (!(la->la_valid & LA_UID) && !(la->la_valid & LA_GID))
324                 RETURN(0);
325
326         rc = dt_attr_get(env, ofd_object_child(fo), ln, BYPASS_CAPA);
327         if (rc != 0)
328                 RETURN(rc);
329
330         LASSERT(ln->la_valid & LA_MODE);
331
332         if (!is_setattr) {
333                 if (!(ln->la_mode & S_ISUID))
334                         la->la_valid &= ~LA_UID;
335                 if (!(ln->la_mode & S_ISGID))
336                         la->la_valid &= ~LA_GID;
337         }
338
339         if ((la->la_valid & LA_UID) && (ln->la_mode & S_ISUID))
340                 mask |= S_ISUID;
341         if ((la->la_valid & LA_GID) && (ln->la_mode & S_ISGID))
342                 mask |= S_ISGID;
343         if (mask != 0) {
344                 if (!(la->la_valid & LA_MODE) || !is_setattr) {
345                         la->la_mode = ln->la_mode;
346                         la->la_valid |= LA_MODE;
347                 }
348                 la->la_mode &= ~mask;
349         }
350
351         RETURN(0);
352 }
353
354 int ofd_attr_set(const struct lu_env *env, struct ofd_object *fo,
355                  struct lu_attr *la, struct filter_fid *ff)
356 {
357         struct ofd_thread_info  *info = ofd_info(env);
358         struct ofd_device       *ofd = ofd_obj2dev(fo);
359         struct thandle          *th;
360         struct ofd_mod_data     *fmd;
361         int                      ff_needed = 0;
362         int                      rc;
363         ENTRY;
364
365         ofd_write_lock(env, fo);
366         if (!ofd_object_exists(fo))
367                 GOTO(unlock, rc = -ENOENT);
368
369         if (la->la_valid & (LA_ATIME | LA_MTIME | LA_CTIME)) {
370                 fmd = ofd_fmd_get(info->fti_exp, &fo->ofo_header.loh_fid);
371                 if (fmd && fmd->fmd_mactime_xid < info->fti_xid)
372                         fmd->fmd_mactime_xid = info->fti_xid;
373                 ofd_fmd_put(info->fti_exp, fmd);
374         }
375
376         /* VBR: version recovery check */
377         rc = ofd_version_get_check(info, fo);
378         if (rc)
379                 GOTO(unlock, rc);
380
381         rc = ofd_attr_handle_ugid(env, fo, la, 1 /* is_setattr */);
382         if (rc != 0)
383                 GOTO(unlock, rc);
384
385         if (ff != NULL) {
386                 rc = ofd_object_ff_check(env, fo);
387                 if (rc == -ENODATA)
388                         ff_needed = 1;
389                 else if (rc < 0)
390                         GOTO(unlock, rc);
391         }
392
393         th = ofd_trans_create(env, ofd);
394         if (IS_ERR(th))
395                 GOTO(unlock, rc = PTR_ERR(th));
396
397         rc = dt_declare_attr_set(env, ofd_object_child(fo), la, th);
398         if (rc)
399                 GOTO(stop, rc);
400
401         if (ff_needed) {
402                 info->fti_buf.lb_buf = ff;
403                 info->fti_buf.lb_len = sizeof(*ff);
404                 rc = dt_declare_xattr_set(env, ofd_object_child(fo),
405                                           &info->fti_buf, XATTR_NAME_FID, 0,
406                                           th);
407                 if (rc)
408                         GOTO(stop, rc);
409         }
410
411         rc = ofd_trans_start(env, ofd, la->la_valid & LA_SIZE ? fo : NULL, th);
412         if (rc)
413                 GOTO(stop, rc);
414
415         rc = dt_attr_set(env, ofd_object_child(fo), la, th,
416                          ofd_object_capa(env, fo));
417         if (rc)
418                 GOTO(stop, rc);
419
420         if (ff_needed)
421                 rc = dt_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
422                                   XATTR_NAME_FID, 0, th, BYPASS_CAPA);
423
424 stop:
425         ofd_trans_stop(env, ofd, th, rc);
426 unlock:
427         ofd_write_unlock(env, fo);
428         RETURN(rc);
429 }
430
431 int ofd_object_punch(const struct lu_env *env, struct ofd_object *fo,
432                      __u64 start, __u64 end, struct lu_attr *la,
433                      struct filter_fid *ff)
434 {
435         struct ofd_thread_info  *info = ofd_info(env);
436         struct ofd_device       *ofd = ofd_obj2dev(fo);
437         struct ofd_mod_data     *fmd;
438         struct dt_object        *dob = ofd_object_child(fo);
439         struct thandle          *th;
440         int                      ff_needed = 0;
441         int                      rc;
442
443         ENTRY;
444
445         /* we support truncate, not punch yet */
446         LASSERT(end == OBD_OBJECT_EOF);
447
448         fmd = ofd_fmd_get(info->fti_exp, &fo->ofo_header.loh_fid);
449         if (fmd && fmd->fmd_mactime_xid < info->fti_xid)
450                 fmd->fmd_mactime_xid = info->fti_xid;
451         ofd_fmd_put(info->fti_exp, fmd);
452
453         ofd_write_lock(env, fo);
454         if (!ofd_object_exists(fo))
455                 GOTO(unlock, rc = -ENOENT);
456
457         /* VBR: version recovery check */
458         rc = ofd_version_get_check(info, fo);
459         if (rc)
460                 GOTO(unlock, rc);
461
462         rc = ofd_attr_handle_ugid(env, fo, la, 0 /* !is_setattr */);
463         if (rc != 0)
464                 GOTO(unlock, rc);
465
466         if (ff != NULL) {
467                 rc = ofd_object_ff_check(env, fo);
468                 if (rc == -ENODATA)
469                         ff_needed = 1;
470                 else if (rc < 0)
471                         GOTO(unlock, rc);
472         }
473
474         th = ofd_trans_create(env, ofd);
475         if (IS_ERR(th))
476                 GOTO(unlock, rc = PTR_ERR(th));
477
478         rc = dt_declare_attr_set(env, dob, la, th);
479         if (rc)
480                 GOTO(stop, rc);
481
482         rc = dt_declare_punch(env, dob, start, OBD_OBJECT_EOF, th);
483         if (rc)
484                 GOTO(stop, rc);
485
486         if (ff_needed) {
487                 info->fti_buf.lb_buf = ff;
488                 info->fti_buf.lb_len = sizeof(*ff);
489                 rc = dt_declare_xattr_set(env, ofd_object_child(fo),
490                                           &info->fti_buf, XATTR_NAME_FID, 0,
491                                           th);
492                 if (rc)
493                         GOTO(stop, rc);
494         }
495
496         rc = ofd_trans_start(env, ofd, fo, th);
497         if (rc)
498                 GOTO(stop, rc);
499
500         rc = dt_punch(env, dob, start, OBD_OBJECT_EOF, th,
501                       ofd_object_capa(env, fo));
502         if (rc)
503                 GOTO(stop, rc);
504
505         rc = dt_attr_set(env, dob, la, th, ofd_object_capa(env, fo));
506         if (rc)
507                 GOTO(stop, rc);
508
509         if (ff_needed)
510                 rc = dt_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
511                                   XATTR_NAME_FID, 0, th, BYPASS_CAPA);
512
513 stop:
514         ofd_trans_stop(env, ofd, th, rc);
515 unlock:
516         ofd_write_unlock(env, fo);
517         RETURN(rc);
518 }
519
520 int ofd_object_destroy(const struct lu_env *env, struct ofd_object *fo,
521                        int orphan)
522 {
523         struct ofd_device       *ofd = ofd_obj2dev(fo);
524         struct thandle          *th;
525         int                      rc = 0;
526
527         ENTRY;
528
529         ofd_write_lock(env, fo);
530         if (!ofd_object_exists(fo))
531                 GOTO(unlock, rc = -ENOENT);
532
533         th = ofd_trans_create(env, ofd);
534         if (IS_ERR(th))
535                 GOTO(unlock, rc = PTR_ERR(th));
536
537         dt_declare_ref_del(env, ofd_object_child(fo), th);
538         dt_declare_destroy(env, ofd_object_child(fo), th);
539         if (orphan)
540                 rc = dt_trans_start_local(env, ofd->ofd_osd, th);
541         else
542                 rc = ofd_trans_start(env, ofd, NULL, th);
543         if (rc)
544                 GOTO(stop, rc);
545
546         ofd_fmd_drop(ofd_info(env)->fti_exp, &fo->ofo_header.loh_fid);
547
548         dt_ref_del(env, ofd_object_child(fo), th);
549         dt_destroy(env, ofd_object_child(fo), th);
550 stop:
551         ofd_trans_stop(env, ofd, th, rc);
552 unlock:
553         ofd_write_unlock(env, fo);
554         RETURN(rc);
555 }
556
557 int ofd_attr_get(const struct lu_env *env, struct ofd_object *fo,
558                  struct lu_attr *la)
559 {
560         int rc = 0;
561
562         ENTRY;
563
564         if (ofd_object_exists(fo)) {
565                 rc = dt_attr_get(env, ofd_object_child(fo), la,
566                                  ofd_object_capa(env, fo));
567
568 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0)
569                 /* Try to correct for a bug in 2.1.0 (LU-221) that caused
570                  * negative timestamps to appear to be in the far future,
571                  * due old timestamp being stored on disk as an unsigned value.
572                  * This fixes up any bad values stored on disk before
573                  * returning them to the client, and ensures any timestamp
574                  * updates are correct.  LU-1042 */
575                 if (unlikely(la->la_atime == LU221_BAD_TIME))
576                         la->la_atime = 0;
577                 if (unlikely(la->la_mtime == LU221_BAD_TIME))
578                         la->la_mtime = 0;
579                 if (unlikely(la->la_ctime == LU221_BAD_TIME))
580                         la->la_ctime = 0;
581 #else
582 #warning "remove old LU-221/LU-1042 workaround code"
583 #endif
584         } else {
585                 rc = -ENOENT;
586         }
587         RETURN(rc);
588 }