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