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