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