1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/liblustre/file.c
38 * Lustre Light file operations
41 #define DEBUG_SUBSYSTEM S_LLITE
47 #include <sys/types.h>
49 #include <sys/queue.h>
52 #include "llite_lib.h"
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)
61 LASSERT(suppgids != NULL);
63 if (cfs_curproc_is_in_groups(i1->i_stbuf.st_gid))
64 suppgids[0] = i1->i_stbuf.st_gid;
69 if (cfs_curproc_is_in_groups(i2->i_stbuf.st_gid))
70 suppgids[1] = i2->i_stbuf.st_gid;
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,
82 LASSERT(i1 != NULL || i2 != NULL);
86 ll_i2gids(op_data->op_suppgids, i1, i2);
87 op_data->op_fid1 = *ll_inode2fid(i1);
89 ll_i2gids(op_data->op_suppgids, i2, i1);
90 op_data->op_fid1 = *ll_inode2fid(i2);
94 op_data->op_fid2 = *ll_inode2fid(i2);
96 fid_zero(&op_data->op_fid2);
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;
106 void obdo_refresh_inode(struct inode *dst,
110 struct intnl_stat *st = llu_i2stat(dst);
111 valid &= src->o_valid;
113 if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
114 CDEBUG(D_INODE,"valid "LPX64", cur time "CFS_TIME_T"/"CFS_TIME_T
116 src->o_valid, LTIME_S(st->st_mtime),
117 LTIME_S(st->st_ctime),
118 (long)src->o_mtime, (long)src->o_ctime);
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;
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.
141 void llu_ioepoch_open(struct llu_inode_info *lli, __u64 ioepoch)
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));
150 int llu_local_open(struct llu_inode_info *lli, struct lookup_intent *it)
152 struct ptlrpc_request *req = it->d.lustre.it_data;
153 struct ll_file_data *fd;
154 struct mdt_body *body;
157 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
158 LASSERT(body != NULL);
160 /* already opened? */
161 if (lli->lli_open_count++)
164 LASSERT(!lli->lli_file_data);
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. */
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);
182 int llu_iop_open(struct pnode *pnode, int flags, mode_t mode)
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 struct lov_stripe_md *lsm;
193 liblustre_wait_event(0);
195 /* don't do anything for '/' */
196 if (llu_is_root_inode(inode))
199 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu\n", (long long)st->st_ino);
200 LL_GET_INTENT(inode, it);
202 if (!it->d.lustre.it_disposition) {
206 rc = it_open_error(DISP_OPEN_OPEN, it);
208 GOTO(out_release, rc);
210 rc = llu_local_open(lli, it);
214 if (!S_ISREG(st->st_mode))
215 GOTO(out_release, rc = 0);
219 flags &= ~O_LOV_DELAY_CREATE;
220 /*XXX: open_flags are overwritten and the previous ones are lost */
221 lli->lli_open_flags = flags & ~(O_CREAT | O_EXCL | O_TRUNC);
224 request = it->d.lustre.it_data;
225 ptlrpc_req_finished(request);
227 it->it_op_release(it);
228 OBD_FREE(it, sizeof(*it));
230 /* libsysio hasn't done anything for O_TRUNC. here we
231 * simply simulate it as open(...); truncate(...); */
232 if (rc == 0 && (flags & O_TRUNC) && S_ISREG(st->st_mode)) {
235 memset(&attr, 0, sizeof(attr));
237 attr.ia_valid |= ATTR_SIZE | ATTR_RAW;
238 rc = llu_setattr_raw(inode, &attr);
240 CERROR("error %d truncate in open()\n", rc);
243 liblustre_wait_event(0);
247 int llu_objects_destroy(struct ptlrpc_request *req, struct inode *dir)
249 struct mdt_body *body;
250 struct lov_mds_md *eadata;
251 struct lov_stripe_md *lsm = NULL;
252 struct obd_trans_info oti = { 0 };
257 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
259 if (!(body->valid & OBD_MD_FLEASIZE))
262 if (body->eadatasize == 0) {
263 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
264 GOTO(out, rc = -EPROTO);
267 /* The MDS sent back the EA because we unlinked the last reference
268 * to this file. Use this EA to unlink the objects on the OST.
269 * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
270 * check it is complete and sensible. */
271 eadata = req_capsule_server_sized_get(&req->rq_pill, &RMF_MDT_MD,
274 LASSERT(eadata != NULL);
276 rc = obd_unpackmd(llu_i2obdexp(dir), &lsm, eadata,body->eadatasize);
278 CERROR("obd_unpackmd: %d\n", rc);
281 LASSERT(rc >= sizeof(*lsm));
285 GOTO(out_free_memmd, rc = -ENOMEM);
287 oa->o_id = lsm->lsm_object_id;
288 oa->o_seq = lsm->lsm_object_seq;
289 oa->o_mode = body->mode & S_IFMT;
290 oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
291 obdo_from_inode(oa, NULL, &llu_i2info(dir)->lli_fid, 0);
293 if (body->valid & OBD_MD_FLCOOKIE) {
294 oa->o_valid |= OBD_MD_FLCOOKIE;
296 req_capsule_server_sized_get(&req->rq_pill,
298 sizeof(struct llog_cookie) *
299 lsm->lsm_stripe_count);
300 if (oti.oti_logcookies == NULL) {
301 oa->o_valid &= ~OBD_MD_FLCOOKIE;
302 body->valid &= ~OBD_MD_FLCOOKIE;
306 rc = obd_destroy(llu_i2obdexp(dir), oa, lsm, &oti, NULL, NULL);
309 CERROR("obd destroy objid 0x"LPX64" error %d\n",
310 lsm->lsm_object_id, rc);
312 obd_free_memmd(llu_i2obdexp(dir), &lsm);
317 /** Cliens updates SOM attributes on MDS: obd_getattr and md_setattr. */
318 int llu_som_update(struct inode *inode, struct md_op_data *op_data)
320 struct llu_inode_info *lli = llu_i2info(inode);
321 struct llu_sb_info *sbi = llu_i2sbi(inode);
322 struct obdo oa = { 0 };
327 LASSERT(!(lli->lli_flags & LLIF_MDS_SIZE_LOCK));
328 LASSERT(sbi->ll_lco.lco_flags & OBD_CONNECT_SOM);
330 old_flags = op_data->op_flags;
331 op_data->op_flags = MF_SOM_CHANGE;
333 /* If inode is already in another epoch, skip getattr from OSTs. */
334 if (lli->lli_ioepoch == op_data->op_ioepoch) {
335 rc = llu_inode_getattr(inode, &oa, op_data->op_ioepoch,
336 old_flags & MF_GETATTR_LOCK);
340 CDEBUG(D_INODE, "objid "LPX64" is destroyed\n",
341 lli->lli_smd->lsm_object_id);
343 CERROR("inode_getattr failed (%d): unable to "
344 "send a Size-on-MDS attribute update "
345 "for inode %llu/%lu\n", rc,
346 (long long)llu_i2stat(inode)->st_ino,
347 lli->lli_st_generation);
349 CDEBUG(D_INODE, "Size-on-MDS update on "DFID"\n",
350 PFID(&lli->lli_fid));
353 /* Install attributes into op_data. */
354 md_from_obdo(op_data, &oa, oa.o_valid);
357 rc = llu_md_setattr(inode, op_data, NULL);
361 void llu_pack_inode2opdata(struct inode *inode, struct md_op_data *op_data,
362 struct lustre_handle *fh)
364 struct llu_inode_info *lli = llu_i2info(inode);
365 struct intnl_stat *st = llu_i2stat(inode);
368 op_data->op_fid1 = lli->lli_fid;
369 op_data->op_attr.ia_atime = st->st_atime;
370 op_data->op_attr.ia_mtime = st->st_mtime;
371 op_data->op_attr.ia_ctime = st->st_ctime;
372 op_data->op_attr.ia_size = st->st_size;
373 op_data->op_attr_blocks = st->st_blocks;
374 op_data->op_attr.ia_attr_flags = lli->lli_st_flags;
375 op_data->op_ioepoch = lli->lli_ioepoch;
377 op_data->op_handle = *fh;
381 /** Pack SOM attributes info @opdata for CLOSE, DONE_WRITING rpc. */
382 void llu_done_writing_attr(struct inode *inode, struct md_op_data *op_data)
384 struct llu_inode_info *lli = llu_i2info(inode);
387 op_data->op_flags |= MF_SOM_CHANGE;
389 /* Pack Size-on-MDS attributes if we are in IO
390 * epoch and attributes are valid. */
391 LASSERT(!(lli->lli_flags & LLIF_MDS_SIZE_LOCK));
392 if (!cl_local_size(inode))
393 op_data->op_attr.ia_valid |= ATTR_MTIME_SET | ATTR_CTIME_SET |
394 ATTR_ATIME_SET | ATTR_SIZE | ATTR_BLOCKS;
399 static void llu_prepare_close(struct inode *inode, struct md_op_data *op_data,
400 struct ll_file_data *fd)
402 struct obd_client_handle *och = &fd->fd_mds_och;
404 op_data->op_attr.ia_valid = ATTR_MODE | ATTR_ATIME_SET |
405 ATTR_MTIME_SET | ATTR_CTIME_SET;
407 if (fd->fd_flags & FMODE_WRITE) {
408 struct llu_sb_info *sbi = llu_i2sbi(inode);
409 if (!(sbi->ll_lco.lco_flags & OBD_CONNECT_SOM) ||
410 !S_ISREG(llu_i2stat(inode)->st_mode)) {
411 op_data->op_attr.ia_valid |= ATTR_SIZE | ATTR_BLOCKS;
413 /* Inode cannot be dirty. Close the epoch. */
414 op_data->op_flags |= MF_EPOCH_CLOSE;
415 /* XXX: Send SOM attributes only if they are really
417 llu_done_writing_attr(inode, op_data);
420 llu_pack_inode2opdata(inode, op_data, &och->och_fh);
421 llu_prep_md_op_data(op_data, inode, NULL, NULL,
422 0, 0, LUSTRE_OPC_ANY);
425 int llu_md_close(struct obd_export *md_exp, struct inode *inode)
427 struct llu_inode_info *lli = llu_i2info(inode);
428 struct ll_file_data *fd = lli->lli_file_data;
429 struct ptlrpc_request *req = NULL;
430 struct obd_client_handle *och = &fd->fd_mds_och;
431 struct intnl_stat *st = llu_i2stat(inode);
432 struct md_op_data op_data = { { 0 } };
436 /* clear group lock, if present */
437 if (fd->fd_flags & LL_FILE_GROUP_LOCKED)
438 llu_put_grouplock(inode, fd->fd_grouplock.cg_gid);
440 llu_prepare_close(inode, &op_data, fd);
441 rc = md_close(md_exp, &op_data, och->och_mod, &req);
443 /* We are the last writer, so the MDS has instructed us to get
444 * the file size and any write cookies, then close again. */
445 LASSERT(lli->lli_open_flags & FMODE_WRITE);
446 rc = llu_som_update(inode, &op_data);
448 CERROR("inode %llu mdc Size-on-MDS update failed: "
449 "rc = %d\n", (long long)st->st_ino, rc);
453 CERROR("inode %llu close failed: rc %d\n",
454 (long long)st->st_ino, rc);
456 rc = llu_objects_destroy(req, inode);
458 CERROR("inode %llu ll_objects destroy: rc = %d\n",
459 (long long)st->st_ino, rc);
462 md_clear_open_replay_data(md_exp, och);
463 ptlrpc_req_finished(req);
464 och->och_fh.cookie = DEAD_HANDLE_MAGIC;
465 lli->lli_file_data = NULL;
466 OBD_FREE(fd, sizeof(*fd));
471 int llu_file_release(struct inode *inode)
473 struct ll_file_data *fd;
474 struct llu_sb_info *sbi = llu_i2sbi(inode);
475 struct llu_inode_info *lli = llu_i2info(inode);
479 CDEBUG(D_VFSTRACE, "VFS Op:inode=%llu/%lu\n",
480 (long long)llu_i2stat(inode)->st_ino, lli->lli_st_generation);
482 if (llu_is_root_inode(inode))
485 /* still opened by others? */
486 if (--lli->lli_open_count)
489 fd = lli->lli_file_data;
490 if (!fd) /* no process opened the file after an mcreate */
493 rc2 = llu_md_close(sbi->ll_md_exp, inode);
501 * libsysio require us return 0
503 int llu_iop_close(struct inode *inode)
507 liblustre_wait_event(0);
509 rc = llu_file_release(inode);
511 CERROR("file close error %d\n", rc);
513 /* if open count == 0 && stale_flag is set, should we
514 * remove the inode immediately? */
515 liblustre_wait_idle();
519 _SYSIO_OFF_T llu_iop_pos(struct inode *ino, _SYSIO_OFF_T off)
523 liblustre_wait_event(0);
525 if (off < 0 || off > ll_file_maxbytes(ino))