summaryrefslogtreecommitdiffstats
path: root/xlators/features/utime/src/utime.c
blob: 8cae0fe43c87bb8f812f9f4f90dbe092550a948f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
  Copyright (c) 2018 Red Hat, Inc. <http://www.redhat.com>
  This file is part of GlusterFS.

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/

#include "utime.h"
#include "utime-helpers.h"
#include "utime-messages.h"
#include "utime-mem-types.h"
#include <glusterfs/call-stub.h>

int32_t
gf_utime_invalidate(xlator_t *this, inode_t *inode)
{
    return 0;
}

int32_t
gf_utime_forget(xlator_t *this, inode_t *inode)
{
    return 0;
}

int32_t
gf_utime_client_destroy(xlator_t *this, client_t *client)
{
    return 0;
}

void
gf_utime_ictxmerge(xlator_t *this, fd_t *fd, inode_t *inode,
                   inode_t *linked_inode)
{
    return;
}

int32_t
gf_utime_release(xlator_t *this, fd_t *fd)
{
    return 0;
}

int32_t
gf_utime_releasedir(xlator_t *this, fd_t *fd)
{
    return 0;
}

int32_t
gf_utime_client_disconnect(xlator_t *this, client_t *client)
{
    return 0;
}

int32_t
gf_utime_fdctx_to_dict(xlator_t *this, fd_t *fd, dict_t *dict)
{
    return 0;
}

int32_t
gf_utime_inode(xlator_t *this)
{
    return 0;
}

int32_t
gf_utime_inode_to_dict(xlator_t *this, dict_t *dict)
{
    return 0;
}

int32_t
gf_utime_history(xlator_t *this)
{
    return 0;
}

int32_t
gf_utime_fd(xlator_t *this)
{
    return 0;
}

int32_t
gf_utime_fd_to_dict(xlator_t *this, dict_t *dict)
{
    return 0;
}

int32_t
gf_utime_fdctx(xlator_t *this, fd_t *fd)
{
    return 0;
}

int32_t
gf_utime_inodectx(xlator_t *this, inode_t *ino)
{
    return 0;
}

int32_t
gf_utime_inodectx_to_dict(xlator_t *this, inode_t *ino, dict_t *dict)
{
    return 0;
}

int32_t
gf_utime_priv_to_dict(xlator_t *this, dict_t *dict, char *brickname)
{
    return 0;
}

int32_t
gf_utime_priv(xlator_t *this)
{
    return 0;
}

int32_t
mem_acct_init(xlator_t *this)
{
    if (xlator_mem_acct_init(this, utime_mt_end + 1) != 0) {
        gf_msg(this->name, GF_LOG_ERROR, ENOMEM, UTIME_MSG_NO_MEMORY,
               "Memory accounting initialization failed.");
        return -1;
    }
    return 0;
}

int32_t
gf_utime_set_mdata_setxattr_cbk(call_frame_t *frame, void *cookie,
                                xlator_t *this, int op_ret, int op_errno,
                                dict_t *xdata)
{
    call_stub_t *stub = frame->local;
    /* Don't fail lookup if mdata setxattr fails */
    if (op_ret) {
        gf_msg(this->name, GF_LOG_ERROR, op_errno, UTIME_MSG_SET_MDATA_FAILED,
               "dict set of key for set-ctime-mdata failed");
    }
    frame->local = NULL;
    call_resume(stub);
    STACK_DESTROY(frame->root);
    return 0;
}

int32_t
gf_utime_set_mdata_lookup_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
                              int32_t op_ret, int32_t op_errno, inode_t *inode,
                              struct iatt *stbuf, dict_t *xdata,
                              struct iatt *postparent)
{
    dict_t *dict = NULL;
    struct mdata_iatt *mdata = NULL;
    int ret = 0;
    loc_t loc = {
        0,
    };
    call_frame_t *new_frame = NULL;

