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