Whamcloud - gitweb
Backout "liblustre now builds" commit. It breaks building in utils:
[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  * Lustre Light file operations
5  *
6  *  Copyright (c) 2002-2004 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LLITE
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <time.h>
30 #include <sys/types.h>
31 #include <sys/queue.h>
32 #include <fcntl.h>
33
34 #ifdef HAVE_XTIO_H
35 #include <xtio.h>
36 #endif
37 #include <sysio.h>
38 #include <fs.h>
39 #include <mount.h>
40 #include <inode.h>
41 #ifdef HAVE_FILE_H
42 #include <file.h>
43 #endif
44
45 #undef LIST_HEAD
46
47 #include "llite_lib.h"
48
49 void llu_prepare_mdc_data(struct mdc_op_data *data, struct inode *i1,
50                           struct inode *i2, const char *name,
51                           int namelen, int mode)
52 {
53         LASSERT(i1);
54         
55         ll_inode2id(&data->id1, i1);
56         if (i2)
57                 ll_inode2id(&data->id2, i2);
58
59         data->valid = 0;
60         data->name = name;
61         data->namelen = namelen;
62         data->create_mode = mode;
63         data->mod_time = CURRENT_TIME;
64 }
65
66 void obdo_refresh_inode(struct inode *dst,
67                         struct obdo *src,
68                         obd_valid valid)
69 {
70         struct llu_inode_info *lli = llu_i2info(dst);
71         valid &= src->o_valid;
72
73         if (valid & (OBD_MD_FLCTIME | OBD_MD_FLMTIME))
74                 CDEBUG(D_INODE, "valid "LPX64", cur time %lu/%lu, new %lu/%lu\n",
75                        src->o_valid, LTIME_S(lli->lli_st_mtime), 
76                        LTIME_S(lli->lli_st_ctime),
77                        (long)src->o_mtime, (long)src->o_ctime);
78
79         if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(lli->lli_st_atime))
80                 LTIME_S(lli->lli_st_atime) = src->o_atime;
81         if (valid & OBD_MD_FLMTIME && src->o_mtime > LTIME_S(lli->lli_st_mtime))
82                 LTIME_S(lli->lli_st_mtime) = src->o_mtime;
83         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(lli->lli_st_ctime))
84                 LTIME_S(lli->lli_st_ctime) = src->o_ctime;
85         if (valid & OBD_MD_FLSIZE && src->o_size > lli->lli_st_size)
86                 lli->lli_st_size = src->o_size;
87         /* optimum IO size */
88         if (valid & OBD_MD_FLBLKSZ)
89                 lli->lli_st_blksize = src->o_blksize;
90         /* allocation of space */
91         if (valid & OBD_MD_FLBLOCKS && src->o_blocks > lli->lli_st_blocks)
92                 lli->lli_st_blocks = src->o_blocks;
93 }
94
95 static int llu_local_open(struct llu_inode_info *lli, struct lookup_intent *it)
96 {
97         struct ptlrpc_request *req = LUSTRE_IT(it)->it_data;
98         struct ll_file_data *fd;
99         struct mds_body *body;
100         ENTRY;
101
102         body = lustre_msg_buf (req->rq_repmsg, 1, sizeof (*body));
103         LASSERT (body != NULL);                 /* reply already checked out */
104         LASSERT_REPSWABBED (req, 1);            /* and swabbed down */
105
106         /* already opened? */
107         if (lli->lli_open_count++)
108                 RETURN(0);
109
110         LASSERT(!lli->lli_file_data);
111
112         OBD_ALLOC(fd, sizeof(*fd));
113         /* We can't handle this well without reorganizing ll_file_open and
114          * ll_md_close(), so don't even try right now. */
115         LASSERT(fd != NULL);
116
117         memcpy(&fd->fd_mds_och.och_fh, &body->handle, sizeof(body->handle));
118         fd->fd_mds_och.och_magic = OBD_CLIENT_HANDLE_MAGIC;
119         lli->lli_file_data = fd;
120
121         mdc_set_open_replay_data(NULL, &fd->fd_mds_och, LUSTRE_IT(it)->it_data);
122
123         RETURN(0);
124 }
125
126 int llu_iop_open(struct pnode *pnode, int flags, mode_t mode)
127 {
128         struct inode *inode = pnode->p_base->pb_ino;
129         struct llu_inode_info *lli = llu_i2info(inode);
130         struct ll_file_data *fd;
131         struct ptlrpc_request *request;
132         struct lookup_intent *it;
133         struct lov_stripe_md *lsm;
134         int rc = 0;
135         ENTRY;
136
137         liblustre_wait_event(0);
138
139         /* don't do anything for '/' */
140         if (llu_is_root_inode(inode))
141                 RETURN(0);
142
143         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu\n", lli->lli_st_ino);
144         LL_GET_INTENT(inode, it);
145
146         if (!LUSTRE_IT(it)->it_disposition)
147                 LBUG();
148
149         rc = it_open_error(DISP_OPEN_OPEN, it);
150         if (rc)
151                 GOTO(out_release, rc);
152
153         rc = llu_local_open(lli, it);
154         if (rc)
155                 LBUG();
156
157         if (!S_ISREG(lli->lli_st_mode))
158                 GOTO(out_release, rc = 0);
159                 
160         fd = lli->lli_file_data;
161
162         lsm = lli->lli_smd;
163         if (lsm == NULL) {
164                 if (fd->fd_flags & O_LOV_DELAY_CREATE) {
165                         CDEBUG(D_INODE, "object creation was delayed\n");
166                         GOTO(out_release, rc);
167                 }
168         }
169         fd->fd_flags &= ~O_LOV_DELAY_CREATE;
170
171         lli->lli_open_flags = flags & ~(O_CREAT | O_EXCL | O_TRUNC);
172
173  out_release:
174         request = LUSTRE_IT(it)->it_data;
175         ptlrpc_req_finished(request);
176
177         it->it_op_release(it);
178         OBD_FREE(it, sizeof(*it));
179
180         /* libsysio haven't doing anything for O_TRUNC. here we
181          * simply simulate it as open(...); truncate(...);
182          */
183         if (rc == 0 && (flags & O_TRUNC) &&
184             S_ISREG(lli->lli_st_mode)) {
185                 struct iattr attr;
186
187                 memset(&attr, 0, sizeof(attr));
188                 attr.ia_size = 0;
189                 attr.ia_valid |= ATTR_SIZE | ATTR_RAW;
190                 rc  = llu_setattr_raw(inode, &attr);
191                 if (rc) {
192                         CERROR("error %d truncate in open()\n", rc);
193                 }
194         }
195
196         RETURN(rc);
197 }
198
199 int llu_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
200 {
201         struct mds_body *body;
202         struct lov_mds_md *eadata;
203         struct lov_stripe_md *lsm = NULL;
204         struct obd_trans_info oti = { 0 };
205         struct obdo *oa;
206         int rc;
207         ENTRY;
208
209         /* req is swabbed so this is safe */
210         body = lustre_msg_buf(request->rq_repmsg, 0, sizeof(*body));
211
212         if (!(body->valid & OBD_MD_FLEASIZE))
213                 RETURN(0);
214
215         if (body->eadatasize == 0) {
216                 CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
217                 GOTO(out, rc = -EPROTO);
218         }
219
220         /* The MDS sent back the EA because we unlinked the last reference
221          * to this file. Use this EA to unlink the objects on the OST.
222          * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
223          * check it is complete and sensible. */
224         eadata = lustre_swab_repbuf(request, 1, body->eadatasize, NULL);
225         LASSERT(eadata != NULL);
226         if (eadata == NULL) {
227                 CERROR("Can't unpack MDS EA data\n");
228                 GOTO(out, rc = -EPROTO);
229         }
230
231         rc = obd_unpackmd(llu_i2dtexp(dir), &lsm, eadata, body->eadatasize);
232         if (rc < 0) {
233                 CERROR("obd_unpackmd: %d\n", rc);
234                 GOTO(out, rc);
235         }
236         LASSERT(rc >= sizeof(*lsm));
237
238         oa = obdo_alloc();
239         if (oa == NULL)
240                 GOTO(out_free_memmd, rc = -ENOMEM);
241
242         oa->o_id = lsm->lsm_object_id;
243         oa->o_mode = body->mode & S_IFMT;
244         oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE;
245
246         if (body->valid & OBD_MD_FLCOOKIE) {
247                 oa->o_valid |= OBD_MD_FLCOOKIE;
248                 oti.oti_logcookies =
249                         lustre_msg_buf(request->rq_repmsg, 2,
250                                        sizeof(struct llog_cookie) *
251                                        lsm->lsm_stripe_count);
252                 if (oti.oti_logcookies == NULL) {
253                         oa->o_valid &= ~OBD_MD_FLCOOKIE;
254                         body->valid &= ~OBD_MD_FLCOOKIE;
255                 }
256         }
257
258         rc = obd_destroy(llu_i2dtexp(dir), oa, lsm, &oti);
259         obdo_free(oa);
260         if (rc)
261                 CERROR("obd destroy objid 0x"LPX64" error %d\n",
262                        lsm->lsm_object_id, rc);
263  out_free_memmd:
264         obd_free_memmd(llu_i2dtexp(dir), &lsm);
265  out:
266         return rc;
267 }
268
269 int llu_mdc_close(struct obd_export *mdc_exp, struct inode *inode)
270 {
271         struct llu_inode_info *lli = llu_i2info(inode);
272         struct ll_file_data *fd = lli->lli_file_data;
273         struct ptlrpc_request *req = NULL;
274         struct obd_client_handle *och = &fd->fd_mds_och;
275         struct obdo obdo;
276         int rc, valid;
277         ENTRY;
278
279         /* clear group lock, if present */
280         if (fd->fd_flags & LL_FILE_GROUP_LOCKED) {
281                 struct lov_stripe_md *lsm = llu_i2info(inode)->lli_smd;
282                 fd->fd_flags &= ~(LL_FILE_GROUP_LOCKED|LL_FILE_IGNORE_LOCK);
283                 rc = llu_extent_unlock(fd, inode, lsm, LCK_GROUP,
284                                        &fd->fd_cwlockh);
285         }
286
287         obdo.o_id = lli->lli_st_ino;
288         obdo.o_valid = OBD_MD_FLID;
289         valid = OBD_MD_FLTYPE | OBD_MD_FLMODE | OBD_MD_FLSIZE |OBD_MD_FLBLOCKS |
290                 OBD_MD_FLATIME | OBD_MD_FLMTIME | OBD_MD_FLCTIME;
291         if (test_bit(LLI_F_HAVE_OST_SIZE_LOCK, &lli->lli_flags))
292                 valid |= OBD_MD_FLSIZE | OBD_MD_FLBLOCKS;
293
294         obdo_from_inode(&obdo, inode, valid);
295
296         if (0 /* ll_is_inode_dirty(inode) */) {
297                 obdo.o_flags = MDS_BFLAG_UNCOMMITTED_WRITES;
298                 obdo.o_valid |= OBD_MD_FLFLAGS;
299         }
300         rc = mdc_close(mdc_exp, &obdo, och, &req);
301         if (rc == EAGAIN) {
302                 /* We are the last writer, so the MDS has instructed us to get
303                  * the file size and any write cookies, then close again. */
304                 //ll_queue_done_writing(inode);
305                 rc = 0;
306         } else if (rc) {
307                 CERROR("inode %lu close failed: rc %d\n", lli->lli_st_ino, rc);
308         } else {
309                 rc = llu_objects_destroy(req, inode);
310                 if (rc)
311                         CERROR("inode %lu ll_objects destroy: rc = %d\n",
312                                 lli->lli_st_ino, rc);
313         }
314
315         mdc_clear_open_replay_data(NULL, och);
316         ptlrpc_req_finished(req);
317         och->och_fh.cookie = DEAD_HANDLE_MAGIC;
318         lli->lli_file_data = NULL;
319         OBD_FREE(fd, sizeof(*fd));
320
321         RETURN(rc);
322 }
323
324 int llu_file_release(struct inode *inode)
325 {
326         struct ll_file_data *fd;
327         struct llu_sb_info *sbi = llu_i2sbi(inode);
328         struct llu_inode_info *lli = llu_i2info(inode);
329         int rc = 0, rc2;
330
331         ENTRY;
332         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu\n", lli->lli_st_ino,
333                lli->lli_st_generation);
334
335         if (llu_is_root_inode(inode))
336                 RETURN(0);
337
338         /* still opened by others? */
339         if (--lli->lli_open_count)
340                 RETURN(0);
341
342         fd = lli->lli_file_data;
343         if (!fd) /* no process opened the file after an mcreate */
344                 RETURN(0);
345
346         rc2 = llu_mdc_close(sbi->ll_md_exp, inode);
347         if (rc2 && !rc)
348                 rc = rc2;
349
350         RETURN(rc);
351 }
352
353 /*
354  * libsysio require us return 0
355  */
356 int llu_iop_close(struct inode *inode)
357 {
358         int rc;
359
360         liblustre_wait_event(0);
361
362         rc = llu_file_release(inode);
363         if (rc) {
364                 CERROR("file close error %d\n", rc);
365         }
366         /* if open count == 0 && stale_flag is set, should we
367          * remove the inode immediately? */
368         return 0;
369 }
370
371 _SYSIO_OFF_T llu_iop_pos(struct inode *ino, _SYSIO_OFF_T off)
372 {
373         ENTRY;
374
375         liblustre_wait_event(0);
376
377         if (off < 0 || off > ll_file_maxbytes(ino))
378                 RETURN(-EINVAL);
379
380         RETURN(off);
381 }
382
383 /* this isn't where truncate starts.   roughly:
384  * sys_truncate->ll_setattr_raw->vmtruncate->ll_truncate
385  * we grab the lock back in setattr_raw to avoid races. */
386 static void llu_truncate(struct inode *inode)
387 {
388         struct llu_inode_info *lli = llu_i2info(inode);
389         struct lov_stripe_md *lsm = lli->lli_smd;
390         struct obdo oa = {0};
391         int err;
392         ENTRY;
393         CDEBUG(D_VFSTRACE, "VFS Op:inode=%lu/%lu\n", lli->lli_st_ino,
394                lli->lli_st_generation);
395
396         if (!lsm) {
397                 CERROR("truncate on inode %lu with no objects\n", lli->lli_st_ino);
398                 EXIT;
399                 return;
400         }
401
402         oa.o_id = lsm->lsm_object_id;
403         oa.o_valid = OBD_MD_FLID;
404         obdo_from_inode(&oa, inode, OBD_MD_FLTYPE|OBD_MD_FLMODE|OBD_MD_FLATIME|
405                                     OBD_MD_FLMTIME | OBD_MD_FLCTIME);
406
407         CDEBUG(D_INFO, "calling punch for "LPX64" (all bytes after %Lu)\n",
408                oa.o_id, lli->lli_st_size);
409
410         /* truncate == punch from new size to absolute end of file */
411         err = obd_punch(llu_i2dtexp(inode), &oa, lsm, lli->lli_st_size,
412                         OBD_OBJECT_EOF, NULL);
413         if (err)
414                 CERROR("obd_truncate fails (%d) ino %lu\n", err, lli->lli_st_ino);
415         else
416                 obdo_to_inode(inode, &oa, OBD_MD_FLSIZE | OBD_MD_FLBLOCKS |
417                                           OBD_MD_FLATIME | OBD_MD_FLMTIME |
418                                           OBD_MD_FLCTIME);
419
420         EXIT;
421         return;
422 }
423
424 int llu_vmtruncate(struct inode * inode, loff_t offset)
425 {
426         struct llu_inode_info *lli = llu_i2info(inode);
427
428         lli->lli_st_size = offset;
429
430         llu_truncate(inode);
431
432         return 0;
433 }