    if (!op_ret && dict_get(xdata, GF_XATTR_MDATA_KEY) == NULL) {
        dict = dict_new();
        if (!dict) {
            op_errno = ENOMEM;
            goto err;
        }
        mdata = GF_MALLOC(sizeof(struct mdata_iatt), gf_common_mt_char);
        if (mdata == NULL) {
            op_errno = ENOMEM;
            goto err;
        }
        iatt_to_mdata(mdata, stbuf);
        ret = dict_set_mdata(dict, CTIME_MDATA_XDATA_KEY, mdata, _gf_false);
        if (ret < 0) {
            gf_msg(this->name, GF_LOG_WARNING, ENOMEM, UTIME_MSG_NO_MEMORY,
                   "dict set of key for set-ctime-mdata failed");
            goto err;
        }
        new_frame = copy_frame(frame);
        if (!new_frame) {
            op_errno = ENOMEM;
            goto stub_err;
        }

        new_frame->local = fop_lookup_cbk_stub(frame, default_lookup_cbk,
                                               op_ret, op_errno, inode, stbuf,
                                               xdata, postparent);
        if (!new_frame->local) {
            gf_msg(this->name, GF_LOG_WARNING, ENOMEM, UTIME_MSG_NO_MEMORY,
                   "lookup_cbk stub allocation failed");
            op_errno = ENOMEM;
            STACK_DESTROY(new_frame->root);
            goto stub_err;
        }

        loc.inode = inode_ref(inode);
        gf_uuid_copy(loc.gfid, stbuf->ia_gfid);

        new_frame->root->uid = 0;
        new_frame->root->gid = 0;
        new_frame->root->pid = GF_CLIENT_PID_SET_UTIME;
        STACK_WIND(new_frame, gf_utime_set_mdata_setxattr_cbk,
                   FIRST_CHILD(this), FIRST_CHILD(this)->fops->setxattr, &loc,
                   dict, 0, NULL);

        dict_unref(dict);
        inode_unref(loc.inode);
        return 0;
    }

    STACK_UNWIND_STRICT(lookup, frame, op_ret, op_errno, inode, stbuf, xdata,
                        postparent);
    return 0;

err:
    if (mdata) {
        GF_FREE(mdata);
    }
stub_err:
    if (dict) {
        dict_unref(dict);
    }
    STACK_UNWIND_STRICT(lookup, frame, -1, op_errno, NULL, NULL, NULL, NULL);
    return 0;
}

int
gf_utime_lookup(call_frame_t *frame, xlator_t *this, loc_t *loc, dict_t *xdata)
{
    int op_errno = -1;
    int ret = -1;

    VALIDATE_OR_GOTO(frame, err);
    VALIDATE_OR_GOTO(this, err);
    VALIDATE_OR_GOTO(loc, err);
    VALIDATE_OR_GOTO(loc->inode, err);

    xdata = xdata ? dict_ref(xdata) : dict_new();
    if (!xdata) {
        op_errno = ENOMEM;
        goto err;
    }

    ret = dict_set_int8(xdata, GF_XATTR_MDATA_KEY, 1);
    if (ret < 0) {
        gf_msg(this->name, GF_LOG_WARNING, -ret, UTIME_MSG_DICT_SET_FAILED,
               "%s: Unable to set dict value for %s", loc->path,
               GF_XATTR_MDATA_KEY);
        op_errno = -ret;
        goto free_dict;
    }

    STACK_WIND(frame, gf_utime_set_mdata_lookup_cbk, FIRST_CHILD(this),
               FIRST_CHILD(this)->fops->lookup, loc, xdata);
    dict_unref(xdata);
    return 0;

free_dict:
    dict_unref(xdata);
err:
    STACK_UNWIND_STRICT(lookup, frame, -1, op_errno, NULL, NULL, NULL, NULL);
    return 0;
}

