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