Whamcloud - gitweb
LU-1146 build: batch update copyright messages
[fs/lustre-release.git] / lustre / liblustre / file.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lustre/liblustre/file.c
39  *
40  * Lustre Light file operations
41  */
42
43 #define DEBUG_SUBSYSTEM S_LLITE
44
45 #include <stdlib.h>
46 #include <string.h>
47 #include <assert.h>
48 #include <time.h>
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <sys/queue.h>
52 #include <fcntl.h>
53
54 #include "llite_lib.h"
55
56 /* Pack the required supplementary groups into the supplied groups array.
57  * If we don't need to use the groups from the target inode(s) then we
58  * instead pack one or more groups from the user's supplementary group
59  * array in case it might be useful.  Not needed if doing an MDS-side upcall. */
60 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
61 {
62         LASSERT(i1 != NULL);
63         LASSERT(suppgids != NULL);
64
65         if (cfs_curproc_is_in_groups(i1->i_stbuf.st_gid))
66                 suppgids[0] = i1->i_stbuf.st_gid;
67         else
68                 suppgids[0] = -1;
69
70         if (i2) {
71                 if (cfs_curproc_is_in_groups(i2->i_stbuf.st_gid))
72                         suppgids[1] = i2->i_stbuf.st_gid;
73                 else
74                         suppgids[1] = -1;
75         } else {
76                 suppgids[1] = -1;
77         }
78 }
79
80 void llu_prep_md_op_data(struct md_op_data *op_data, struct inode *i1,
81                          struct inode *i2, const char *name, int namelen,
82                          int mode, __u32 opc)
83 {
84         LASSERT(i1 != NULL || i2 != NULL);
85         LASSERT(op_data);
86
87         if (i1) {
88                 ll_i2gids(op_data->op_suppgids, i1, i2);
89                 op_data->op_fid1 = *ll_inode2fid(i1);
90         }else {
91                 ll_i2gids(op_data->op_suppgids, i2, i1);
92                 op_data->op_fid1 = *ll_inode2fid(i2);
93         }
94
95         if (i2)
96                 op_data->op_fid2 = *ll_inode2fid(i2);
97         else
98                 fid_zero(&op_data->op_fid2);
99
100         op_data->op_opc = opc;
101         op_data->op_name = name;
102         op_data->op_mode = mode;
103         op_data->op_namelen = namelen;
104         op_data->op_mod_time = CFS_CURRENT_TIME;
105         op_data->op_data = NULL;
106 }
107
108 void obdo_refresh_inode(struct inode *dst,
109                         struct obdo *src,
110                         obd_flag valid)
111 {
112         struct intnl_stat *st = llu_i2stat(dst);
113         valid &= src->o_valid;
114
115         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
116                 CDEBUG(D_INODE,"valid "LPX64", cur time "CFS_TIME_T"/"CFS_TIME_T
117                        ", new %lu/%lu\n",
118                        src->o_valid, LTIME_S(st->st_mtime),
119                        LTIME_S(st->st_ctime),
120                        (long)src->o_mtime, (long)src->o_ctime);
121
122         if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(st->st_atime))
123                 LTIME_S(st->st_atime) = src->o_atime;
124         if (valid & OBD_MD_FLMTIME && src->o_mtime > LTIME_S(st->st_mtime))
125                 LTIME_S(st->st_mtime) = src->o_mtime;
126         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(st->st_ctime))
127                 LTIME_S(st->st_ctime) = src->o_ctime;
128         if (valid & OBD_MD_FLSIZE && src->o_size > st->st_size)
129                 st->st_size = src->o_size;
130         /* optimum IO size */
131         if (valid & OBD_MD_FLBLKSZ)
132                 st->st_blksize = src->o_blksize;
133         /* allocation of space */
134         if (valid & OBD_MD_FLBLOCKS && src->o_blocks > st->st_blocks)
135                 st->st_blocks = src->o_blocks;
136 }
137
138 /**
139  * Assign an obtained @ioepoch to client's inode. No lock is needed, MDS does
140  * not believe attributes if a few ioepoch holders exist. Attributes for
141  * previous ioepoch if new one is opened are also skipped by MDS.
142  */
143 void llu_ioepoch_open(struct llu_inode_info *lli, __u64 ioepoch)
144 {
145         if (ioepoch && lli->lli_ioepoch != ioepoch) {
146                 lli->lli_ioepoch = ioepoch;
147                 CDEBUG(D_INODE, "Epoch "LPU64" opened on "DFID" for truncate\n",
148                        ioepoch, PFID(&lli->lli_fid));
149         }
150 }
151
152 int llu_local_open(struct llu_inode_info *lli, struct lookup_intent *it)
153 {
154         struct ptlrpc_request *req = it->d.lustre.it_data;
155         struct ll_file_data *fd;
156         struct mdt_body *body;
157         ENTRY;
158
159         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
160         LASSERT(body != NULL);
161
162         /* already opened? */
163         if (lli->lli_open_count++)
164                 RETURN(0);
165
166         LASSERT(!lli->lli_file_data);
167
168         OBD_ALLOC(fd, sizeof(*fd));
169         /* We can't handle this well without reorganizing ll_file_open and
170          * ll_md_close, so don't even try right now. */
171         LASSERT(fd != NULL);
172
173         memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
174         fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
175         fd->fd_mds_och.och_fid   = lli->lli_fid;
176         lli->lli_file_data = fd;
177         llu_ioepoch_open(lli, body->ioepoch);
178         md_set_open_replay_data(lli->lli_sbi->ll_md_exp,
179                                 &fd->fd_mds_och, it->d.lustre.it_data);
180
181         RETURN(0);
182 }
183
184 int llu_iop_open(struct pnode *pnode, int flags, mode_t mode)
185 {
186         struct inode *inode = pnode->p_base->pb_ino;
187         struct llu_inode_info *lli = llu_i2info(inode);
188         struct intnl_stat *st = llu_i2stat(inode);
189         struct ptlrpc_request *request;
190         struct lookup_intent *it;
191         struct lov_stripe_md *lsm;
192         int rc = 0;
193         ENTRY;
194
195         liblustre_wait_event(0);
196
197         /* don't do anything for '/' */
198         if (llu_is_root_inode(inode))
199                 RETURN(0);
200
201         CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu\n", (long long)st->st_ino);
202         LL_GET_INTENT(inode, it);
203
204         if (!it->d.lustre.it_disposition) {
205                 LBUG();
206         }
207
208         rc = it_open_error(DISP_OPEN_OPEN, it);
209         if (rc)
210                 GOTO(out_release, rc);
211
212         rc = llu_local_open(lli, it);
213         if (rc)
214                 LBUG();
215
216         if (!S_ISREG(st->st_mode))
217                 GOTO(out_release, rc = 0);
218
219         lsm = lli->lli_smd;
220         if (lsm)
221                 flags &= ~O_LOV_DELAY_CREATE;
222         /*XXX: open_flags are overwritten and the previous ones are lost */
223         lli->lli_open_flags = flags & ~(O_CREAT | O_EXCL | O_TRUNC);
224
225  out_release:
226         request = it->d.lustre.it_data;
227         ptlrpc_req_finished(request);
228
229         it->it_op_release(it);
230         OBD_FREE(it, sizeof(*it));
231
232         /* libsysio hasn't done anything for O_TRUNC. here we
233          * simply simulate it as open(...); truncate(...); */
234         if (rc == 0 && (flags & O_TRUNC) && S_ISREG(st->st_mode)) {
235                 struct iattr attr;
236
237                 memset(&attr, 0, sizeof(attr));
238                 attr.ia_size = 0;
239                 attr.ia_valid |= ATTR_SIZE | ATTR_RAW;
240                 rc = llu_setattr_raw(inode, &attr);
241                 if (rc)
242                         CERROR("error %d truncate in open()\n", rc);
243         }
244
245         liblustre_wait_event(0);
246         RETURN(rc);
247 }
248
249 int llu_objects_destroy(struct ptlrpc_request *req, struct inode *dir)
250 {
251         struct mdt_body *body;
252         struct lov_mds_md *eadata;
253         struct lov_stripe_md *lsm = NULL;
254         struct obd_trans_info oti = { 0 };
255         struct obdo *oa;
256         int rc;
257         ENTRY;
258
259         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
260
261         if (!(body->valid & OBD_MD_FLEASIZE))
262                 RETURN(0);
263
264         if (body->eadatasize == 0) {
265                 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
266                 GOTO(out, rc = -EPROTO);
267         }
268
269         /* The MDS sent back the EA because we unlinked the last reference
270          * to this file. Use this EA to unlink the objects on the OST.
271          * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
272          * check it is complete and sensible. */
273         eadata = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD,
274                                               body->eadatasize);
275
276         LASSERT(eadata != NULL);
277
278         rc = obd_unpackmd(llu_i2obdexp(dir), &lsm, eadata,body->eadatasize);
279         if (rc < 0) {
280                 CERROR("obd_unpackmd: %d\n", rc);
281                 GOTO(out, rc);
282         }
283         LASSERT(rc >= sizeof(*lsm));
284
285         OBDO_ALLOC(oa);
286         if (oa == NULL)
287                 GOTO(out_free_memmd, rc = -ENOMEM);
288
289         oa->o_id = lsm->lsm_object_id;
290         oa->o_seq = lsm->lsm_object_seq;
291         oa->o_mode = body->mode & S_IFMT;
292         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
293         obdo_from_inode(oa, NULL, &llu_i2info(dir)->lli_fid, 0);
294
295         if (body->valid & OBD_MD_FLCOOKIE) {
296                 oa->o_valid |= OBD_MD_FLCOOKIE;
297                 oti.oti_logcookies =
298                         req_capsule_server_sized_get(&req->rq_pill,
299                                                    &RMF_LOGCOOKIES,
300                                                    sizeof(struct llog_cookie) *
301                                                    lsm->lsm_stripe_count);
302                 if (oti.oti_logcookies == NULL) {
303                         oa->o_valid &= ~OBD_MD_FLCOOKIE;
304                         body->valid &= ~OBD_MD_FLCOOKIE;
305                 }
306         }
307
308         rc = obd_destroy(llu_i2obdexp(dir), oa, lsm, &oti, NULL, NULL);
309         OBDO_FREE(oa);
310         if (rc)
311                 CERROR("obd destroy objid 0x"LPX64" error %d\n",
312                        lsm->lsm_object_id, rc);
313  out_free_memmd:
314         obd_free_memmd(llu_i2obdexp(dir), &lsm);
315  out:
316         return rc;
317 }
318
319 /** Cliens updates SOM attributes on MDS: obd_getattr and md_setattr. */
320 int llu_som_update(struct inode *inode, struct md_op_data *op_data)
321 {
322         struct llu_inode_info *lli = llu_i2info(inode);
323         struct llu_sb_info *sbi = llu_i2sbi(inode);
324         struct obdo oa = { 0 };
325         __u32 old_flags;
326         int rc;
327         ENTRY;
328
329         LASSERT(!(lli->lli_flags & LLIF_MDS_SIZE_LOCK));
330         LASSERT(sbi->ll_lco.lco_flags & OBD_CONNECT_SOM);
331
332         old_flags = op_data->op_flags;
333         op_data->op_flags = MF_SOM_CHANGE;
334
335         /* If inode is already in another epoch, skip getattr from OSTs. */
336         if (lli->lli_ioepoch == op_data->op_ioepoch) {
337                 rc = llu_inode_getattr(inode, &oa, op_data->op_ioepoch,
338                                        old_flags & MF_GETATTR_LOCK);
339                 if (rc) {
340                         oa.o_valid = 0;
341                         if (rc == -ENOENT)
342                                 CDEBUG(D_INODE, "objid "LPX64" is destroyed\n",
343                                        lli->lli_smd->lsm_object_id);
344                         else
345                                 CERROR("inode_getattr failed (%d): unable to "
346                                        "send a Size-on-MDS attribute update "
347                                        "for inode %llu/%lu\n", rc,
348                                        (long long)llu_i2stat(inode)->st_ino,
349                                        lli->lli_st_generation);
350                 }  else {
351                         CDEBUG(D_INODE, "Size-on-MDS update on "DFID"\n",
352                                PFID(&lli->lli_fid));
353                 }
354
355                 /* Install attributes into op_data. */
356                 md_from_obdo(op_data, &oa, oa.o_valid);
357         }
358
359         rc = llu_md_setattr(inode, op_data, NULL);
360         RETURN(rc);
361 }
362
363 void llu_pack_inode2opdata(struct inode *inode, struct md_op_data *op_data,
364                            struct lustre_handle *fh)
365 {
366         struct llu_inode_info *lli = llu_i2info(inode);
367         struct intnl_stat *st = llu_i2stat(inode);
368         ENTRY;
369
370         op_data->op_fid1 = lli->lli_fid;
371         op_data->op_attr.ia_atime = st->st_atime;
372         op_data->op_attr.ia_mtime = st->st_mtime;
373         op_data->op_attr.ia_ctime = st->st_ctime;
374         op_data->op_attr.ia_size = st->st_size;
375         op_data->op_attr_blocks = st->st_blocks;
376         op_data->op_attr.ia_attr_flags = lli->lli_st_flags;
377         op_data->op_ioepoch = lli->lli_ioepoch;
378         if (fh)
379                 op_data->op_handle = *fh;
380         EXIT;
381 }
382
383 /** Pack SOM attributes info @opdata for CLOSE, DONE_WRITING rpc. */
384 void llu_done_writing_attr(struct inode *inode, struct md_op_data *op_data)
385 {
386         struct llu_inode_info *lli = llu_i2info(inode);
387         ENTRY;
388
389         op_data->op_flags |= MF_SOM_CHANGE;
390
391         /* Pack Size-on-MDS attributes if we are in IO
392          * epoch and attributes are valid. */
393         LASSERT(!(lli->lli_flags & LLIF_MDS_SIZE_LOCK));
394         if (!cl_local_size(inode))
395                 op_data->op_attr.ia_valid |= ATTR_MTIME_SET | ATTR_CTIME_SET |
396                         ATTR_ATIME_SET | ATTR_SIZE | ATTR_BLOCKS;
397
398         EXIT;
399 }
400
401 static void llu_prepare_close(struct inode *inode, struct md_op_data *op_data,
402                               struct ll_file_data *fd)
403 {
404         struct obd_client_handle *och = &fd->fd_mds_och;
405
406         op_data->op_attr.ia_valid = ATTR_MODE      | ATTR_ATIME_SET |
407                                     ATTR_MTIME_SET | ATTR_CTIME_SET;
408
409         if (fd->fd_flags & FMODE_WRITE) {
410                 struct llu_sb_info *sbi = llu_i2sbi(inode);
411                 if (!(sbi->ll_lco.lco_flags & OBD_CONNECT_SOM) ||
412                     !S_ISREG(llu_i2stat(inode)->st_mode)) {
413                         op_data->op_attr.ia_valid |= ATTR_SIZE | ATTR_BLOCKS;
414                 } else {
415                         /* Inode cannot be dirty. Close the epoch. */
416                         op_data->op_flags |= MF_EPOCH_CLOSE;
417                         /* XXX: Send SOM attributes only if they are really
418                          * changed.  */
419                         llu_done_writing_attr(inode, op_data);
420                 }
421         }
422         llu_pack_inode2opdata(inode, op_data, &och->och_fh);
423         llu_prep_md_op_data(op_data, inode, NULL, NULL,
424                             0, 0, LUSTRE_OPC_ANY);
425 }
426
427 int llu_md_close(struct obd_export *md_exp, struct inode *inode)
428 {
429         struct llu_inode_info *lli = llu_i2info(inode);
430         struct ll_file_data *fd = lli->lli_file_data;
431         struct ptlrpc_request *req = NULL;
432         struct obd_client_handle *och = &fd->fd_mds_och;
433         struct intnl_stat *st = llu_i2stat(inode);
434         struct md_op_data op_data = { { 0 } };
435         int rc;
436         ENTRY;
437
438         /* clear group lock, if present */
439         if (fd->fd_flags & LL_FILE_GROUP_LOCKED)
440                 llu_put_grouplock(inode, fd->fd_grouplock.cg_gid);
441
442         llu_prepare_close(inode, &op_data, fd);
443         rc = md_close(md_exp, &op_data, och->och_mod, &req);
444         if (rc == -EAGAIN) {
445                 /* We are the last writer, so the MDS has instructed us to get
446                  * the file size and any write cookies, then close again. */
447                 LASSERT(lli->lli_open_flags & FMODE_WRITE);
448                 rc = llu_som_update(inode, &op_data);
449                 if (rc) {
450                         CERROR("inode %llu mdc Size-on-MDS update failed: "
451                                "rc = %d\n", (long long)st->st_ino, rc);
452                         rc = 0;
453                 }
454         } else if (rc) {
455                 CERROR("inode %llu close failed: rc %d\n",
456                        (long long)st->st_ino, rc);
457         } else {
458                 rc = llu_objects_destroy(req, inode);
459                 if (rc)
460                         CERROR("inode %llu ll_objects destroy: rc = %d\n",
461                                (long long)st->st_ino, rc);
462         }
463
464         md_clear_open_replay_data(md_exp, och);
465         ptlrpc_req_finished(req);
466         och->och_fh.cookie = DEAD_HANDLE_MAGIC;
467         lli->lli_file_data = NULL;
468         OBD_FREE(fd, sizeof(*fd));
469
470         RETURN(rc);
471 }
472
473 int llu_file_release(struct inode *inode)
474 {
475         struct ll_file_data *fd;
476         struct llu_sb_info *sbi = llu_i2sbi(inode);
477         struct llu_inode_info *lli = llu_i2info(inode);
478         int rc = 0, rc2;
479
480         ENTRY;
481         CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu\n",
482                (long long)llu_i2stat(inode)->st_ino, lli->lli_st_generation);
483
484         if (llu_is_root_inode(inode))
485                 RETURN(0);
486
487         /* still opened by others? */
488         if (--lli->lli_open_count)
489                 RETURN(0);
490
491         fd = lli->lli_file_data;
492         if (!fd) /* no process opened the file after an mcreate */
493                 RETURN(0);
494
495         rc2 = llu_md_close(sbi->ll_md_exp, inode);
496         if (rc2 && !rc)
497                 rc = rc2;
498
499         RETURN(rc);
500 }
501
502 /*
503  * libsysio require us return 0
504  */
505 int llu_iop_close(struct inode *inode)
506 {
507         int rc;
508
509         liblustre_wait_event(0);
510
511         rc = llu_file_release(inode);
512         if (rc) {
513                 CERROR("file close error %d\n", rc);
514         }
515         /* if open count == 0 && stale_flag is set, should we
516          * remove the inode immediately? */
517         liblustre_wait_idle();
518         return 0;
519 }
520
521 _SYSIO_OFF_T llu_iop_pos(struct inode *ino, _SYSIO_OFF_T off)
522 {
523         ENTRY;
524
525         liblustre_wait_event(0);
526
527         if (off < 0 || off > ll_file_maxbytes(ino))
528                 RETURN(-EINVAL);
529
530         RETURN(off);
531 }