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