Whamcloud - gitweb
LU-3030 build: Update Master Copyrights pre 2.4 split
[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 = (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(ostid_seq(&oseq->os_oi)) &&
164                      (id + nr) >= IDIF_MAX_OID)) {
165                 CERROR("%s:"DOSTID" hit the IDIF_MAX_OID (1<<48)!\n",
166                        ofd_name(ofd), id, ostid_seq(&oseq->os_oi));
167                 RETURN(rc = -ENOSPC);
168         } else if (unlikely(!fid_seq_is_mdt0(ostid_seq(&oseq->os_oi)) &&
169                             (id + nr) >= OBIF_MAX_OID)) {
170                 CERROR("%s:"DOSTID" hit the OBIF_MAX_OID (1<<32)!\n",
171                        ofd_name(ofd), id, ostid_seq(&oseq->os_oi));
172                 RETURN(rc = -ENOSPC);
173         }
174
175         OBD_ALLOC(batch, nr_saved * sizeof(struct ofd_object *));
176         if (batch == NULL)
177                 RETURN(-ENOMEM);
178
179         info->fti_attr.la_valid = LA_TYPE | LA_MODE;
180         /*
181          * We mark object SUID+SGID to flag it for accepting UID+GID from
182          * client on first write.  Currently the permission bits on the OST are
183          * never used, so this is OK.
184          */
185         info->fti_attr.la_mode = S_IFREG | S_ISUID | S_ISGID | 0666;
186         info->fti_dof.dof_type = dt_mode_to_dft(S_IFREG);
187
188         /* Initialize a/c/m time so any client timestamp will always
189          * be newer and update the inode. ctime = 0 is also handled
190          * specially in osd_inode_setattr(). See LU-221, LU-1042 */
191         info->fti_attr.la_valid |= LA_ATIME | LA_MTIME | LA_CTIME;
192         info->fti_attr.la_atime = 0;
193         info->fti_attr.la_mtime = 0;
194         info->fti_attr.la_ctime = 0;
195
196         /* prepare objects */
197         ostid_set_seq(&info->fti_ostid, ostid_seq(&oseq->os_oi));
198         for (i = 0; i < nr; i++) {
199                 ostid_set_id(&info->fti_ostid, id + i);
200                 rc = ostid_to_fid(&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", ostid_seq(&oseq->os_oi), 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, "%s: create new object "DFID" nr %d\n",
264                ofd_name(ofd), PFID(&info->fti_fid), nr);
265
266         for (i = 0; i < nr; i++) {
267                 fo = batch[i];
268                 LASSERT(fo);
269
270                 if (likely(!ofd_object_exists(fo))) {
271                         next = ofd_object_child(fo);
272                         LASSERT(next != NULL);
273
274                         rc = dt_create(env, next, &info->fti_attr, NULL,
275                                        &info->fti_dof, th);
276                         if (rc)
277                                 break;
278                         LASSERT(ofd_object_exists(fo));
279                 }
280                 ofd_seq_last_oid_set(oseq, id + i);
281         }
282
283         objects = i;
284         if (objects > 0) {
285                 tmp = cpu_to_le64(ofd_seq_last_oid(oseq));
286                 rc = dt_record_write(env, oseq->os_lastid_obj,
287                                      &info->fti_buf, &info->fti_off, th);
288         }
289 trans_stop:
290         ofd_trans_stop(env, ofd, th, rc);
291 out:
292         for (i = 0; i < nr_saved; i++) {
293                 fo = batch[i];
294                 if (fo) {
295                         ofd_write_unlock(env, fo);
296                         ofd_object_put(env, fo);
297                 }
298         }
299         OBD_FREE(batch, nr_saved * sizeof(struct ofd_object *));
300
301         CDEBUG((objects == 0 && rc == 0) ? D_ERROR : D_OTHER,
302                "created %d/%d objects: %d\n", objects, nr_saved, rc);
303
304         LASSERT(ergo(objects == 0, rc < 0));
305         RETURN(objects > 0 ? objects : rc);
306 }
307
308 /*
309  * If the object still has SUID+SGID bits set (see ofd_precreate_object()) then
310  * we will accept the UID+GID if sent by the client for initializing the
311  * ownership of this object.  We only allow this to happen once (so clear these
312  * bits) and later only allow setattr.
313  */
314 int ofd_attr_handle_ugid(const struct lu_env *env, struct ofd_object *fo,
315                          struct lu_attr *la, int is_setattr)
316 {
317         struct ofd_thread_info  *info = ofd_info(env);
318         struct lu_attr          *ln = &info->fti_attr2;
319         __u32                    mask = 0;
320         int                      rc;
321
322         ENTRY;
323
324         if (!(la->la_valid & LA_UID) && !(la->la_valid & LA_GID))
325                 RETURN(0);
326
327         rc = dt_attr_get(env, ofd_object_child(fo), ln, BYPASS_CAPA);
328         if (rc != 0)
329                 RETURN(rc);
330
331         LASSERT(ln->la_valid & LA_MODE);
332
333         if (!is_setattr) {
334                 if (!(ln->la_mode & S_ISUID))
335                         la->la_valid &= ~LA_UID;
336                 if (!(ln->la_mode & S_ISGID))
337                         la->la_valid &= ~LA_GID;
338         }
339
340         if ((la->la_valid & LA_UID) && (ln->la_mode & S_ISUID))
341                 mask |= S_ISUID;
342         if ((la->la_valid & LA_GID) && (ln->la_mode & S_ISGID))
343                 mask |= S_ISGID;
344         if (mask != 0) {
345                 if (!(la->la_valid & LA_MODE) || !is_setattr) {
346                         la->la_mode = ln->la_mode;
347                         la->la_valid |= LA_MODE;
348                 }
349                 la->la_mode &= ~mask;
350         }
351
352         RETURN(0);
353 }
354
355 int ofd_attr_set(const struct lu_env *env, struct ofd_object *fo,
356                  struct lu_attr *la, struct filter_fid *ff)
357 {
358         struct ofd_thread_info  *info = ofd_info(env);
359         struct ofd_device       *ofd = ofd_obj2dev(fo);
360         struct thandle          *th;
361         struct ofd_mod_data     *fmd;
362         int                      ff_needed = 0;
363         int                      rc;
364         ENTRY;
365
366         ofd_write_lock(env, fo);
367         if (!ofd_object_exists(fo))
368                 GOTO(unlock, rc = -ENOENT);
369
370         if (la->la_valid & (LA_ATIME | LA_MTIME | LA_CTIME)) {
371                 fmd = ofd_fmd_get(info->fti_exp, &fo->ofo_header.loh_fid);
372                 if (fmd && fmd->fmd_mactime_xid < info->fti_xid)
373                         fmd->fmd_mactime_xid = info->fti_xid;
374                 ofd_fmd_put(info->fti_exp, fmd);
375         }
376
377         /* VBR: version recovery check */
378         rc = ofd_version_get_check(info, fo);
379         if (rc)
380                 GOTO(unlock, rc);
381
382         rc = ofd_attr_handle_ugid(env, fo, la, 1 /* is_setattr */);
383         if (rc != 0)
384                 GOTO(unlock, rc);
385
386         if (ff != NULL) {
387                 rc = ofd_object_ff_check(env, fo);
388                 if (rc == -ENODATA)
389                         ff_needed = 1;
390                 else if (rc < 0)
391                         GOTO(unlock, rc);
392         }
393
394         th = ofd_trans_create(env, ofd);
395         if (IS_ERR(th))
396                 GOTO(unlock, rc = PTR_ERR(th));
397
398         rc = dt_declare_attr_set(env, ofd_object_child(fo), la, th);
399         if (rc)
400                 GOTO(stop, rc);
401
402         if (ff_needed) {
403                 info->fti_buf.lb_buf = ff;
404                 info->fti_buf.lb_len = sizeof(*ff);
405                 rc = dt_declare_xattr_set(env, ofd_object_child(fo),
406                                           &info->fti_buf, XATTR_NAME_FID, 0,
407                                           th);
408                 if (rc)
409                         GOTO(stop, rc);
410         }
411
412         rc = ofd_trans_start(env, ofd, la->la_valid & LA_SIZE ? fo : NULL, th);
413         if (rc)
414                 GOTO(stop, rc);
415
416         rc = dt_attr_set(env, ofd_object_child(fo), la, th,
417                          ofd_object_capa(env, fo));
418         if (rc)
419                 GOTO(stop, rc);
420
421         if (ff_needed)
422                 rc = dt_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
423                                   XATTR_NAME_FID, 0, th, BYPASS_CAPA);
424
425 stop:
426         ofd_trans_stop(env, ofd, th, rc);
427 unlock:
428         ofd_write_unlock(env, fo);
429         RETURN(rc);
430 }
431
432 int ofd_object_punch(const struct lu_env *env, struct ofd_object *fo,
433                      __u64 start, __u64 end, struct lu_attr *la,
434                      struct filter_fid *ff)
435 {
436         struct ofd_thread_info  *info = ofd_info(env);
437         struct ofd_device       *ofd = ofd_obj2dev(fo);
438         struct ofd_mod_data     *fmd;
439         struct dt_object        *dob = ofd_object_child(fo);
440         struct thandle          *th;
441         int                      ff_needed = 0;
442         int                      rc;
443
444         ENTRY;
445
446         /* we support truncate, not punch yet */
447         LASSERT(end == OBD_OBJECT_EOF);
448
449         fmd = ofd_fmd_get(info->fti_exp, &fo->ofo_header.loh_fid);
450         if (fmd && fmd->fmd_mactime_xid < info->fti_xid)
451                 fmd->fmd_mactime_xid = info->fti_xid;
452         ofd_fmd_put(info->fti_exp, fmd);
453
454         ofd_write_lock(env, fo);
455         if (!ofd_object_exists(fo))
456                 GOTO(unlock, rc = -ENOENT);
457
458         /* VBR: version recovery check */
459         rc = ofd_version_get_check(info, fo);
460         if (rc)
461                 GOTO(unlock, rc);
462
463         rc = ofd_attr_handle_ugid(env, fo, la, 0 /* !is_setattr */);
464         if (rc != 0)
465                 GOTO(unlock, rc);
466
467         if (ff != NULL) {
468                 rc = ofd_object_ff_check(env, fo);
469                 if (rc == -ENODATA)
470                         ff_needed = 1;
471                 else if (rc < 0)
472                         GOTO(unlock, rc);
473         }
474
475         th = ofd_trans_create(env, ofd);
476         if (IS_ERR(th))
477                 GOTO(unlock, rc = PTR_ERR(th));
478
479         rc = dt_declare_attr_set(env, dob, la, th);
480         if (rc)
481                 GOTO(stop, rc);
482
483         rc = dt_declare_punch(env, dob, start, OBD_OBJECT_EOF, th);
484         if (rc)
485                 GOTO(stop, rc);
486
487         if (ff_needed) {
488                 info->fti_buf.lb_buf = ff;
489                 info->fti_buf.lb_len = sizeof(*ff);
490                 rc = dt_declare_xattr_set(env, ofd_object_child(fo),
491                                           &info->fti_buf, XATTR_NAME_FID, 0,
492                                           th);
493                 if (rc)
494                         GOTO(stop, rc);
495         }
496
497         rc = ofd_trans_start(env, ofd, fo, th);
498         if (rc)
499                 GOTO(stop, rc);
500
501         rc = dt_punch(env, dob, start, OBD_OBJECT_EOF, th,
502                       ofd_object_capa(env, fo));
503         if (rc)
504                 GOTO(stop, rc);
505
506         rc = dt_attr_set(env, dob, la, th, ofd_object_capa(env, fo));
507         if (rc)
508                 GOTO(stop, rc);
509
510         if (ff_needed)
511                 rc = dt_xattr_set(env, ofd_object_child(fo), &info->fti_buf,
512                                   XATTR_NAME_FID, 0, th, BYPASS_CAPA);
513
514 stop:
515         ofd_trans_stop(env, ofd, th, rc);
516 unlock:
517         ofd_write_unlock(env, fo);
518         RETURN(rc);
519 }
520
521 int ofd_object_destroy(const struct lu_env *env, struct ofd_object *fo,
522                        int orphan)
523 {
524         struct ofd_device       *ofd = ofd_obj2dev(fo);
525         struct thandle          *th;
526         int                      rc = 0;
527
528         ENTRY;
529
530         ofd_write_lock(env, fo);
531         if (!ofd_object_exists(fo))
532                 GOTO(unlock, rc = -ENOENT);
533
534         th = ofd_trans_create(env, ofd);
535         if (IS_ERR(th))
536                 GOTO(unlock, rc = PTR_ERR(th));
537
538         dt_declare_ref_del(env, ofd_object_child(fo), th);
539         dt_declare_destroy(env, ofd_object_child(fo), th);
540         if (orphan)
541                 rc = dt_trans_start_local(env, ofd->ofd_osd, th);
542         else
543                 rc = ofd_trans_start(env, ofd, NULL, th);
544         if (rc)
545                 GOTO(stop, rc);
546
547         ofd_fmd_drop(ofd_info(env)->fti_exp, &fo->ofo_header.loh_fid);
548
549         dt_ref_del(env, ofd_object_child(fo), th);
550         dt_destroy(env, ofd_object_child(fo), th);
551 stop:
552         ofd_trans_stop(env, ofd, th, rc);
553 unlock:
554         ofd_write_unlock(env, fo);
555         RETURN(rc);
556 }
557
558 int ofd_attr_get(const struct lu_env *env, struct ofd_object *fo,
559                  struct lu_attr *la)
560 {
561         int rc = 0;
562
563         ENTRY;
564
565         if (ofd_object_exists(fo)) {
566                 rc = dt_attr_get(env, ofd_object_child(fo), la,
567                                  ofd_object_capa(env, fo));
568
569 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 7, 50, 0)
570                 /* Try to correct for a bug in 2.1.0 (LU-221) that caused
571                  * negative timestamps to appear to be in the far future,
572                  * due old timestamp being stored on disk as an unsigned value.
573                  * This fixes up any bad values stored on disk before
574                  * returning them to the client, and ensures any timestamp
575                  * updates are correct.  LU-1042 */
576                 if (unlikely(la->la_atime == LU221_BAD_TIME))
577                         la->la_atime = 0;
578                 if (unlikely(la->la_mtime == LU221_BAD_TIME))
579                         la->la_mtime = 0;
580                 if (unlikely(la->la_ctime == LU221_BAD_TIME))
581                         la->la_ctime = 0;
582 #else
583 #warning "remove old LU-221/LU-1042 workaround code"
584 #endif
585         } else {
586                 rc = -ENOENT;
587         }
588         RETURN(rc);
589 }