int32_t
init(xlator_t *this)
{
    utime_priv_t *utime = NULL;

    utime = GF_MALLOC(sizeof(*utime), utime_mt_utime_t);
    if (utime == NULL) {
        gf_msg(this->name, GF_LOG_ERROR, ENOMEM, UTIME_MSG_NO_MEMORY,
               "Failed to allocate private memory.");
        return -1;
    }
    memset(utime, 0, sizeof(*utime));

    this->private = utime;
    GF_OPTION_INIT("noatime", utime->noatime, bool, err);

    return 0;
err:
    return -1;
}

void
fini(xlator_t *this)
{
    utime_priv_t *utime = NULL;

    utime = this->private;
    GF_FREE(utime);
    return;
}

int32_t
reconfigure(xlator_t *this, dict_t *options)
{
    utime_priv_t *utime = this->private;

    GF_OPTION_RECONF("noatime", utime->noatime, options, bool, err);

    return 0;
err:
    return -1;
}

int
notify(xlator_t *this, int event, void *data, ...)
{
    return default_notify(this, event, data);
}

struct xlator_fops fops = {
    .rename = gf_utime_rename,
    .mknod = gf_utime_mknod,
    .readv = gf_utime_readv,
    .fremovexattr = gf_utime_fremovexattr,
    .open = gf_utime_open,
    .create = gf_utime_create,
    .mkdir = gf_utime_mkdir,
    .writev = gf_utime_writev,
    .rmdir = gf_utime_rmdir,
    .fallocate = gf_utime_fallocate,
    .truncate = gf_utime_truncate,
    .symlink = gf_utime_symlink,
    .zerofill = gf_utime_zerofill,
    .link = gf_utime_link,
    .ftruncate = gf_utime_ftruncate,
    .unlink = gf_utime_unlink,
    .setattr = gf_utime_setattr,
    .fsetattr = gf_utime_fsetattr,
    .opendir = gf_utime_opendir,
    .removexattr = gf_utime_removexattr,
    .lookup = gf_utime_lookup,
};
struct xlator_cbks cbks = {
    .invalidate = gf_utime_invalidate,
    .forget = gf_utime_forget,
    .client_destroy = gf_utime_client_destroy,
    .ictxmerge = gf_utime_ictxmerge,
    .release = gf_utime_release,
    .releasedir = gf_utime_releasedir,
    .client_disconnect = gf_utime_client_disconnect,
};
struct xlator_dumpops dumpops = {
    .fdctx_to_dict = gf_utime_fdctx_to_dict,
    .inode = gf_utime_inode,
    .inode_to_dict = gf_utime_inode_to_dict,
    .history = gf_utime_history,
    .fd = gf_utime_fd,
    .fd_to_dict = gf_utime_fd_to_dict,
    .fdctx = gf_utime_fdctx,
    .inodectx = gf_utime_inodectx,
    .inodectx_to_dict = gf_utime_inodectx_to_dict,
    .priv_to_dict = gf_utime_priv_to_dict,
    .priv = gf_utime_priv,
};

struct volume_options options[] = {
    {.key = {"noatime"},
     .type = GF_OPTION_TYPE_BOOL,
     .default_value = "on",
     .op_version = {GD_OP_VERSION_5_0},
     .flags = OPT_FLAG_SETTABLE | OPT_FLAG_CLIENT_OPT | OPT_FLAG_DOC,
     .tags = {"ctime"},
     .description = "Enable/Disable atime updation when ctime feature is "
                    "enabled. When noatime is on, atime is not updated with "
                    "ctime feature enabled and vice versa."},
    {.key = {NULL}}};

xlator_api_t xlator_api = {
    .init = init,
    .fini = fini,
    .notify = notify,
    .reconfigure = reconfigure,
    .mem_acct_init = mem_acct_init,
    .op_version = {GD_OP_VERSION_5_0},
    .dumpops = &dumpops,
    .fops = &fops,
    .cbks = &cbks,
    .options = options,
    .identifier = "utime",
    .category = GF_MAINTAINED,
};