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