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