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