Whamcloud - gitweb
c9ff4283c66a96e794a818fb5d0c0a70eac14c31
[fs/lustre-release.git] / lustre / llite / llite_close.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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 <lustre_mdc.h>
46 #include <lustre_lite.h>
47 #include "llite_internal.h"
48
49 /** records that a write is in flight */
50 void vvp_write_pending(struct ccc_object *club, struct ccc_page *page)
51 {
52         struct ll_inode_info *lli = ll_i2info(club->cob_inode);
53
54         ENTRY;
55         spin_lock(&lli->lli_lock);
56         lli->lli_flags |= LLIF_SOM_DIRTY;
57         if (page != NULL && list_empty(&page->cpg_pending_linkage))
58                 list_add(&page->cpg_pending_linkage, &club->cob_pending_list);
59         spin_unlock(&lli->lli_lock);
60         EXIT;
61 }
62
63 /** records that a write has completed */
64 void vvp_write_complete(struct ccc_object *club, struct ccc_page *page)
65 {
66         struct ll_inode_info *lli = ll_i2info(club->cob_inode);
67         int rc = 0;
68
69         ENTRY;
70         spin_lock(&lli->lli_lock);
71         if (page != NULL && !list_empty(&page->cpg_pending_linkage)) {
72                 list_del_init(&page->cpg_pending_linkage);
73                 rc = 1;
74         }
75         spin_unlock(&lli->lli_lock);
76         if (rc)
77                 ll_queue_done_writing(club->cob_inode, 0);
78         EXIT;
79 }
80
81 /** Queues DONE_WRITING if
82  * - done writing is allowed;
83  * - inode has no no dirty pages; */
84 void ll_queue_done_writing(struct inode *inode, unsigned long flags)
85 {
86         struct ll_inode_info *lli = ll_i2info(inode);
87         struct ccc_object *club = cl2ccc(ll_i2info(inode)->lli_clob);
88         spin_lock(&lli->lli_lock);
89         lli->lli_flags |= flags;
90         ENTRY;
91
92         if ((lli->lli_flags & LLIF_DONE_WRITING) &&
93             list_empty(&club->cob_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("ino %lu/%u(flags %lu) som valid it just after "
98                                "recovery\n",
99                                inode->i_ino, inode->i_generation,
100                                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 %lu/%u to close list\n",
106                        inode->i_ino, inode->i_generation);
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 /** Closes epoch and sends Size-on-MDS attribute update if possible.  Call
125  * this under ll_inode_info::lli_lock spinlock. */
126 void ll_epoch_close(struct inode *inode, struct md_op_data *op_data,
127                     struct obd_client_handle **och, unsigned long flags)
128 {
129         struct ll_inode_info *lli = ll_i2info(inode);
130         struct ccc_object *club = cl2ccc(ll_i2info(inode)->lli_clob);
131         ENTRY;
132
133         spin_lock(&lli->lli_lock);
134         if (!(list_empty(&club->cob_pending_list))) {
135                 if (!(lli->lli_flags & LLIF_EPOCH_PENDING)) {
136                         LASSERT(*och != NULL);
137                         LASSERT(lli->lli_pending_och == NULL);
138                         /* Inode is dirty and there is no pending write done
139                          * request yet, DONE_WRITE is to be sent later. */
140                         lli->lli_flags |= LLIF_EPOCH_PENDING;
141                         lli->lli_pending_och = *och;
142                         spin_unlock(&lli->lli_lock);
143
144                         inode = igrab(inode);
145                         LASSERT(inode);
146                         GOTO(out, 0);
147                 }
148                 if (flags & LLIF_DONE_WRITING) {
149                         /* Some pages are still dirty, it is early to send
150                          * DONE_WRITE. Wait untill all pages will be flushed
151                          * and try DONE_WRITE again later. */
152                         LASSERT(!(lli->lli_flags & LLIF_DONE_WRITING));
153                         lli->lli_flags |= LLIF_DONE_WRITING;
154                         spin_unlock(&lli->lli_lock);
155
156                         inode = igrab(inode);
157                         LASSERT(inode);
158                         GOTO(out, 0);
159                 }
160         }
161         CDEBUG(D_INODE, "Epoch "LPU64" closed on "DFID"\n",
162                ll_i2info(inode)->lli_ioepoch, PFID(&lli->lli_fid));
163         op_data->op_flags |= MF_EPOCH_CLOSE;
164
165         if (flags & LLIF_DONE_WRITING) {
166                 LASSERT(lli->lli_flags & LLIF_SOM_DIRTY);
167                 LASSERT(!(lli->lli_flags & LLIF_DONE_WRITING));
168                 *och = lli->lli_pending_och;
169                 lli->lli_pending_och = NULL;
170                 lli->lli_flags &= ~LLIF_EPOCH_PENDING;
171         } else {
172                 /* Pack Size-on-MDS inode attributes only if they has changed */
173                 if (!(lli->lli_flags & LLIF_SOM_DIRTY)) {
174                         spin_unlock(&lli->lli_lock);
175                         GOTO(out, 0);
176                 }
177
178                 /* There is a pending DONE_WRITE -- close epoch with no
179                  * attribute change. */
180                 if (lli->lli_flags & LLIF_EPOCH_PENDING) {
181                         spin_unlock(&lli->lli_lock);
182                         GOTO(out, 0);
183                 }
184         }
185
186         LASSERT(list_empty(&club->cob_pending_list));
187         lli->lli_flags &= ~LLIF_SOM_DIRTY;
188         spin_unlock(&lli->lli_lock);
189         op_data->op_flags |= MF_SOM_CHANGE;
190
191         /* Check if Size-on-MDS attributes are valid. */
192         if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
193                 CWARN("ino %lu/%u(flags %lu) som valid it just after "
194                       "recovery\n",
195                       inode->i_ino, inode->i_generation, lli->lli_flags);
196
197         if (!cl_local_size(inode)) {
198                 /* Send Size-on-MDS Attributes if valid. Atime is sent along
199                  * with all the attributes. */
200                 op_data->op_attr.ia_valid |= ATTR_MTIME_SET | ATTR_CTIME_SET |
201                                 ATTR_ATIME_SET | ATTR_SIZE | ATTR_BLOCKS;
202         }
203         EXIT;
204 out:
205         return;
206 }
207
208 int ll_sizeonmds_update(struct inode *inode, struct lustre_handle *fh,
209                         __u64 ioepoch)
210 {
211         struct ll_inode_info *lli = ll_i2info(inode);
212         struct md_op_data *op_data;
213         struct obdo *oa;
214         int rc;
215         ENTRY;
216
217         /* LASSERT(!(lli->lli_flags & LLIF_MDS_SIZE_LOCK)); */
218         /* After recovery that can be valid. */
219         if (lli->lli_flags & LLIF_MDS_SIZE_LOCK)
220                 CWARN("ino %lu/%u(flags %lu) som valid it just after "
221                       "recovery\n", inode->i_ino, inode->i_generation,
222                       lli->lli_flags);
223
224         OBDO_ALLOC(oa);
225         OBD_ALLOC_PTR(op_data);
226         if (!oa || !op_data) {
227                 CERROR("can't allocate memory for Size-on-MDS update.\n");
228                 RETURN(-ENOMEM);
229         }
230         rc = ll_inode_getattr(inode, oa);
231         if (rc == -ENOENT) {
232                 oa->o_valid = 0;
233                 CDEBUG(D_INODE, "objid "LPX64" is already destroyed\n",
234                        lli->lli_smd->lsm_object_id);
235         } else if (rc) {
236                 CERROR("inode_getattr failed (%d): unable to send a "
237                        "Size-on-MDS attribute update for inode %lu/%u\n",
238                        rc, inode->i_ino, inode->i_generation);
239                 GOTO(out, rc);
240         }
241         CDEBUG(D_INODE, "Size-on-MDS update on "DFID"\n", PFID(&lli->lli_fid));
242
243         md_from_obdo(op_data, oa, oa->o_valid);
244         memcpy(&op_data->op_handle, fh, sizeof(*fh));
245
246         op_data->op_ioepoch = ioepoch;
247         op_data->op_flags |= MF_SOM_CHANGE;
248
249         rc = ll_md_setattr(inode, op_data, NULL);
250         EXIT;
251 out:
252         if (oa)
253                 OBDO_FREE(oa);
254         if (op_data)
255                 ll_finish_md_op_data(op_data);
256         return rc;
257 }
258
259 /** Sends a DONE_WRITING rpc, packs Size-on-MDS attributes into it, if
260  * possible */
261 static void ll_done_writing(struct inode *inode)
262 {
263         struct obd_client_handle *och = NULL;
264         struct md_op_data *op_data;
265         int rc;
266         ENTRY;
267
268         LASSERT(ll_i2mdexp(inode)->exp_connect_flags & OBD_CONNECT_SOM);
269
270         OBD_ALLOC_PTR(op_data);
271         if (op_data == NULL) {
272                 CERROR("can't allocate op_data\n");
273                 EXIT;
274                 return;
275         }
276
277         ll_epoch_close(inode, op_data, &och, LLIF_DONE_WRITING);
278         /* If there is no @och, we do not do D_W yet. */
279         if (och == NULL)
280                 GOTO(out, 0);
281
282         ll_pack_inode2opdata(inode, op_data, &och->och_fh);
283
284         rc = md_done_writing(ll_i2sbi(inode)->ll_md_exp, op_data, NULL);
285         if (rc == -EAGAIN) {
286                 /* MDS has instructed us to obtain Size-on-MDS attribute from
287                  * OSTs and send setattr to back to MDS. */
288                 rc = ll_sizeonmds_update(inode, &och->och_fh,
289                                          op_data->op_ioepoch);
290         } else if (rc) {
291                 CERROR("inode %lu mdc done_writing failed: rc = %d\n",
292                        inode->i_ino, rc);
293         }
294 out:
295         ll_finish_md_op_data(op_data);
296         if (och) {
297                 md_clear_open_replay_data(ll_i2sbi(inode)->ll_md_exp, och);
298                 OBD_FREE_PTR(och);
299         }
300         EXIT;
301 }
302
303 static struct ll_inode_info *ll_close_next_lli(struct ll_close_queue *lcq)
304 {
305         struct ll_inode_info *lli = NULL;
306
307         spin_lock(&lcq->lcq_lock);
308
309         if (!list_empty(&lcq->lcq_head)) {
310                 lli = list_entry(lcq->lcq_head.next, struct ll_inode_info,
311                                  lli_close_list);
312                 list_del_init(&lli->lli_close_list);
313         } else if (atomic_read(&lcq->lcq_stop))
314                 lli = ERR_PTR(-EALREADY);
315
316         spin_unlock(&lcq->lcq_lock);
317         return lli;
318 }
319
320 static int ll_close_thread(void *arg)
321 {
322         struct ll_close_queue *lcq = arg;
323         ENTRY;
324
325         {
326                 char name[CFS_CURPROC_COMM_MAX];
327                 snprintf(name, sizeof(name) - 1, "ll_close");
328                 cfs_daemonize(name);
329         }
330
331         complete(&lcq->lcq_comp);
332
333         while (1) {
334                 struct l_wait_info lwi = { 0 };
335                 struct ll_inode_info *lli;
336                 struct inode *inode;
337
338                 l_wait_event_exclusive(lcq->lcq_waitq,
339                                        (lli = ll_close_next_lli(lcq)) != NULL,
340                                        &lwi);
341                 if (IS_ERR(lli))
342                         break;
343
344                 inode = ll_info2i(lli);
345                 CDEBUG(D_INFO, "done_writting for inode %lu/%u\n",
346                        inode->i_ino, inode->i_generation);
347                 ll_done_writing(inode);
348                 iput(inode);
349         }
350
351         CDEBUG(D_INFO, "ll_close exiting\n");
352         complete(&lcq->lcq_comp);
353         RETURN(0);
354 }
355
356 int ll_close_thread_start(struct ll_close_queue **lcq_ret)
357 {
358         struct ll_close_queue *lcq;
359         pid_t pid;
360
361         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CLOSE_THREAD))
362                 return -EINTR;
363
364         OBD_ALLOC(lcq, sizeof(*lcq));
365         if (lcq == NULL)
366                 return -ENOMEM;
367
368         spin_lock_init(&lcq->lcq_lock);
369         INIT_LIST_HEAD(&lcq->lcq_head);
370         init_waitqueue_head(&lcq->lcq_waitq);
371         init_completion(&lcq->lcq_comp);
372
373         pid = kernel_thread(ll_close_thread, lcq, 0);
374         if (pid < 0) {
375                 OBD_FREE(lcq, sizeof(*lcq));
376                 return pid;
377         }
378
379         wait_for_completion(&lcq->lcq_comp);
380         *lcq_ret = lcq;
381         return 0;
382 }
383
384 void ll_close_thread_shutdown(struct ll_close_queue *lcq)
385 {
386         init_completion(&lcq->lcq_comp);
387         atomic_inc(&lcq->lcq_stop);
388         wake_up(&lcq->lcq_waitq);
389         wait_for_completion(&lcq->lcq_comp);
390         OBD_FREE(lcq, sizeof(*lcq));
391 }