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