• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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) 2002, 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 
37 #include <linux/fs.h>
38 #include <linux/sched.h>
39 #include <linux/mm.h>
40 #include <linux/quotaops.h>
41 #include <linux/highmem.h>
42 #include <linux/pagemap.h>
43 #include <linux/security.h>
44 
45 #define DEBUG_SUBSYSTEM S_LLITE
46 
47 #include "../include/obd_support.h"
48 #include "../include/lustre_fid.h"
49 #include "../include/lustre_lite.h"
50 #include "../include/lustre_dlm.h"
51 #include "../include/lustre_ver.h"
52 #include "llite_internal.h"
53 
54 static int ll_create_it(struct inode *, struct dentry *,
55 			int, struct lookup_intent *);
56 
57 /* called from iget5_locked->find_inode() under inode_hash_lock spinlock */
ll_test_inode(struct inode * inode,void * opaque)58 static int ll_test_inode(struct inode *inode, void *opaque)
59 {
60 	struct ll_inode_info *lli = ll_i2info(inode);
61 	struct lustre_md     *md = opaque;
62 
63 	if (unlikely(!(md->body->valid & OBD_MD_FLID))) {
64 		CERROR("MDS body missing FID\n");
65 		return 0;
66 	}
67 
68 	if (!lu_fid_eq(&lli->lli_fid, &md->body->fid1))
69 		return 0;
70 
71 	return 1;
72 }
73 
ll_set_inode(struct inode * inode,void * opaque)74 static int ll_set_inode(struct inode *inode, void *opaque)
75 {
76 	struct ll_inode_info *lli = ll_i2info(inode);
77 	struct mdt_body *body = ((struct lustre_md *)opaque)->body;
78 
79 	if (unlikely(!(body->valid & OBD_MD_FLID))) {
80 		CERROR("MDS body missing FID\n");
81 		return -EINVAL;
82 	}
83 
84 	lli->lli_fid = body->fid1;
85 	if (unlikely(!(body->valid & OBD_MD_FLTYPE))) {
86 		CERROR("Can not initialize inode " DFID
87 		       " without object type: valid = %#llx\n",
88 		       PFID(&lli->lli_fid), body->valid);
89 		return -EINVAL;
90 	}
91 
92 	inode->i_mode = (inode->i_mode & ~S_IFMT) | (body->mode & S_IFMT);
93 	if (unlikely(inode->i_mode == 0)) {
94 		CERROR("Invalid inode "DFID" type\n", PFID(&lli->lli_fid));
95 		return -EINVAL;
96 	}
97 
98 	ll_lli_init(lli);
99 
100 	return 0;
101 }
102 
103 /*
104  * Get an inode by inode number (already instantiated by the intent lookup).
105  * Returns inode or NULL
106  */
ll_iget(struct super_block * sb,ino_t hash,struct lustre_md * md)107 struct inode *ll_iget(struct super_block *sb, ino_t hash,
108 		      struct lustre_md *md)
109 {
110 	struct inode	 *inode;
111 
112 	LASSERT(hash != 0);
113 	inode = iget5_locked(sb, hash, ll_test_inode, ll_set_inode, md);
114 
115 	if (inode) {
116 		if (inode->i_state & I_NEW) {
117 			int rc = 0;
118 
119 			ll_read_inode2(inode, md);
120 			if (S_ISREG(inode->i_mode) &&
121 			    ll_i2info(inode)->lli_clob == NULL) {
122 				CDEBUG(D_INODE,
123 					"%s: apply lsm %p to inode "DFID".\n",
124 					ll_get_fsname(sb, NULL, 0), md->lsm,
125 					PFID(ll_inode2fid(inode)));
126 				rc = cl_file_inode_init(inode, md);
127 			}
128 			if (rc != 0) {
129 				make_bad_inode(inode);
130 				unlock_new_inode(inode);
131 				iput(inode);
132 				inode = ERR_PTR(rc);
133 			} else
134 				unlock_new_inode(inode);
135 		} else if (!(inode->i_state & (I_FREEING | I_CLEAR)))
136 			ll_update_inode(inode, md);
137 		CDEBUG(D_VFSTRACE, "got inode: %p for "DFID"\n",
138 		       inode, PFID(&md->body->fid1));
139 	}
140 	return inode;
141 }
142 
ll_invalidate_negative_children(struct inode * dir)143 static void ll_invalidate_negative_children(struct inode *dir)
144 {
145 	struct dentry *dentry, *tmp_subdir;
146 
147 	ll_lock_dcache(dir);
148 	hlist_for_each_entry(dentry, &dir->i_dentry, d_u.d_alias) {
149 		spin_lock(&dentry->d_lock);
150 		if (!list_empty(&dentry->d_subdirs)) {
151 			struct dentry *child;
152 
153 			list_for_each_entry_safe(child, tmp_subdir,
154 						 &dentry->d_subdirs,
155 						 d_child) {
156 				if (d_really_is_negative(child))
157 					d_lustre_invalidate(child, 1);
158 			}
159 		}
160 		spin_unlock(&dentry->d_lock);
161 	}
162 	ll_unlock_dcache(dir);
163 }
164 
ll_md_blocking_ast(struct ldlm_lock * lock,struct ldlm_lock_desc * desc,void * data,int flag)165 int ll_md_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
166 		       void *data, int flag)
167 {
168 	struct lustre_handle lockh;
169 	int rc;
170 
171 	switch (flag) {
172 	case LDLM_CB_BLOCKING:
173 		ldlm_lock2handle(lock, &lockh);
174 		rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
175 		if (rc < 0) {
176 			CDEBUG(D_INODE, "ldlm_cli_cancel: rc = %d\n", rc);
177 			return rc;
178 		}
179 		break;
180 	case LDLM_CB_CANCELING: {
181 		struct inode *inode = ll_inode_from_resource_lock(lock);
182 		__u64 bits = lock->l_policy_data.l_inodebits.bits;
183 
184 		/* Inode is set to lock->l_resource->lr_lvb_inode
185 		 * for mdc - bug 24555 */
186 		LASSERT(lock->l_ast_data == NULL);
187 
188 		if (inode == NULL)
189 			break;
190 
191 		/* Invalidate all dentries associated with this inode */
192 		LASSERT(lock->l_flags & LDLM_FL_CANCELING);
193 
194 		if (!fid_res_name_eq(ll_inode2fid(inode),
195 				     &lock->l_resource->lr_name)) {
196 			LDLM_ERROR(lock, "data mismatch with object "DFID"(%p)",
197 				   PFID(ll_inode2fid(inode)), inode);
198 			LBUG();
199 		}
200 
201 		if (bits & MDS_INODELOCK_XATTR) {
202 			ll_xattr_cache_destroy(inode);
203 			bits &= ~MDS_INODELOCK_XATTR;
204 		}
205 
206 		/* For OPEN locks we differentiate between lock modes
207 		 * LCK_CR, LCK_CW, LCK_PR - bug 22891 */
208 		if (bits & MDS_INODELOCK_OPEN)
209 			ll_have_md_lock(inode, &bits, lock->l_req_mode);
210 
211 		if (bits & MDS_INODELOCK_OPEN) {
212 			fmode_t fmode;
213 
214 			switch (lock->l_req_mode) {
215 			case LCK_CW:
216 				fmode = FMODE_WRITE;
217 				break;
218 			case LCK_PR:
219 				fmode = FMODE_EXEC;
220 				break;
221 			case LCK_CR:
222 				fmode = FMODE_READ;
223 				break;
224 			default:
225 				LDLM_ERROR(lock, "bad lock mode for OPEN lock");
226 				LBUG();
227 			}
228 
229 			ll_md_real_close(inode, fmode);
230 		}
231 
232 		if (bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
233 			    MDS_INODELOCK_LAYOUT | MDS_INODELOCK_PERM))
234 			ll_have_md_lock(inode, &bits, LCK_MINMODE);
235 
236 		if (bits & MDS_INODELOCK_LAYOUT) {
237 			struct cl_object_conf conf = {
238 				.coc_opc = OBJECT_CONF_INVALIDATE,
239 				.coc_inode = inode,
240 			};
241 
242 			rc = ll_layout_conf(inode, &conf);
243 			if (rc < 0)
244 				CDEBUG(D_INODE, "cannot invalidate layout of "
245 				       DFID": rc = %d\n",
246 				       PFID(ll_inode2fid(inode)), rc);
247 		}
248 
249 		if (bits & MDS_INODELOCK_UPDATE) {
250 			struct ll_inode_info *lli = ll_i2info(inode);
251 
252 			spin_lock(&lli->lli_lock);
253 			lli->lli_flags &= ~LLIF_MDS_SIZE_LOCK;
254 			spin_unlock(&lli->lli_lock);
255 		}
256 
257 		if ((bits & MDS_INODELOCK_UPDATE) && S_ISDIR(inode->i_mode)) {
258 			CDEBUG(D_INODE, "invalidating inode %lu\n",
259 			       inode->i_ino);
260 			truncate_inode_pages(inode->i_mapping, 0);
261 			ll_invalidate_negative_children(inode);
262 		}
263 
264 		if ((bits & (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_PERM)) &&
265 		    inode->i_sb->s_root != NULL &&
266 		    !is_root_inode(inode))
267 			ll_invalidate_aliases(inode);
268 
269 		iput(inode);
270 		break;
271 	}
272 	default:
273 		LBUG();
274 	}
275 
276 	return 0;
277 }
278 
ll_i2suppgid(struct inode * i)279 __u32 ll_i2suppgid(struct inode *i)
280 {
281 	if (in_group_p(i->i_gid))
282 		return (__u32)from_kgid(&init_user_ns, i->i_gid);
283 	else
284 		return (__u32)(-1);
285 }
286 
287 /* Pack the required supplementary groups into the supplied groups array.
288  * If we don't need to use the groups from the target inode(s) then we
289  * instead pack one or more groups from the user's supplementary group
290  * array in case it might be useful.  Not needed if doing an MDS-side upcall. */
ll_i2gids(__u32 * suppgids,struct inode * i1,struct inode * i2)291 void ll_i2gids(__u32 *suppgids, struct inode *i1, struct inode *i2)
292 {
293 #if 0
294 	int i;
295 #endif
296 
297 	LASSERT(i1 != NULL);
298 	LASSERT(suppgids != NULL);
299 
300 	suppgids[0] = ll_i2suppgid(i1);
301 
302 	if (i2)
303 		suppgids[1] = ll_i2suppgid(i2);
304 		else
305 			suppgids[1] = -1;
306 
307 #if 0
308 	for (i = 0; i < current_ngroups; i++) {
309 		if (suppgids[0] == -1) {
310 			if (current_groups[i] != suppgids[1])
311 				suppgids[0] = current_groups[i];
312 			continue;
313 		}
314 		if (suppgids[1] == -1) {
315 			if (current_groups[i] != suppgids[0])
316 				suppgids[1] = current_groups[i];
317 			continue;
318 		}
319 		break;
320 	}
321 #endif
322 }
323 
324 /*
325  * try to reuse three types of dentry:
326  * 1. unhashed alias, this one is unhashed by d_invalidate (but it may be valid
327  *    by concurrent .revalidate).
328  * 2. INVALID alias (common case for no valid ldlm lock held, but this flag may
329  *    be cleared by others calling d_lustre_revalidate).
330  * 3. DISCONNECTED alias.
331  */
ll_find_alias(struct inode * inode,struct dentry * dentry)332 static struct dentry *ll_find_alias(struct inode *inode, struct dentry *dentry)
333 {
334 	struct dentry *alias, *discon_alias, *invalid_alias;
335 
336 	if (hlist_empty(&inode->i_dentry))
337 		return NULL;
338 
339 	discon_alias = invalid_alias = NULL;
340 
341 	ll_lock_dcache(inode);
342 	hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
343 		LASSERT(alias != dentry);
344 
345 		spin_lock(&alias->d_lock);
346 		if (alias->d_flags & DCACHE_DISCONNECTED)
347 			/* LASSERT(last_discon == NULL); LU-405, bz 20055 */
348 			discon_alias = alias;
349 		else if (alias->d_parent == dentry->d_parent	     &&
350 			 alias->d_name.hash == dentry->d_name.hash       &&
351 			 alias->d_name.len == dentry->d_name.len	 &&
352 			 memcmp(alias->d_name.name, dentry->d_name.name,
353 				dentry->d_name.len) == 0)
354 			invalid_alias = alias;
355 		spin_unlock(&alias->d_lock);
356 
357 		if (invalid_alias)
358 			break;
359 	}
360 	alias = invalid_alias ?: discon_alias ?: NULL;
361 	if (alias) {
362 		spin_lock(&alias->d_lock);
363 		dget_dlock(alias);
364 		spin_unlock(&alias->d_lock);
365 	}
366 	ll_unlock_dcache(inode);
367 
368 	return alias;
369 }
370 
371 /*
372  * Similar to d_splice_alias(), but lustre treats invalid alias
373  * similar to DCACHE_DISCONNECTED, and tries to use it anyway.
374  */
ll_splice_alias(struct inode * inode,struct dentry * de)375 struct dentry *ll_splice_alias(struct inode *inode, struct dentry *de)
376 {
377 	struct dentry *new;
378 	int rc;
379 
380 	if (inode) {
381 		new = ll_find_alias(inode, de);
382 		if (new) {
383 			rc = ll_d_init(new);
384 			if (rc < 0) {
385 				dput(new);
386 				return ERR_PTR(rc);
387 			}
388 			d_move(new, de);
389 			iput(inode);
390 			CDEBUG(D_DENTRY,
391 			       "Reuse dentry %p inode %p refc %d flags %#x\n",
392 			      new, d_inode(new), d_count(new), new->d_flags);
393 			return new;
394 		}
395 	}
396 	rc = ll_d_init(de);
397 	if (rc < 0)
398 		return ERR_PTR(rc);
399 	d_add(de, inode);
400 	CDEBUG(D_DENTRY, "Add dentry %p inode %p refc %d flags %#x\n",
401 	       de, d_inode(de), d_count(de), de->d_flags);
402 	return de;
403 }
404 
ll_lookup_it_finish(struct ptlrpc_request * request,struct lookup_intent * it,struct inode * parent,struct dentry ** de)405 static int ll_lookup_it_finish(struct ptlrpc_request *request,
406 			       struct lookup_intent *it,
407 			       struct inode *parent, struct dentry **de)
408 {
409 	struct inode *inode = NULL;
410 	__u64 bits = 0;
411 	int rc = 0;
412 
413 	/* NB 1 request reference will be taken away by ll_intent_lock()
414 	 * when I return */
415 	CDEBUG(D_DENTRY, "it %p it_disposition %x\n", it,
416 	       it->d.lustre.it_disposition);
417 	if (!it_disposition(it, DISP_LOOKUP_NEG)) {
418 		rc = ll_prep_inode(&inode, request, (*de)->d_sb, it);
419 		if (rc)
420 			return rc;
421 
422 		ll_set_lock_data(ll_i2sbi(parent)->ll_md_exp, inode, it, &bits);
423 
424 		/* We used to query real size from OSTs here, but actually
425 		   this is not needed. For stat() calls size would be updated
426 		   from subsequent do_revalidate()->ll_inode_revalidate_it() in
427 		   2.4 and
428 		   vfs_getattr_it->ll_getattr()->ll_inode_revalidate_it() in 2.6
429 		   Everybody else who needs correct file size would call
430 		   ll_glimpse_size or some equivalent themselves anyway.
431 		   Also see bug 7198. */
432 	}
433 
434 	/* Only hash *de if it is unhashed (new dentry).
435 	 * Atoimc_open may passing hashed dentries for open.
436 	 */
437 	if (d_unhashed(*de)) {
438 		struct dentry *alias;
439 
440 		alias = ll_splice_alias(inode, *de);
441 		if (IS_ERR(alias)) {
442 			rc = PTR_ERR(alias);
443 			goto out;
444 		}
445 		*de = alias;
446 	} else if (!it_disposition(it, DISP_LOOKUP_NEG)  &&
447 		   !it_disposition(it, DISP_OPEN_CREATE)) {
448 		/* With DISP_OPEN_CREATE dentry will
449 		   instantiated in ll_create_it. */
450 		LASSERT(d_inode(*de) == NULL);
451 		d_instantiate(*de, inode);
452 	}
453 
454 	if (!it_disposition(it, DISP_LOOKUP_NEG)) {
455 		/* we have lookup look - unhide dentry */
456 		if (bits & MDS_INODELOCK_LOOKUP)
457 			d_lustre_revalidate(*de);
458 	} else if (!it_disposition(it, DISP_OPEN_CREATE)) {
459 		/* If file created on server, don't depend on parent UPDATE
460 		 * lock to unhide it. It is left hidden and next lookup can
461 		 * find it in ll_splice_alias.
462 		 */
463 		/* Check that parent has UPDATE lock. */
464 		struct lookup_intent parent_it = {
465 					.it_op = IT_GETATTR,
466 					.d.lustre.it_lock_handle = 0 };
467 
468 		if (md_revalidate_lock(ll_i2mdexp(parent), &parent_it,
469 				       &ll_i2info(parent)->lli_fid, NULL)) {
470 			d_lustre_revalidate(*de);
471 			ll_intent_release(&parent_it);
472 		}
473 	}
474 
475 out:
476 	if (rc != 0 && it->it_op & IT_OPEN)
477 		ll_open_cleanup((*de)->d_sb, request);
478 
479 	return rc;
480 }
481 
ll_lookup_it(struct inode * parent,struct dentry * dentry,struct lookup_intent * it,int lookup_flags)482 static struct dentry *ll_lookup_it(struct inode *parent, struct dentry *dentry,
483 				   struct lookup_intent *it, int lookup_flags)
484 {
485 	struct lookup_intent lookup_it = { .it_op = IT_LOOKUP };
486 	struct dentry *save = dentry, *retval;
487 	struct ptlrpc_request *req = NULL;
488 	struct inode *inode;
489 	struct md_op_data *op_data;
490 	__u32 opc;
491 	int rc;
492 
493 	if (dentry->d_name.len > ll_i2sbi(parent)->ll_namelen)
494 		return ERR_PTR(-ENAMETOOLONG);
495 
496 	CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p),intent=%s\n",
497 	       dentry, parent->i_ino,
498 	       parent->i_generation, parent, LL_IT2STR(it));
499 
500 	if (d_mountpoint(dentry))
501 		CERROR("Tell Peter, lookup on mtpt, it %s\n", LL_IT2STR(it));
502 
503 	if (it == NULL || it->it_op == IT_GETXATTR)
504 		it = &lookup_it;
505 
506 	if (it->it_op == IT_GETATTR) {
507 		rc = ll_statahead_enter(parent, &dentry, 0);
508 		if (rc == 1) {
509 			if (dentry == save)
510 				retval = NULL;
511 			else
512 				retval = dentry;
513 			goto out;
514 		}
515 	}
516 
517 	if (it->it_op & IT_CREAT)
518 		opc = LUSTRE_OPC_CREATE;
519 	else
520 		opc = LUSTRE_OPC_ANY;
521 
522 	op_data = ll_prep_md_op_data(NULL, parent, NULL, dentry->d_name.name,
523 				     dentry->d_name.len, lookup_flags, opc,
524 				     NULL);
525 	if (IS_ERR(op_data))
526 		return (void *)op_data;
527 
528 	/* enforce umask if acl disabled or MDS doesn't support umask */
529 	if (!IS_POSIXACL(parent) || !exp_connect_umask(ll_i2mdexp(parent)))
530 		it->it_create_mode &= ~current_umask();
531 
532 	rc = md_intent_lock(ll_i2mdexp(parent), op_data, NULL, 0, it,
533 			    lookup_flags, &req, ll_md_blocking_ast, 0);
534 	ll_finish_md_op_data(op_data);
535 	if (rc < 0) {
536 		retval = ERR_PTR(rc);
537 		goto out;
538 	}
539 
540 	rc = ll_lookup_it_finish(req, it, parent, &dentry);
541 	if (rc != 0) {
542 		ll_intent_release(it);
543 		retval = ERR_PTR(rc);
544 		goto out;
545 	}
546 
547 	inode = d_inode(dentry);
548 	if ((it->it_op & IT_OPEN) && inode &&
549 	    !S_ISREG(inode->i_mode) &&
550 	    !S_ISDIR(inode->i_mode)) {
551 		ll_release_openhandle(inode, it);
552 	}
553 	ll_lookup_finish_locks(it, inode);
554 
555 	if (dentry == save)
556 		retval = NULL;
557 	else
558 		retval = dentry;
559 	goto out;
560  out:
561 	if (req)
562 		ptlrpc_req_finished(req);
563 	if (it->it_op == IT_GETATTR && (retval == NULL || retval == dentry))
564 		ll_statahead_mark(parent, dentry);
565 	return retval;
566 }
567 
ll_lookup_nd(struct inode * parent,struct dentry * dentry,unsigned int flags)568 static struct dentry *ll_lookup_nd(struct inode *parent, struct dentry *dentry,
569 				   unsigned int flags)
570 {
571 	struct lookup_intent *itp, it = { .it_op = IT_GETATTR };
572 	struct dentry *de;
573 
574 	CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p),flags=%u\n",
575 	       dentry, parent->i_ino,
576 	       parent->i_generation, parent, flags);
577 
578 	/* Optimize away (CREATE && !OPEN). Let .create handle the race. */
579 	if ((flags & LOOKUP_CREATE) && !(flags & LOOKUP_OPEN))
580 		return NULL;
581 
582 	if (flags & (LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE))
583 		itp = NULL;
584 	else
585 		itp = &it;
586 	de = ll_lookup_it(parent, dentry, itp, 0);
587 
588 	if (itp != NULL)
589 		ll_intent_release(itp);
590 
591 	return de;
592 }
593 
594 /*
595  * For cached negative dentry and new dentry, handle lookup/create/open
596  * together.
597  */
ll_atomic_open(struct inode * dir,struct dentry * dentry,struct file * file,unsigned open_flags,umode_t mode,int * opened)598 static int ll_atomic_open(struct inode *dir, struct dentry *dentry,
599 			  struct file *file, unsigned open_flags,
600 			  umode_t mode, int *opened)
601 {
602 	struct lookup_intent *it;
603 	struct dentry *de;
604 	long long lookup_flags = LOOKUP_OPEN;
605 	int rc = 0;
606 
607 	CDEBUG(D_VFSTRACE,
608 	       "VFS Op:name=%pd,dir=%lu/%u(%p),file %p,open_flags %x,mode %x opened %d\n",
609 	       dentry, dir->i_ino,
610 	       dir->i_generation, dir, file, open_flags, mode, *opened);
611 
612 	it = kzalloc(sizeof(*it), GFP_NOFS);
613 	if (!it)
614 		return -ENOMEM;
615 
616 	it->it_op = IT_OPEN;
617 	if (open_flags & O_CREAT) {
618 		it->it_op |= IT_CREAT;
619 		lookup_flags |= LOOKUP_CREATE;
620 	}
621 	it->it_create_mode = (mode & S_IALLUGO) | S_IFREG;
622 	it->it_flags = (open_flags & ~O_ACCMODE) | OPEN_FMODE(open_flags);
623 
624 	/* Dentry added to dcache tree in ll_lookup_it */
625 	de = ll_lookup_it(dir, dentry, it, lookup_flags);
626 	if (IS_ERR(de))
627 		rc = PTR_ERR(de);
628 	else if (de != NULL)
629 		dentry = de;
630 
631 	if (!rc) {
632 		if (it_disposition(it, DISP_OPEN_CREATE)) {
633 			/* Dentry instantiated in ll_create_it. */
634 			rc = ll_create_it(dir, dentry, mode, it);
635 			if (rc) {
636 				/* We dget in ll_splice_alias. */
637 				if (de != NULL)
638 					dput(de);
639 				goto out_release;
640 			}
641 
642 			*opened |= FILE_CREATED;
643 		}
644 		if (d_really_is_positive(dentry) && it_disposition(it, DISP_OPEN_OPEN)) {
645 			/* Open dentry. */
646 			if (S_ISFIFO(d_inode(dentry)->i_mode)) {
647 				/* We cannot call open here as it would
648 				 * deadlock.
649 				 */
650 				if (it_disposition(it, DISP_ENQ_OPEN_REF))
651 					ptlrpc_req_finished(
652 						       (struct ptlrpc_request *)
653 							  it->d.lustre.it_data);
654 				rc = finish_no_open(file, de);
655 			} else {
656 				file->private_data = it;
657 				rc = finish_open(file, dentry, NULL, opened);
658 				/* We dget in ll_splice_alias. finish_open takes
659 				 * care of dget for fd open.
660 				 */
661 				if (de != NULL)
662 					dput(de);
663 			}
664 		} else {
665 			rc = finish_no_open(file, de);
666 		}
667 	}
668 
669 out_release:
670 	ll_intent_release(it);
671 	kfree(it);
672 
673 	return rc;
674 }
675 
676 /* We depend on "mode" being set with the proper file type/umask by now */
ll_create_node(struct inode * dir,struct lookup_intent * it)677 static struct inode *ll_create_node(struct inode *dir, struct lookup_intent *it)
678 {
679 	struct inode *inode = NULL;
680 	struct ptlrpc_request *request = NULL;
681 	struct ll_sb_info *sbi = ll_i2sbi(dir);
682 	int rc;
683 
684 	LASSERT(it && it->d.lustre.it_disposition);
685 
686 	LASSERT(it_disposition(it, DISP_ENQ_CREATE_REF));
687 	request = it->d.lustre.it_data;
688 	it_clear_disposition(it, DISP_ENQ_CREATE_REF);
689 	rc = ll_prep_inode(&inode, request, dir->i_sb, it);
690 	if (rc) {
691 		inode = ERR_PTR(rc);
692 		goto out;
693 	}
694 
695 	LASSERT(hlist_empty(&inode->i_dentry));
696 
697 	/* We asked for a lock on the directory, but were granted a
698 	 * lock on the inode.  Since we finally have an inode pointer,
699 	 * stuff it in the lock. */
700 	CDEBUG(D_DLMTRACE, "setting l_ast_data to inode %p (%lu/%u)\n",
701 	       inode, inode->i_ino, inode->i_generation);
702 	ll_set_lock_data(sbi->ll_md_exp, inode, it, NULL);
703  out:
704 	ptlrpc_req_finished(request);
705 	return inode;
706 }
707 
708 /*
709  * By the time this is called, we already have created the directory cache
710  * entry for the new file, but it is so far negative - it has no inode.
711  *
712  * We defer creating the OBD object(s) until open, to keep the intent and
713  * non-intent code paths similar, and also because we do not have the MDS
714  * inode number before calling ll_create_node() (which is needed for LOV),
715  * so we would need to do yet another RPC to the MDS to store the LOV EA
716  * data on the MDS.  If needed, we would pass the PACKED lmm as data and
717  * lmm_size in datalen (the MDS still has code which will handle that).
718  *
719  * If the create succeeds, we fill in the inode information
720  * with d_instantiate().
721  */
ll_create_it(struct inode * dir,struct dentry * dentry,int mode,struct lookup_intent * it)722 static int ll_create_it(struct inode *dir, struct dentry *dentry, int mode,
723 			struct lookup_intent *it)
724 {
725 	struct inode *inode;
726 	int rc = 0;
727 
728 	CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p),intent=%s\n",
729 	       dentry, dir->i_ino,
730 	       dir->i_generation, dir, LL_IT2STR(it));
731 
732 	rc = it_open_error(DISP_OPEN_CREATE, it);
733 	if (rc)
734 		return rc;
735 
736 	inode = ll_create_node(dir, it);
737 	if (IS_ERR(inode))
738 		return PTR_ERR(inode);
739 
740 	d_instantiate(dentry, inode);
741 	return 0;
742 }
743 
ll_update_times(struct ptlrpc_request * request,struct inode * inode)744 static void ll_update_times(struct ptlrpc_request *request,
745 			    struct inode *inode)
746 {
747 	struct mdt_body *body = req_capsule_server_get(&request->rq_pill,
748 						       &RMF_MDT_BODY);
749 
750 	LASSERT(body);
751 	if (body->valid & OBD_MD_FLMTIME &&
752 	    body->mtime > LTIME_S(inode->i_mtime)) {
753 		CDEBUG(D_INODE, "setting ino %lu mtime from %lu to %llu\n",
754 		       inode->i_ino, LTIME_S(inode->i_mtime), body->mtime);
755 		LTIME_S(inode->i_mtime) = body->mtime;
756 	}
757 	if (body->valid & OBD_MD_FLCTIME &&
758 	    body->ctime > LTIME_S(inode->i_ctime))
759 		LTIME_S(inode->i_ctime) = body->ctime;
760 }
761 
ll_new_node(struct inode * dir,struct dentry * dentry,const char * tgt,int mode,int rdev,__u32 opc)762 static int ll_new_node(struct inode *dir, struct dentry *dentry,
763 		       const char *tgt, int mode, int rdev,
764 		       __u32 opc)
765 {
766 	struct ptlrpc_request *request = NULL;
767 	struct md_op_data *op_data;
768 	struct inode *inode = NULL;
769 	struct ll_sb_info *sbi = ll_i2sbi(dir);
770 	int tgt_len = 0;
771 	int err;
772 
773 	if (unlikely(tgt != NULL))
774 		tgt_len = strlen(tgt) + 1;
775 
776 	op_data = ll_prep_md_op_data(NULL, dir, NULL,
777 				     dentry->d_name.name,
778 				     dentry->d_name.len,
779 				     0, opc, NULL);
780 	if (IS_ERR(op_data)) {
781 		err = PTR_ERR(op_data);
782 		goto err_exit;
783 	}
784 
785 	err = md_create(sbi->ll_md_exp, op_data, tgt, tgt_len, mode,
786 			from_kuid(&init_user_ns, current_fsuid()),
787 			from_kgid(&init_user_ns, current_fsgid()),
788 			cfs_curproc_cap_pack(), rdev, &request);
789 	ll_finish_md_op_data(op_data);
790 	if (err)
791 		goto err_exit;
792 
793 	ll_update_times(request, dir);
794 
795 	err = ll_prep_inode(&inode, request, dir->i_sb, NULL);
796 	if (err)
797 		goto err_exit;
798 
799 	d_instantiate(dentry, inode);
800 err_exit:
801 	ptlrpc_req_finished(request);
802 
803 	return err;
804 }
805 
ll_mknod(struct inode * dir,struct dentry * dchild,umode_t mode,dev_t rdev)806 static int ll_mknod(struct inode *dir, struct dentry *dchild,
807 		    umode_t mode, dev_t rdev)
808 {
809 	int err;
810 
811 	CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p) mode %o dev %x\n",
812 	       dchild, dir->i_ino, dir->i_generation, dir,
813 	       mode, old_encode_dev(rdev));
814 
815 	if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
816 		mode &= ~current_umask();
817 
818 	switch (mode & S_IFMT) {
819 	case 0:
820 		mode |= S_IFREG; /* for mode = 0 case, fallthrough */
821 	case S_IFREG:
822 	case S_IFCHR:
823 	case S_IFBLK:
824 	case S_IFIFO:
825 	case S_IFSOCK:
826 		err = ll_new_node(dir, dchild, NULL, mode,
827 				  old_encode_dev(rdev),
828 				  LUSTRE_OPC_MKNOD);
829 		break;
830 	case S_IFDIR:
831 		err = -EPERM;
832 		break;
833 	default:
834 		err = -EINVAL;
835 	}
836 
837 	if (!err)
838 		ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKNOD, 1);
839 
840 	return err;
841 }
842 
843 /*
844  * Plain create. Intent create is handled in atomic_open.
845  */
ll_create_nd(struct inode * dir,struct dentry * dentry,umode_t mode,bool want_excl)846 static int ll_create_nd(struct inode *dir, struct dentry *dentry,
847 			umode_t mode, bool want_excl)
848 {
849 	int rc;
850 
851 	CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p),flags=%u, excl=%d\n",
852 	       dentry, dir->i_ino,
853 	       dir->i_generation, dir, mode, want_excl);
854 
855 	rc = ll_mknod(dir, dentry, mode, 0);
856 
857 	ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_CREATE, 1);
858 
859 	CDEBUG(D_VFSTRACE, "VFS Op:name=%pd, unhashed %d\n",
860 	       dentry, d_unhashed(dentry));
861 
862 	return rc;
863 }
864 
ll_get_child_fid(struct dentry * child,struct lu_fid * fid)865 static inline void ll_get_child_fid(struct dentry *child, struct lu_fid *fid)
866 {
867 	if (d_really_is_positive(child))
868 		*fid = *ll_inode2fid(d_inode(child));
869 }
870 
ll_objects_destroy(struct ptlrpc_request * request,struct inode * dir)871 int ll_objects_destroy(struct ptlrpc_request *request, struct inode *dir)
872 {
873 	struct mdt_body *body;
874 	struct lov_mds_md *eadata;
875 	struct lov_stripe_md *lsm = NULL;
876 	struct obd_trans_info oti = { 0 };
877 	struct obdo *oa;
878 	int rc;
879 
880 	/* req is swabbed so this is safe */
881 	body = req_capsule_server_get(&request->rq_pill, &RMF_MDT_BODY);
882 	if (!(body->valid & OBD_MD_FLEASIZE))
883 		return 0;
884 
885 	if (body->eadatasize == 0) {
886 		CERROR("OBD_MD_FLEASIZE set but eadatasize zero\n");
887 		rc = -EPROTO;
888 		goto out;
889 	}
890 
891 	/* The MDS sent back the EA because we unlinked the last reference
892 	 * to this file. Use this EA to unlink the objects on the OST.
893 	 * It's opaque so we don't swab here; we leave it to obd_unpackmd() to
894 	 * check it is complete and sensible. */
895 	eadata = req_capsule_server_sized_get(&request->rq_pill, &RMF_MDT_MD,
896 					      body->eadatasize);
897 	LASSERT(eadata != NULL);
898 
899 	rc = obd_unpackmd(ll_i2dtexp(dir), &lsm, eadata, body->eadatasize);
900 	if (rc < 0) {
901 		CERROR("obd_unpackmd: %d\n", rc);
902 		goto out;
903 	}
904 	LASSERT(rc >= sizeof(*lsm));
905 
906 	oa = kmem_cache_alloc(obdo_cachep, GFP_NOFS | __GFP_ZERO);
907 	if (oa == NULL) {
908 		rc = -ENOMEM;
909 		goto out_free_memmd;
910 	}
911 
912 	oa->o_oi = lsm->lsm_oi;
913 	oa->o_mode = body->mode & S_IFMT;
914 	oa->o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLGROUP;
915 
916 	if (body->valid & OBD_MD_FLCOOKIE) {
917 		oa->o_valid |= OBD_MD_FLCOOKIE;
918 		oti.oti_logcookies =
919 			req_capsule_server_sized_get(&request->rq_pill,
920 						     &RMF_LOGCOOKIES,
921 						   sizeof(struct llog_cookie) *
922 						     lsm->lsm_stripe_count);
923 		if (oti.oti_logcookies == NULL) {
924 			oa->o_valid &= ~OBD_MD_FLCOOKIE;
925 			body->valid &= ~OBD_MD_FLCOOKIE;
926 		}
927 	}
928 
929 	rc = obd_destroy(NULL, ll_i2dtexp(dir), oa, lsm, &oti,
930 			 ll_i2mdexp(dir));
931 	if (rc)
932 		CERROR("obd destroy objid "DOSTID" error %d\n",
933 		       POSTID(&lsm->lsm_oi), rc);
934 out_free_memmd:
935 	obd_free_memmd(ll_i2dtexp(dir), &lsm);
936 	kmem_cache_free(obdo_cachep, oa);
937 out:
938 	return rc;
939 }
940 
941 /* ll_unlink() doesn't update the inode with the new link count.
942  * Instead, ll_ddelete() and ll_d_iput() will update it based upon if there
943  * is any lock existing. They will recycle dentries and inodes based upon locks
944  * too. b=20433 */
ll_unlink(struct inode * dir,struct dentry * dentry)945 static int ll_unlink(struct inode *dir, struct dentry *dentry)
946 {
947 	struct ptlrpc_request *request = NULL;
948 	struct md_op_data *op_data;
949 	int rc;
950 
951 	CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p)\n",
952 	       dentry, dir->i_ino, dir->i_generation, dir);
953 
954 	op_data = ll_prep_md_op_data(NULL, dir, NULL,
955 				     dentry->d_name.name,
956 				     dentry->d_name.len,
957 				     0, LUSTRE_OPC_ANY, NULL);
958 	if (IS_ERR(op_data))
959 		return PTR_ERR(op_data);
960 
961 	ll_get_child_fid(dentry, &op_data->op_fid3);
962 	op_data->op_fid2 = op_data->op_fid3;
963 	rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
964 	ll_finish_md_op_data(op_data);
965 	if (rc)
966 		goto out;
967 
968 	ll_update_times(request, dir);
969 	ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_UNLINK, 1);
970 
971 	rc = ll_objects_destroy(request, dir);
972  out:
973 	ptlrpc_req_finished(request);
974 	return rc;
975 }
976 
ll_mkdir(struct inode * dir,struct dentry * dentry,umode_t mode)977 static int ll_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
978 {
979 	int err;
980 
981 	CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p)\n",
982 	       dentry, dir->i_ino, dir->i_generation, dir);
983 
984 	if (!IS_POSIXACL(dir) || !exp_connect_umask(ll_i2mdexp(dir)))
985 		mode &= ~current_umask();
986 	mode = (mode & (S_IRWXUGO|S_ISVTX)) | S_IFDIR;
987 	err = ll_new_node(dir, dentry, NULL, mode, 0, LUSTRE_OPC_MKDIR);
988 
989 	if (!err)
990 		ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_MKDIR, 1);
991 
992 	return err;
993 }
994 
ll_rmdir(struct inode * dir,struct dentry * dentry)995 static int ll_rmdir(struct inode *dir, struct dentry *dentry)
996 {
997 	struct ptlrpc_request *request = NULL;
998 	struct md_op_data *op_data;
999 	int rc;
1000 
1001 	CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p)\n",
1002 	       dentry, dir->i_ino, dir->i_generation, dir);
1003 
1004 	op_data = ll_prep_md_op_data(NULL, dir, NULL,
1005 				     dentry->d_name.name,
1006 				     dentry->d_name.len,
1007 				     S_IFDIR, LUSTRE_OPC_ANY, NULL);
1008 	if (IS_ERR(op_data))
1009 		return PTR_ERR(op_data);
1010 
1011 	ll_get_child_fid(dentry, &op_data->op_fid3);
1012 	op_data->op_fid2 = op_data->op_fid3;
1013 	rc = md_unlink(ll_i2sbi(dir)->ll_md_exp, op_data, &request);
1014 	ll_finish_md_op_data(op_data);
1015 	if (rc == 0) {
1016 		ll_update_times(request, dir);
1017 		ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_RMDIR, 1);
1018 	}
1019 
1020 	ptlrpc_req_finished(request);
1021 	return rc;
1022 }
1023 
ll_symlink(struct inode * dir,struct dentry * dentry,const char * oldname)1024 static int ll_symlink(struct inode *dir, struct dentry *dentry,
1025 		      const char *oldname)
1026 {
1027 	int err;
1028 
1029 	CDEBUG(D_VFSTRACE, "VFS Op:name=%pd,dir=%lu/%u(%p),target=%.*s\n",
1030 	       dentry, dir->i_ino, dir->i_generation,
1031 	       dir, 3000, oldname);
1032 
1033 	err = ll_new_node(dir, dentry, oldname, S_IFLNK | S_IRWXUGO,
1034 			0, LUSTRE_OPC_SYMLINK);
1035 
1036 	if (!err)
1037 		ll_stats_ops_tally(ll_i2sbi(dir), LPROC_LL_SYMLINK, 1);
1038 
1039 	return err;
1040 }
1041 
ll_link(struct dentry * old_dentry,struct inode * dir,struct dentry * new_dentry)1042 static int ll_link(struct dentry *old_dentry, struct inode *dir,
1043 		   struct dentry *new_dentry)
1044 {
1045 	struct inode *src = d_inode(old_dentry);
1046 	struct ll_sb_info *sbi = ll_i2sbi(dir);
1047 	struct ptlrpc_request *request = NULL;
1048 	struct md_op_data *op_data;
1049 	int err;
1050 
1051 	CDEBUG(D_VFSTRACE,
1052 	       "VFS Op: inode=%lu/%u(%p), dir=%lu/%u(%p), target=%pd\n",
1053 	       src->i_ino, src->i_generation, src, dir->i_ino,
1054 	       dir->i_generation, dir, new_dentry);
1055 
1056 	op_data = ll_prep_md_op_data(NULL, src, dir, new_dentry->d_name.name,
1057 				     new_dentry->d_name.len,
1058 				     0, LUSTRE_OPC_ANY, NULL);
1059 	if (IS_ERR(op_data))
1060 		return PTR_ERR(op_data);
1061 
1062 	err = md_link(sbi->ll_md_exp, op_data, &request);
1063 	ll_finish_md_op_data(op_data);
1064 	if (err)
1065 		goto out;
1066 
1067 	ll_update_times(request, dir);
1068 	ll_stats_ops_tally(sbi, LPROC_LL_LINK, 1);
1069 out:
1070 	ptlrpc_req_finished(request);
1071 	return err;
1072 }
1073 
ll_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry)1074 static int ll_rename(struct inode *old_dir, struct dentry *old_dentry,
1075 		     struct inode *new_dir, struct dentry *new_dentry)
1076 {
1077 	struct ptlrpc_request *request = NULL;
1078 	struct ll_sb_info *sbi = ll_i2sbi(old_dir);
1079 	struct md_op_data *op_data;
1080 	int err;
1081 
1082 	CDEBUG(D_VFSTRACE,
1083 	       "VFS Op:oldname=%pd,src_dir=%lu/%u(%p),newname=%pd,tgt_dir=%lu/%u(%p)\n",
1084 	       old_dentry, old_dir->i_ino, old_dir->i_generation, old_dir,
1085 	       new_dentry, new_dir->i_ino, new_dir->i_generation, new_dir);
1086 
1087 	op_data = ll_prep_md_op_data(NULL, old_dir, new_dir, NULL, 0, 0,
1088 				     LUSTRE_OPC_ANY, NULL);
1089 	if (IS_ERR(op_data))
1090 		return PTR_ERR(op_data);
1091 
1092 	ll_get_child_fid(old_dentry, &op_data->op_fid3);
1093 	ll_get_child_fid(new_dentry, &op_data->op_fid4);
1094 	err = md_rename(sbi->ll_md_exp, op_data,
1095 			old_dentry->d_name.name,
1096 			old_dentry->d_name.len,
1097 			new_dentry->d_name.name,
1098 			new_dentry->d_name.len, &request);
1099 	ll_finish_md_op_data(op_data);
1100 	if (!err) {
1101 		ll_update_times(request, old_dir);
1102 		ll_update_times(request, new_dir);
1103 		ll_stats_ops_tally(sbi, LPROC_LL_RENAME, 1);
1104 		err = ll_objects_destroy(request, old_dir);
1105 	}
1106 
1107 	ptlrpc_req_finished(request);
1108 	if (!err)
1109 		d_move(old_dentry, new_dentry);
1110 	return err;
1111 }
1112 
1113 const struct inode_operations ll_dir_inode_operations = {
1114 	.mknod	      = ll_mknod,
1115 	.atomic_open	    = ll_atomic_open,
1116 	.lookup	     = ll_lookup_nd,
1117 	.create	     = ll_create_nd,
1118 	/* We need all these non-raw things for NFSD, to not patch it. */
1119 	.unlink	     = ll_unlink,
1120 	.mkdir	      = ll_mkdir,
1121 	.rmdir	      = ll_rmdir,
1122 	.symlink	    = ll_symlink,
1123 	.link	       = ll_link,
1124 	.rename	     = ll_rename,
1125 	.setattr	    = ll_setattr,
1126 	.getattr	    = ll_getattr,
1127 	.permission	 = ll_inode_permission,
1128 	.setxattr	   = ll_setxattr,
1129 	.getxattr	   = ll_getxattr,
1130 	.listxattr	  = ll_listxattr,
1131 	.removexattr	= ll_removexattr,
1132 	.get_acl	    = ll_get_acl,
1133 };
1134 
1135 const struct inode_operations ll_special_inode_operations = {
1136 	.setattr	= ll_setattr,
1137 	.getattr	= ll_getattr,
1138 	.permission     = ll_inode_permission,
1139 	.setxattr       = ll_setxattr,
1140 	.getxattr       = ll_getxattr,
1141 	.listxattr      = ll_listxattr,
1142 	.removexattr    = ll_removexattr,
1143 	.get_acl	    = ll_get_acl,
1144 };
1145