Linux-libre 4.14.68-gnu
[librecmc/linux-libre.git] / drivers / staging / lustre / lustre / llite / glimpse.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) 2008, 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  * glimpse code shared between vvp and liblustre (and other Lustre clients in
33  * the future).
34  *
35  *   Author: Nikita Danilov <nikita.danilov@sun.com>
36  *   Author: Oleg Drokin <oleg.drokin@sun.com>
37  */
38
39 #include <linux/libcfs/libcfs.h>
40 #include <obd_class.h>
41 #include <obd_support.h>
42 #include <obd.h>
43
44 #include <lustre_dlm.h>
45 #include <lustre_mdc.h>
46 #include <linux/pagemap.h>
47 #include <linux/file.h>
48
49 #include <cl_object.h>
50 #include "llite_internal.h"
51
52 static const struct cl_lock_descr whole_file = {
53         .cld_start = 0,
54         .cld_end   = CL_PAGE_EOF,
55         .cld_mode  = CLM_READ
56 };
57
58 /*
59  * Check whether file has possible unwriten pages.
60  *
61  * \retval 1    file is mmap-ed or has dirty pages
62  *       0    otherwise
63  */
64 blkcnt_t dirty_cnt(struct inode *inode)
65 {
66         blkcnt_t cnt = 0;
67         struct vvp_object *vob = cl_inode2vvp(inode);
68         void          *results[1];
69
70         if (inode->i_mapping)
71                 cnt += radix_tree_gang_lookup_tag(&inode->i_mapping->page_tree,
72                                                   results, 0, 1,
73                                                   PAGECACHE_TAG_DIRTY);
74         if (cnt == 0 && atomic_read(&vob->vob_mmap_cnt) > 0)
75                 cnt = 1;
76
77         return (cnt > 0) ? 1 : 0;
78 }
79
80 int cl_glimpse_lock(const struct lu_env *env, struct cl_io *io,
81                     struct inode *inode, struct cl_object *clob, int agl)
82 {
83         const struct lu_fid  *fid   = lu_object_fid(&clob->co_lu);
84         struct cl_lock *lock = vvp_env_lock(env);
85         struct cl_lock_descr *descr = &lock->cll_descr;
86         int result = 0;
87
88         CDEBUG(D_DLMTRACE, "Glimpsing inode " DFID "\n", PFID(fid));
89
90         /* NOTE: this looks like DLM lock request, but it may
91          *       not be one. Due to CEF_ASYNC flag (translated
92          *       to LDLM_FL_HAS_INTENT by osc), this is
93          *       glimpse request, that won't revoke any
94          *       conflicting DLM locks held. Instead,
95          *       ll_glimpse_callback() will be called on each
96          *       client holding a DLM lock against this file,
97          *       and resulting size will be returned for each
98          *       stripe. DLM lock on [0, EOF] is acquired only
99          *       if there were no conflicting locks. If there
100          *       were conflicting locks, enqueuing or waiting
101          *       fails with -ENAVAIL, but valid inode
102          *       attributes are returned anyway.
103          */
104         *descr = whole_file;
105         descr->cld_obj = clob;
106         descr->cld_mode = CLM_READ;
107         descr->cld_enq_flags = CEF_ASYNC | CEF_MUST;
108         if (agl)
109                 descr->cld_enq_flags |= CEF_AGL;
110         /*
111          * CEF_ASYNC is used because glimpse sub-locks cannot
112          * deadlock (because they never conflict with other
113          * locks) and, hence, can be enqueued out-of-order.
114          *
115          * CEF_MUST protects glimpse lock from conversion into
116          * a lockless mode.
117          */
118         result = cl_lock_request(env, io, lock);
119         if (result < 0)
120                 return result;
121
122         if (!agl) {
123                 ll_merge_attr(env, inode);
124                 if (i_size_read(inode) > 0 && !inode->i_blocks) {
125                         /*
126                          * LU-417: Add dirty pages block count
127                          * lest i_blocks reports 0, some "cp" or
128                          * "tar" may think it's a completely
129                          * sparse file and skip it.
130                          */
131                         inode->i_blocks = dirty_cnt(inode);
132                 }
133         }
134
135         cl_lock_release(env, lock);
136
137         return result;
138 }
139
140 static int cl_io_get(struct inode *inode, struct lu_env **envout,
141                      struct cl_io **ioout, u16 *refcheck)
142 {
143         struct lu_env     *env;
144         struct cl_io       *io;
145         struct ll_inode_info    *lli = ll_i2info(inode);
146         struct cl_object       *clob = lli->lli_clob;
147         int result;
148
149         if (S_ISREG(inode->i_mode)) {
150                 env = cl_env_get(refcheck);
151                 if (!IS_ERR(env)) {
152                         io = vvp_env_thread_io(env);
153                         io->ci_obj = clob;
154                         *envout = env;
155                         *ioout  = io;
156                         result = 1;
157                 } else {
158                         result = PTR_ERR(env);
159                 }
160         } else {
161                 result = 0;
162         }
163         return result;
164 }
165
166 int cl_glimpse_size0(struct inode *inode, int agl)
167 {
168         /*
169          * We don't need ast_flags argument to cl_glimpse_size(), because
170          * osc_lock_enqueue() takes care of the possible deadlock that said
171          * argument was introduced to avoid.
172          */
173         /*
174          * XXX but note that ll_file_seek() passes LDLM_FL_BLOCK_NOWAIT to
175          * cl_glimpse_size(), which doesn't make sense: glimpse locks are not
176          * blocking anyway.
177          */
178         struct lu_env     *env = NULL;
179         struct cl_io       *io  = NULL;
180         int                  result;
181         u16 refcheck;
182
183         result = cl_io_get(inode, &env, &io, &refcheck);
184         if (result > 0) {
185 again:
186                 io->ci_verify_layout = 1;
187                 result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
188                 if (result > 0)
189                         /*
190                          * nothing to do for this io. This currently happens
191                          * when stripe sub-object's are not yet created.
192                          */
193                         result = io->ci_result;
194                 else if (result == 0)
195                         result = cl_glimpse_lock(env, io, inode, io->ci_obj,
196                                                  agl);
197
198                 OBD_FAIL_TIMEOUT(OBD_FAIL_GLIMPSE_DELAY, 2);
199                 cl_io_fini(env, io);
200                 if (unlikely(io->ci_need_restart))
201                         goto again;
202                 cl_env_put(env, &refcheck);
203         }
204         return result;
205 }