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