Whamcloud - gitweb
LU-5971 llite: rename ccc_object to vvp_object
[fs/lustre-release.git] / lustre / llite / llite_close.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, 2012, 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/llite/llite_close.c
37  *
38  * Lustre Lite routines to issue a secondary close after writeback
39  */
40
41 #include <linux/module.h>
42
43 #define DEBUG_SUBSYSTEM S_LLITE
44
45 #include "llite_internal.h"
46
47 /** records that a write is in flight */
48 void vvp_write_pending(struct vvp_object *club, struct ccc_page *page)
49 {
50         struct ll_inode_info *lli = ll_i2info(club->vob_inode);
51
52         ENTRY;
53         spin_lock(&lli->lli_lock);
54         lli->lli_flags |= LLIF_SOM_DIRTY;
55         if (page != NULL && list_empty(&page->cpg_pending_linkage))
56                 list_add(&page->cpg_pending_linkage,
57                              &club->vob_pending_list);
58         spin_unlock(&lli->lli_lock);
59         EXIT;
60 }
61
62 /** records that a write has completed */
63 void vvp_write_complete(struct vvp_object *club, struct ccc_page *page)
64 {
65         struct ll_inode_info *lli = ll_i2info(club->vob_inode);
66         int rc = 0;
67
68         ENTRY;
69         spin_lock(&lli->lli_lock);
70         if (page != NULL && !list_empty(&page->cpg_pending_linkage)) {
71                 list_del_init(&page->cpg_pending_linkage);
72                 rc = 1;
73         }
74         spin_unlock(&lli->lli_lock);
75         if (rc)
76                 ll_queue_done_writing(club->vob_inode, 0);
77         EXIT;
78 }
79
80 /** Queues DONE_WRITING if
81  * - done writing is allowed;
82  * - inode has no no dirty pages; */
83 void ll_queue_done_writing(struct inode *inode, unsigned long flags)
84 {
85         struct ll_inode_info *lli = ll_i2info(inode);
86         struct vvp_object *club = cl2vvp(ll_i2info(inode)->lli_clob);
87         ENTRY;
88
89         spin_lock(&lli->lli_lock);
90         lli->lli_flags |= flags;
91
92         if ((lli->lli_flags & LLIF_DONE_WRITING) &&
93             list_empty(&club->vob_pending_list)) {
94                 struct ll_close_queue *lcq = ll_i2sbi(inode)->ll_lcq;
95
96                 if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
97                         CWARN("%s: file "DFID"(flags %u) Size-on-MDS valid, "
98                               "done writing allowed and no diry pages\n",
99                               ll_get_fsname(inode->i_sb, NULL, 0),
100                               PFID(ll_inode2fid(inode)), lli->lli_flags);
101                 /* DONE_WRITING is allowed and inode has no dirty page. */
102                 spin_lock(&lcq->lcq_lock);
103
104                 LASSERT(list_empty(&lli->lli_close_list));
105                 CDEBUG(D_INODE, "adding inode "DFID" to close list\n",
106                        PFID(ll_inode2fid(inode)));
107                 list_add_tail(&lli->lli_close_list, &lcq->lcq_head);
108
109                 /* Avoid a concurrent insertion into the close thread queue:
110                  * an inode is already in the close thread, open(), write(),
111                  * close() happen, epoch is closed as the inode is marked as
112                  * LLIF_EPOCH_PENDING. When pages are written inode should not
113                  * be inserted into the queue again, clear this flag to avoid
114                  * it. */
115                 lli->lli_flags &= ~LLIF_DONE_WRITING;
116
117                 wake_up(&lcq->lcq_waitq);
118                 spin_unlock(&lcq->lcq_lock);
119         }
120         spin_unlock(&lli->lli_lock);
121         EXIT;
122 }
123
124 /** Pack SOM attributes info @opdata for CLOSE, DONE_WRITING rpc. */
125 void ll_done_writing_attr(struct inode *inode, struct md_op_data *op_data)
126 {
127         struct ll_inode_info *lli = ll_i2info(inode);
128         ENTRY;
129
130         op_data->op_flags |= MF_SOM_CHANGE;
131         /* Check if Size-on-MDS attributes are valid. */
132         if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
133                 CERROR("%s: inode "DFID"(flags %u) MDS holds lock on "
134                        "Size-on-MDS attributes\n",
135                        ll_get_fsname(inode->i_sb, NULL, 0),
136                        PFID(ll_inode2fid(inode)), lli->lli_flags);
137
138         if (!cl_local_size(inode)) {
139                 /* Send Size-on-MDS Attributes if valid. */
140                 op_data->op_attr.ia_valid |= ATTR_MTIME_SET | ATTR_CTIME_SET |
141                                 ATTR_ATIME_SET | ATTR_SIZE | ATTR_BLOCKS;
142         }
143         EXIT;
144 }
145
146 /** Closes ioepoch and packs Size-on-MDS attribute if needed into @op_data. */
147 void ll_ioepoch_close(struct inode *inode, struct md_op_data *op_data,
148                       struct obd_client_handle **och, unsigned long flags)
149 {
150         struct ll_inode_info *lli = ll_i2info(inode);
151         struct vvp_object *club = cl2vvp(ll_i2info(inode)->lli_clob);
152         ENTRY;
153
154         spin_lock(&lli->lli_lock);
155         if (!(list_empty(&club->vob_pending_list))) {
156                 if (!(lli->lli_flags & LLIF_EPOCH_PENDING)) {
157                         LASSERT(*och != NULL);
158                         LASSERT(lli->lli_pending_och == NULL);
159                         /* Inode is dirty and there is no pending write done
160                          * request yet, DONE_WRITE is to be sent later. */
161                         lli->lli_flags |= LLIF_EPOCH_PENDING;
162                         lli->lli_pending_och = *och;
163                         spin_unlock(&lli->lli_lock);
164
165                         inode = igrab(inode);
166                         LASSERT(inode);
167                         GOTO(out, 0);
168                 }
169                 if (flags & LLIF_DONE_WRITING) {
170                         /* Some pages are still dirty, it is early to send
171                          * DONE_WRITE. Wait untill all pages will be flushed
172                          * and try DONE_WRITE again later. */
173                         LASSERT(!(lli->lli_flags & LLIF_DONE_WRITING));
174                         lli->lli_flags |= LLIF_DONE_WRITING;
175                         spin_unlock(&lli->lli_lock);
176
177                         inode = igrab(inode);
178                         LASSERT(inode);
179                         GOTO(out, 0);
180                 }
181         }
182         CDEBUG(D_INODE, "Epoch "LPU64" closed on "DFID"\n",
183                ll_i2info(inode)->lli_ioepoch, PFID(&lli->lli_fid));
184         op_data->op_flags |= MF_EPOCH_CLOSE;
185
186         if (flags & LLIF_DONE_WRITING) {
187                 LASSERT(lli->lli_flags & LLIF_SOM_DIRTY);
188                 LASSERT(!(lli->lli_flags & LLIF_DONE_WRITING));
189                 *och = lli->lli_pending_och;
190                 lli->lli_pending_och = NULL;
191                 lli->lli_flags &= ~LLIF_EPOCH_PENDING;
192         } else {
193                 /* Pack Size-on-MDS inode attributes only if they has changed */
194                 if (!(lli->lli_flags & LLIF_SOM_DIRTY)) {
195                         spin_unlock(&lli->lli_lock);
196                         GOTO(out, 0);
197                 }
198
199                 /* There is a pending DONE_WRITE -- close epoch with no
200                  * attribute change. */
201                 if (lli->lli_flags & LLIF_EPOCH_PENDING) {
202                         spin_unlock(&lli->lli_lock);
203                         GOTO(out, 0);
204                 }
205         }
206
207         LASSERT(list_empty(&club->vob_pending_list));
208         lli->lli_flags &= ~LLIF_SOM_DIRTY;
209         spin_unlock(&lli->lli_lock);
210         ll_done_writing_attr(inode, op_data);
211
212         EXIT;
213 out:
214         return;
215 }
216
217 /**
218  * Cliens updates SOM attributes on MDS (including llog cookies):
219  * obd_getattr with no lock and md_setattr.
220  */
221 int ll_som_update(struct inode *inode, struct md_op_data *op_data)
222 {
223         struct ll_inode_info *lli = ll_i2info(inode);
224         struct ptlrpc_request *request = NULL;
225         __u32 old_flags;
226         struct obdo *oa;
227         int rc;
228         ENTRY;
229
230         LASSERT(op_data != NULL);
231         if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
232                 CERROR("%s: inode "DFID"(flags %u) MDS holds lock on "
233                        "Size-on-MDS attributes\n",
234                        ll_get_fsname(inode->i_sb, NULL, 0),
235                        PFID(ll_inode2fid(inode)), lli->lli_flags);
236
237         OBDO_ALLOC(oa);
238         if (!oa) {
239                 CERROR("can't allocate memory for Size-on-MDS update.\n");
240                 RETURN(-ENOMEM);
241         }
242
243         old_flags = op_data->op_flags;
244         op_data->op_flags = MF_SOM_CHANGE;
245
246         /* If inode is already in another epoch, skip getattr from OSTs. */
247         if (lli->lli_ioepoch == op_data->op_ioepoch) {
248                 rc = ll_inode_getattr(inode, oa, op_data->op_ioepoch,
249                                       old_flags & MF_GETATTR_LOCK);
250                 if (rc) {
251                         oa->o_valid = 0;
252                         if (rc != -ENOENT)
253                                 CERROR("%s: inode_getattr failed - unable to "
254                                        "send a Size-on-MDS attribute update "
255                                        "for inode "DFID": rc = %d\n",
256                                        ll_get_fsname(inode->i_sb, NULL, 0),
257                                        PFID(ll_inode2fid(inode)), rc);
258                 } else {
259                         CDEBUG(D_INODE, "Size-on-MDS update on "DFID"\n",
260                                PFID(&lli->lli_fid));
261                 }
262                 /* Install attributes into op_data. */
263                 md_from_obdo(op_data, oa, oa->o_valid);
264         }
265
266         rc = md_setattr(ll_i2sbi(inode)->ll_md_exp, op_data,
267                         NULL, 0, NULL, 0, &request, NULL);
268         ptlrpc_req_finished(request);
269
270         OBDO_FREE(oa);
271         RETURN(rc);
272 }
273
274 /**
275  * Closes the ioepoch and packs all the attributes into @op_data for
276  * DONE_WRITING rpc.
277  */
278 static void ll_prepare_done_writing(struct inode *inode,
279                                     struct md_op_data *op_data,
280                                     struct obd_client_handle **och)
281 {
282         ll_ioepoch_close(inode, op_data, och, LLIF_DONE_WRITING);
283         /* If there is no @och, we do not do D_W yet. */
284         if (*och == NULL)
285                 return;
286
287         ll_pack_inode2opdata(inode, op_data, &(*och)->och_fh);
288         ll_prep_md_op_data(op_data, inode, NULL, NULL,
289                            0, 0, LUSTRE_OPC_ANY, NULL);
290 }
291
292 /** Send a DONE_WRITING rpc. */
293 static void ll_done_writing(struct inode *inode)
294 {
295         struct obd_client_handle *och = NULL;
296         struct md_op_data *op_data;
297         int rc;
298         ENTRY;
299
300         LASSERT(exp_connect_som(ll_i2mdexp(inode)));
301
302         OBD_ALLOC_PTR(op_data);
303         if (op_data == NULL) {
304                 CERROR("can't allocate op_data\n");
305                 EXIT;
306                 return;
307         }
308
309         ll_prepare_done_writing(inode, op_data, &och);
310         /* If there is no @och, we do not do D_W yet. */
311         if (och == NULL)
312                 GOTO(out, 0);
313
314         rc = md_done_writing(ll_i2sbi(inode)->ll_md_exp, op_data, NULL);
315         if (rc == -EAGAIN) {
316                 /* MDS has instructed us to obtain Size-on-MDS attribute from
317                  * OSTs and send setattr to back to MDS. */
318                 rc = ll_som_update(inode, op_data);
319         } else if (rc) {
320                 CERROR("%s: inode "DFID" mdc done_writing failed: rc = %d\n",
321                        ll_get_fsname(inode->i_sb, NULL, 0),
322                        PFID(ll_inode2fid(inode)), rc);
323         }
324 out:
325         ll_finish_md_op_data(op_data);
326         if (och) {
327                 md_clear_open_replay_data(ll_i2sbi(inode)->ll_md_exp, och);
328                 OBD_FREE_PTR(och);
329         }
330         EXIT;
331 }
332
333 static struct ll_inode_info *ll_close_next_lli(struct ll_close_queue *lcq)
334 {
335         struct ll_inode_info *lli = NULL;
336
337         spin_lock(&lcq->lcq_lock);
338
339         if (!list_empty(&lcq->lcq_head)) {
340                 lli = list_entry(lcq->lcq_head.next, struct ll_inode_info,
341                                  lli_close_list);
342                 list_del_init(&lli->lli_close_list);
343         } else if (atomic_read(&lcq->lcq_stop))
344                 lli = ERR_PTR(-EALREADY);
345
346         spin_unlock(&lcq->lcq_lock);
347         return lli;
348 }
349
350 static int ll_close_thread(void *arg)
351 {
352         struct ll_close_queue *lcq = arg;
353         ENTRY;
354
355         complete(&lcq->lcq_comp);
356
357         while (1) {
358                 struct l_wait_info lwi = { 0 };
359                 struct ll_inode_info *lli;
360                 struct inode *inode;
361
362                 l_wait_event_exclusive(lcq->lcq_waitq,
363                                        (lli = ll_close_next_lli(lcq)) != NULL,
364                                        &lwi);
365                 if (IS_ERR(lli))
366                         break;
367
368                 inode = ll_info2i(lli);
369                 CDEBUG(D_INFO, "done_writting for inode "DFID"\n",
370                        PFID(ll_inode2fid(inode)));
371                 ll_done_writing(inode);
372                 iput(inode);
373         }
374
375         CDEBUG(D_INFO, "ll_close exiting\n");
376         complete(&lcq->lcq_comp);
377         RETURN(0);
378 }
379
380 int ll_close_thread_start(struct ll_close_queue **lcq_ret)
381 {
382         struct ll_close_queue *lcq;
383         struct task_struct *task;
384
385         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CLOSE_THREAD))
386                 return -EINTR;
387
388         OBD_ALLOC(lcq, sizeof(*lcq));
389         if (lcq == NULL)
390                 return -ENOMEM;
391
392         spin_lock_init(&lcq->lcq_lock);
393         INIT_LIST_HEAD(&lcq->lcq_head);
394         init_waitqueue_head(&lcq->lcq_waitq);
395         init_completion(&lcq->lcq_comp);
396
397         task = kthread_run(ll_close_thread, lcq, "ll_close");
398         if (IS_ERR(task)) {
399                 OBD_FREE(lcq, sizeof(*lcq));
400                 return PTR_ERR(task);
401         }
402
403         wait_for_completion(&lcq->lcq_comp);
404         *lcq_ret = lcq;
405         return 0;
406 }
407
408 void ll_close_thread_shutdown(struct ll_close_queue *lcq)
409 {
410         init_completion(&lcq->lcq_comp);
411         atomic_inc(&lcq->lcq_stop);
412         wake_up(&lcq->lcq_waitq);
413         wait_for_completion(&lcq->lcq_comp);
414         OBD_FREE(lcq, sizeof(*lcq));
415 }