Repository navigation
Expand file tree
/
Copy pathinode.c
More file actions
3340 lines (2931 loc) · 93.4 KB
/
Copy pathinode.c
File metadata and controls
3340 lines (2931 loc) · 93.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0
#include <linux/ceph/ceph_debug.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/uaccess.h>
#include <linux/kernel.h>
#include <linux/writeback.h>
#include <linux/vmalloc.h>
#include <linux/xattr.h>
#include <linux/posix_acl.h>
#include <linux/random.h>
#include <linux/sort.h>
#include <linux/iversion.h>
#include <linux/fscrypt.h>
#include "super.h"
#include "mds_client.h"
#include "cache.h"
#include "crypto.h"
#include <linux/ceph/decode.h>
/*
* Ceph inode operations
*
* Implement basic inode helpers (get, alloc) and inode ops (getattr,
* setattr, etc.), xattr helpers, and helpers for assimilating
* metadata returned by the MDS into our cache.
*
* Also define helpers for doing asynchronous writeback, invalidation,
* and truncation for the benefit of those who can't afford to block
* (typically because they are in the message handler path).
*/
static const struct inode_operations ceph_symlink_iops;
static const struct inode_operations ceph_encrypted_symlink_iops;
static void ceph_inode_work(struct work_struct *work);
/*
* find or create an inode, given the ceph ino number
*/
static int ceph_set_ino_cb(struct inode *inode, void *data)
{
#if BITS_PER_LONG >= 64
const struct ceph_vino *vino = data;
#endif
struct ceph_inode_info *ci = ceph_inode(inode);
struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
#if BITS_PER_LONG >= 64
inode->i_ino = vino->ino;
ci->i_snap = vino->snap;
#else
ci->i_vino = *(struct ceph_vino *)data;
inode->i_ino = ceph_vino_to_ino_t(ci->i_vino);
#endif
inode_set_iversion_raw(inode, 0);
percpu_counter_inc(&mdsc->metric.total_inodes);
return 0;
}
/*
* Check if the parent inode matches the vino from directory reply info
*/
static inline bool ceph_vino_matches_parent(struct inode *parent,
struct ceph_vino vino)
{
return ceph_ino(parent) == vino.ino && ceph_snap(parent) == vino.snap;
}
/*
* Validate that the directory inode referenced by @req->r_parent matches the
* inode number and snapshot id contained in the reply's directory record. If
* they do not match – which can theoretically happen if the parent dentry was
* moved between the time the request was issued and the reply arrived – fall
* back to looking up the correct inode in the inode cache.
*
* A reference is *always* returned. Callers that receive a different inode
* than the original @parent are responsible for dropping the extra reference
* once the reply has been processed.
*/
static struct inode *ceph_get_reply_dir(struct super_block *sb,
struct inode *parent,
struct ceph_mds_reply_info_parsed *rinfo)
{
struct ceph_vino vino;
if (unlikely(!rinfo->diri.in))
return parent; /* nothing to compare against */
/* If we didn't have a cached parent inode to begin with, just bail out. */
if (!parent)
return NULL;
vino.ino = le64_to_cpu(rinfo->diri.in->ino);
vino.snap = le64_to_cpu(rinfo->diri.in->snapid);
if (likely(ceph_vino_matches_parent(parent, vino)))
return parent; /* matches – use the original reference */
/* Mismatch – this should be rare. Emit a WARN and obtain the correct inode. */
WARN_ONCE(1, "ceph: reply dir mismatch (parent valid %llx.%llx reply %llx.%llx)\n",
ceph_ino(parent), ceph_snap(parent), vino.ino, vino.snap);
return ceph_get_inode(sb, vino, NULL);
}
/**
* ceph_new_inode - allocate a new inode in advance of an expected create
* @dir: parent directory for new inode
* @dentry: dentry that may eventually point to new inode
* @mode: mode of new inode
* @as_ctx: pointer to inherited security context
*
* Allocate a new inode in advance of an operation to create a new inode.
* This allocates the inode and sets up the acl_sec_ctx with appropriate
* info for the new inode.
*
* Returns a pointer to the new inode or an ERR_PTR.
*/
struct inode *ceph_new_inode(struct inode *dir, struct dentry *dentry,
umode_t *mode, struct ceph_acl_sec_ctx *as_ctx)
{
int err;
struct inode *inode;
inode = new_inode(dir->i_sb);
if (!inode)
return ERR_PTR(-ENOMEM);
inode->i_blkbits = CEPH_FSCRYPT_BLOCK_SHIFT;
if (!S_ISLNK(*mode)) {
err = ceph_pre_init_acls(dir, mode, as_ctx);
if (err < 0)
goto out_err;
}
inode_state_assign_raw(inode, 0);
inode->i_mode = *mode;
err = ceph_security_init_secctx(dentry, *mode, as_ctx);
if (err < 0)
goto out_err;
/*
* We'll skip setting fscrypt context for snapshots, leaving that for
* the handle_reply().
*/
if (ceph_snap(dir) != CEPH_SNAPDIR) {
err = ceph_fscrypt_prepare_context(dir, inode, as_ctx);
if (err)
goto out_err;
}
return inode;
out_err:
iput(inode);
return ERR_PTR(err);
}
void ceph_as_ctx_to_req(struct ceph_mds_request *req,
struct ceph_acl_sec_ctx *as_ctx)
{
if (as_ctx->pagelist) {
req->r_pagelist = as_ctx->pagelist;
as_ctx->pagelist = NULL;
}
ceph_fscrypt_as_ctx_to_req(req, as_ctx);
}
/**
* ceph_get_inode - find or create/hash a new inode
* @sb: superblock to search and allocate in
* @vino: vino to search for
* @newino: optional new inode to insert if one isn't found (may be NULL)
*
* Search for or insert a new inode into the hash for the given vino, and
* return a reference to it. If new is non-NULL, its reference is consumed.
*/
struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino,
struct inode *newino)
{
struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(sb);
struct ceph_client *cl = mdsc->fsc->client;
struct inode *inode;
if (ceph_vino_is_reserved(vino))
return ERR_PTR(-EREMOTEIO);
if (newino) {
inode = inode_insert5(newino, (unsigned long)vino.ino,
ceph_ino_compare, ceph_set_ino_cb, &vino);
if (inode != newino)
iput(newino);
} else {
inode = iget5_locked(sb, (unsigned long)vino.ino,
ceph_ino_compare, ceph_set_ino_cb, &vino);
}
if (!inode) {
doutc(cl, "no inode found for %llx.%llx\n", vino.ino, vino.snap);
return ERR_PTR(-ENOMEM);
}
doutc(cl, "on %llx=%llx.%llx got %p new %d\n",
ceph_present_inode(inode), ceph_vinop(inode), inode,
!!(inode_state_read_once(inode) & I_NEW));
return inode;
}
/*
* get/construct snapdir inode for a given directory
*/
struct inode *ceph_get_snapdir(struct inode *parent)
{
struct ceph_client *cl = ceph_inode_to_client(parent);
struct ceph_vino vino = {
.ino = ceph_ino(parent),
.snap = CEPH_SNAPDIR,
};
struct inode *inode = ceph_get_inode(parent->i_sb, vino, NULL);
struct ceph_inode_info *ci = ceph_inode(inode);
int ret = -ENOTDIR;
if (IS_ERR(inode))
return inode;
if (!S_ISDIR(parent->i_mode)) {
pr_warn_once_client(cl, "bad snapdir parent type (mode=0%o)\n",
parent->i_mode);
goto err;
}
if (!(inode_state_read_once(inode) & I_NEW) && !S_ISDIR(inode->i_mode)) {
pr_warn_once_client(cl, "bad snapdir inode type (mode=0%o)\n",
inode->i_mode);
goto err;
}
inode->i_mode = parent->i_mode;
inode->i_uid = parent->i_uid;
inode->i_gid = parent->i_gid;
inode_set_mtime_to_ts(inode, inode_get_mtime(parent));
inode_set_ctime_to_ts(inode, inode_get_ctime(parent));
inode_set_atime_to_ts(inode, inode_get_atime(parent));
ci->i_rbytes = 0;
ci->i_btime = ceph_inode(parent)->i_btime;
#ifdef CONFIG_FS_ENCRYPTION
/* if encrypted, just borrow fscrypt_auth from parent */
if (IS_ENCRYPTED(parent)) {
struct ceph_inode_info *pci = ceph_inode(parent);
ci->fscrypt_auth = kmemdup(pci->fscrypt_auth,
pci->fscrypt_auth_len,
GFP_KERNEL);
if (ci->fscrypt_auth) {
inode->i_flags |= S_ENCRYPTED;
ci->fscrypt_auth_len = pci->fscrypt_auth_len;
} else {
doutc(cl, "Failed to alloc snapdir fscrypt_auth\n");
ret = -ENOMEM;
goto err;
}
}
#endif
if (inode_state_read_once(inode) & I_NEW) {
inode->i_op = &ceph_snapdir_iops;
inode->i_fop = &ceph_snapdir_fops;
ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */
unlock_new_inode(inode);
}
return inode;
err:
if ((inode_state_read_once(inode) & I_NEW))
discard_new_inode(inode);
else
iput(inode);
return ERR_PTR(ret);
}
const struct inode_operations ceph_file_iops = {
.permission = ceph_permission,
.setattr = ceph_setattr,
.getattr = ceph_getattr,
.listxattr = ceph_listxattr,
.get_inode_acl = ceph_get_acl,
.set_acl = ceph_set_acl,
};
/*
* We use a 'frag tree' to keep track of the MDS's directory fragments
* for a given inode (usually there is just a single fragment). We
* need to know when a child frag is delegated to a new MDS, or when
* it is flagged as replicated, so we can direct our requests
* accordingly.
*/
/*
* find/create a frag in the tree
*/
static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci,
u32 f)
{
struct inode *inode = &ci->netfs.inode;
struct ceph_client *cl = ceph_inode_to_client(inode);
struct rb_node **p;
struct rb_node *parent = NULL;
struct ceph_inode_frag *frag;
int c;
p = &ci->i_fragtree.rb_node;
while (*p) {
parent = *p;
frag = rb_entry(parent, struct ceph_inode_frag, node);
c = ceph_frag_compare(f, frag->frag);
if (c < 0)
p = &(*p)->rb_left;
else if (c > 0)
p = &(*p)->rb_right;
else
return frag;
}
frag = kmalloc_obj(*frag, GFP_NOFS);
if (!frag)
return ERR_PTR(-ENOMEM);
frag->frag = f;
frag->split_by = 0;
frag->mds = -1;
frag->ndist = 0;
rb_link_node(&frag->node, parent, p);
rb_insert_color(&frag->node, &ci->i_fragtree);
doutc(cl, "added %p %llx.%llx frag %x\n", inode, ceph_vinop(inode), f);
return frag;
}
/*
* find a specific frag @f
*/
struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f)
{
struct rb_node *n = ci->i_fragtree.rb_node;
while (n) {
struct ceph_inode_frag *frag =
rb_entry(n, struct ceph_inode_frag, node);
int c = ceph_frag_compare(f, frag->frag);
if (c < 0)
n = n->rb_left;
else if (c > 0)
n = n->rb_right;
else
return frag;
}
return NULL;
}
/*
* Choose frag containing the given value @v. If @pfrag is
* specified, copy the frag delegation info to the caller if
* it is present.
*/
static u32 __ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
struct ceph_inode_frag *pfrag, int *found)
{
struct ceph_client *cl = ceph_inode_to_client(&ci->netfs.inode);
u32 t = ceph_frag_make(0, 0);
struct ceph_inode_frag *frag;
unsigned nway, i;
u32 n;
if (found)
*found = 0;
while (1) {
WARN_ON(!ceph_frag_contains_value(t, v));
frag = __ceph_find_frag(ci, t);
if (!frag)
break; /* t is a leaf */
if (frag->split_by == 0) {
if (pfrag)
memcpy(pfrag, frag, sizeof(*pfrag));
if (found)
*found = 1;
break;
}
/* choose child */
nway = 1 << frag->split_by;
doutc(cl, "frag(%x) %x splits by %d (%d ways)\n", v, t,
frag->split_by, nway);
for (i = 0; i < nway; i++) {
n = ceph_frag_make_child(t, frag->split_by, i);
if (ceph_frag_contains_value(n, v)) {
t = n;
break;
}
}
BUG_ON(i == nway);
}
doutc(cl, "frag(%x) = %x\n", v, t);
return t;
}
u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
struct ceph_inode_frag *pfrag, int *found)
{
u32 ret;
mutex_lock(&ci->i_fragtree_mutex);
ret = __ceph_choose_frag(ci, v, pfrag, found);
mutex_unlock(&ci->i_fragtree_mutex);
return ret;
}
/*
* Process dirfrag (delegation) info from the mds. Include leaf
* fragment in tree ONLY if ndist > 0. Otherwise, only
* branches/splits are included in i_fragtree)
*/
static int ceph_fill_dirfrag(struct inode *inode,
struct ceph_mds_reply_dirfrag *dirinfo)
{
struct ceph_inode_info *ci = ceph_inode(inode);
struct ceph_client *cl = ceph_inode_to_client(inode);
struct ceph_inode_frag *frag;
u32 id = le32_to_cpu(dirinfo->frag);
int mds = le32_to_cpu(dirinfo->auth);
int ndist = le32_to_cpu(dirinfo->ndist);
int diri_auth = -1;
int i;
int err = 0;
spin_lock(&ci->i_ceph_lock);
if (ci->i_auth_cap)
diri_auth = ci->i_auth_cap->mds;
spin_unlock(&ci->i_ceph_lock);
if (mds == -1) /* CDIR_AUTH_PARENT */
mds = diri_auth;
mutex_lock(&ci->i_fragtree_mutex);
if (ndist == 0 && mds == diri_auth) {
/* no delegation info needed. */
frag = __ceph_find_frag(ci, id);
if (!frag)
goto out;
if (frag->split_by == 0) {
/* tree leaf, remove */
doutc(cl, "removed %p %llx.%llx frag %x (no ref)\n",
inode, ceph_vinop(inode), id);
rb_erase(&frag->node, &ci->i_fragtree);
kfree(frag);
} else {
/* tree branch, keep and clear */
doutc(cl, "cleared %p %llx.%llx frag %x referral\n",
inode, ceph_vinop(inode), id);
frag->mds = -1;
frag->ndist = 0;
}
goto out;
}
/* find/add this frag to store mds delegation info */
frag = __get_or_create_frag(ci, id);
if (IS_ERR(frag)) {
/* this is not the end of the world; we can continue
with bad/inaccurate delegation info */
pr_err_client(cl, "ENOMEM on mds ref %p %llx.%llx fg %x\n",
inode, ceph_vinop(inode),
le32_to_cpu(dirinfo->frag));
err = -ENOMEM;
goto out;
}
frag->mds = mds;
frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP);
for (i = 0; i < frag->ndist; i++)
frag->dist[i] = le32_to_cpu(dirinfo->dist[i]);
doutc(cl, "%p %llx.%llx frag %x ndist=%d\n", inode,
ceph_vinop(inode), frag->frag, frag->ndist);
out:
mutex_unlock(&ci->i_fragtree_mutex);
return err;
}
static int frag_tree_split_cmp(const void *l, const void *r)
{
struct ceph_frag_tree_split *ls = (struct ceph_frag_tree_split*)l;
struct ceph_frag_tree_split *rs = (struct ceph_frag_tree_split*)r;
return ceph_frag_compare(le32_to_cpu(ls->frag),
le32_to_cpu(rs->frag));
}
static bool is_frag_child(u32 f, struct ceph_inode_frag *frag)
{
if (!frag)
return f == ceph_frag_make(0, 0);
if (ceph_frag_bits(f) != ceph_frag_bits(frag->frag) + frag->split_by)
return false;
return ceph_frag_contains_value(frag->frag, ceph_frag_value(f));
}
static int ceph_fill_fragtree(struct inode *inode,
struct ceph_frag_tree_head *fragtree,
struct ceph_mds_reply_dirfrag *dirinfo)
{
struct ceph_client *cl = ceph_inode_to_client(inode);
struct ceph_inode_info *ci = ceph_inode(inode);
struct ceph_inode_frag *frag, *prev_frag = NULL;
struct rb_node *rb_node;
unsigned i, split_by, nsplits;
u32 id;
bool update = false;
mutex_lock(&ci->i_fragtree_mutex);
nsplits = le32_to_cpu(fragtree->nsplits);
if (nsplits != ci->i_fragtree_nsplits) {
update = true;
} else if (nsplits) {
i = get_random_u32_below(nsplits);
id = le32_to_cpu(fragtree->splits[i].frag);
if (!__ceph_find_frag(ci, id))
update = true;
} else if (!RB_EMPTY_ROOT(&ci->i_fragtree)) {
rb_node = rb_first(&ci->i_fragtree);
frag = rb_entry(rb_node, struct ceph_inode_frag, node);
if (frag->frag != ceph_frag_make(0, 0) || rb_next(rb_node))
update = true;
}
if (!update && dirinfo) {
id = le32_to_cpu(dirinfo->frag);
if (id != __ceph_choose_frag(ci, id, NULL, NULL))
update = true;
}
if (!update)
goto out_unlock;
if (nsplits > 1) {
sort(fragtree->splits, nsplits, sizeof(fragtree->splits[0]),
frag_tree_split_cmp, NULL);
}
doutc(cl, "%p %llx.%llx\n", inode, ceph_vinop(inode));
rb_node = rb_first(&ci->i_fragtree);
for (i = 0; i < nsplits; i++) {
id = le32_to_cpu(fragtree->splits[i].frag);
split_by = le32_to_cpu(fragtree->splits[i].by);
if (split_by == 0 || ceph_frag_bits(id) + split_by > 24) {
pr_err_client(cl, "%p %llx.%llx invalid split %d/%u, "
"frag %x split by %d\n", inode,
ceph_vinop(inode), i, nsplits, id, split_by);
continue;
}
frag = NULL;
while (rb_node) {
frag = rb_entry(rb_node, struct ceph_inode_frag, node);
if (ceph_frag_compare(frag->frag, id) >= 0) {
if (frag->frag != id)
frag = NULL;
else
rb_node = rb_next(rb_node);
break;
}
rb_node = rb_next(rb_node);
/* delete stale split/leaf node */
if (frag->split_by > 0 ||
!is_frag_child(frag->frag, prev_frag)) {
rb_erase(&frag->node, &ci->i_fragtree);
if (frag->split_by > 0)
ci->i_fragtree_nsplits--;
kfree(frag);
}
frag = NULL;
}
if (!frag) {
frag = __get_or_create_frag(ci, id);
if (IS_ERR(frag))
continue;
}
if (frag->split_by == 0)
ci->i_fragtree_nsplits++;
frag->split_by = split_by;
doutc(cl, " frag %x split by %d\n", frag->frag, frag->split_by);
prev_frag = frag;
}
while (rb_node) {
frag = rb_entry(rb_node, struct ceph_inode_frag, node);
rb_node = rb_next(rb_node);
/* delete stale split/leaf node */
if (frag->split_by > 0 ||
!is_frag_child(frag->frag, prev_frag)) {
rb_erase(&frag->node, &ci->i_fragtree);
if (frag->split_by > 0)
ci->i_fragtree_nsplits--;
kfree(frag);
}
}
out_unlock:
mutex_unlock(&ci->i_fragtree_mutex);
return 0;
}
/*
* initialize a newly allocated inode.
*/
struct inode *ceph_alloc_inode(struct super_block *sb)
{
struct ceph_fs_client *fsc = ceph_sb_to_fs_client(sb);
struct ceph_inode_info *ci;
int i;
ci = alloc_inode_sb(sb, ceph_inode_cachep, GFP_NOFS);
if (!ci)
return NULL;
doutc(fsc->client, "%p\n", &ci->netfs.inode);
/* Set parameters for the netfs library */
netfs_inode_init(&ci->netfs, &ceph_netfs_ops, false);
spin_lock_init(&ci->i_ceph_lock);
ci->i_version = 0;
ceph_set_inline_version(ci, 0);
ci->i_time_warp_seq = 0;
ci->i_ceph_flags = 0;
atomic64_set(&ci->i_ordered_count, 1);
atomic64_set(&ci->i_release_count, 1);
atomic64_set(&ci->i_complete_seq[0], 0);
atomic64_set(&ci->i_complete_seq[1], 0);
ci->i_symlink = NULL;
ci->i_max_bytes = 0;
ci->i_max_files = 0;
ci->i_subvolume_id = CEPH_SUBVOLUME_ID_NONE;
memset(&ci->i_dir_layout, 0, sizeof(ci->i_dir_layout));
memset(&ci->i_cached_layout, 0, sizeof(ci->i_cached_layout));
RCU_INIT_POINTER(ci->i_layout.pool_ns, NULL);
ci->i_fragtree = RB_ROOT;
mutex_init(&ci->i_fragtree_mutex);
ci->i_xattrs.blob = NULL;
ci->i_xattrs.prealloc_blob = NULL;
ci->i_xattrs.dirty = false;
ci->i_xattrs.index = RB_ROOT;
ci->i_xattrs.count = 0;
ci->i_xattrs.names_size = 0;
ci->i_xattrs.vals_size = 0;
ci->i_xattrs.version = 0;
ci->i_xattrs.index_version = 0;
ci->i_caps = RB_ROOT;
ci->i_auth_cap = NULL;
ci->i_dirty_caps = 0;
ci->i_flushing_caps = 0;
INIT_LIST_HEAD(&ci->i_dirty_item);
INIT_LIST_HEAD(&ci->i_flushing_item);
ci->i_prealloc_cap_flush = NULL;
INIT_LIST_HEAD(&ci->i_cap_flush_list);
init_waitqueue_head(&ci->i_cap_wq);
ci->i_hold_caps_max = 0;
INIT_LIST_HEAD(&ci->i_cap_delay_list);
INIT_LIST_HEAD(&ci->i_cap_snaps);
ci->i_head_snapc = NULL;
ci->i_snap_caps = 0;
ci->i_last_cap_flush_ack = 0;
ci->i_last_rd = ci->i_last_wr = jiffies - 3600 * HZ;
for (i = 0; i < CEPH_FILE_MODE_BITS; i++)
ci->i_nr_by_mode[i] = 0;
mutex_init(&ci->i_truncate_mutex);
ci->i_truncate_seq = 0;
ci->i_truncate_size = 0;
ci->i_truncate_pending = false;
ci->i_truncate_shrank = false;
ci->i_truncate_pagecache_size = 0;
ci->i_max_size = 0;
ci->i_reported_size = 0;
ci->i_wanted_max_size = 0;
ci->i_requested_max_size = 0;
ci->i_pin_ref = 0;
ci->i_rd_ref = 0;
ci->i_rdcache_ref = 0;
ci->i_wr_ref = 0;
ci->i_wb_ref = 0;
ci->i_fx_ref = 0;
ci->i_wrbuffer_ref = 0;
ci->i_wrbuffer_ref_head = 0;
atomic_set(&ci->i_filelock_ref, 0);
atomic_set(&ci->i_shared_gen, 1);
ci->i_rdcache_gen = 0;
ci->i_rdcache_revoking = 0;
INIT_LIST_HEAD(&ci->i_unsafe_dirops);
INIT_LIST_HEAD(&ci->i_unsafe_iops);
spin_lock_init(&ci->i_unsafe_lock);
ci->i_snap_realm = NULL;
INIT_LIST_HEAD(&ci->i_snap_realm_item);
INIT_LIST_HEAD(&ci->i_snap_flush_item);
INIT_WORK(&ci->i_work, ceph_inode_work);
ci->i_work_mask = 0;
memset(&ci->i_btime, '\0', sizeof(ci->i_btime));
#ifdef CONFIG_FS_ENCRYPTION
ci->i_crypt_info = NULL;
ci->fscrypt_auth = NULL;
ci->fscrypt_auth_len = 0;
#endif
return &ci->netfs.inode;
}
void ceph_free_inode(struct inode *inode)
{
struct ceph_inode_info *ci = ceph_inode(inode);
kfree(ci->i_symlink);
#ifdef CONFIG_FS_ENCRYPTION
kfree(ci->fscrypt_auth);
#endif
fscrypt_free_inode(inode);
kmem_cache_free(ceph_inode_cachep, ci);
}
void ceph_evict_inode(struct inode *inode)
{
struct ceph_inode_info *ci = ceph_inode(inode);
struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
struct ceph_client *cl = ceph_inode_to_client(inode);
struct ceph_inode_frag *frag;
struct rb_node *n;
doutc(cl, "%p ino %llx.%llx\n", inode, ceph_vinop(inode));
percpu_counter_dec(&mdsc->metric.total_inodes);
ci->i_subvolume_id = CEPH_SUBVOLUME_ID_NONE;
netfs_wait_for_outstanding_io(inode);
truncate_inode_pages_final(&inode->i_data);
if (inode_state_read_once(inode) & I_PINNING_NETFS_WB)
ceph_fscache_unuse_cookie(inode, true);
clear_inode(inode);
ceph_fscache_unregister_inode_cookie(ci);
fscrypt_put_encryption_info(inode);
__ceph_remove_caps(ci);
if (__ceph_has_quota(ci, QUOTA_GET_ANY))
ceph_adjust_quota_realms_count(inode, false);
/*
* we may still have a snap_realm reference if there are stray
* caps in i_snap_caps.
*/
if (ci->i_snap_realm) {
if (!ceph_in_snap(inode)) {
doutc(cl, " dropping residual ref to snap realm %p\n",
ci->i_snap_realm);
ceph_change_snap_realm(inode, NULL);
} else {
ceph_put_snapid_map(mdsc, ci->i_snapid_map);
ci->i_snap_realm = NULL;
}
}
while ((n = rb_first(&ci->i_fragtree)) != NULL) {
frag = rb_entry(n, struct ceph_inode_frag, node);
rb_erase(n, &ci->i_fragtree);
kfree(frag);
}
ci->i_fragtree_nsplits = 0;
__ceph_destroy_xattrs(ci);
if (ci->i_xattrs.blob)
ceph_buffer_put(ci->i_xattrs.blob);
if (ci->i_xattrs.prealloc_blob)
ceph_buffer_put(ci->i_xattrs.prealloc_blob);
ceph_put_string(rcu_dereference_raw(ci->i_layout.pool_ns));
ceph_put_string(rcu_dereference_raw(ci->i_cached_layout.pool_ns));
}
/*
* Decide whether to keep an inode once its last reference is gone.
*
* Dropping it here runs ceph_evict_inode(), which throws away the page
* cache along with the caps that still vouch for it. Keep a regular
* file's inode while it has cached pages and still holds real caps, so
* that a close and reopen can serve reads from the page cache instead
* of re-reading everything from the OSDs.
*
* Such an inode is not pinned: it holds no reference of its own, and
* page reclaim frees its pages like any other file's. It is not on the
* inode LRU either, since mapping_shrinkable() is false while real
* folios are present; emptying the mapping is what puts it there for
* the inode shrinker. Unmount evicts it regardless, since
* evict_inodes() walks sb->s_inodes rather than the LRU.
*
* The retention is bypassed whenever fscrypt requires the inode to be
* evicted, e.g. after its encryption key has been removed: the page
* cache holds plaintext, so it has to go with the key.
* FS_IOC_REMOVE_ENCRYPTION_KEY relies on that, as it evicts the
* dentries and then expects the inodes to follow. fscrypt is
* therefore asked before any of the retention below, and its answer is
* never overridden by it.
*
* Handing caps back to the MDS used to fall out of this eviction, so
* trim_caps_cb() sets CEPH_I_EVICT_ON_FINAL_IPUT_BIT to override the
* retention for one put when the MDS asks for its caps back.
*
* ->drop_inode() is called with inode->i_lock held, so i_ceph_lock
* cannot be taken here and i_caps is tested racily. That is good
* enough: either answer only keeps or drops an inode that could have
* gone the other way.
*/
int ceph_drop_inode(struct inode *inode)
{
struct ceph_inode_info *ci = ceph_inode(inode);
/* the MDS asked for this one back */
if (test_and_clear_bit(CEPH_I_EVICT_ON_FINAL_IPUT_BIT,
&ci->i_ceph_flags))
return 1;
if (inode_generic_drop(inode))
return 1;
if (fscrypt_drop_inode(inode))
return 1;
if (ceph_inode_is_shutdown(inode))
return 1;
/* nothing here worth keeping the inode for */
if (!S_ISREG(inode->i_mode) || !inode->i_data.nrpages)
return 1;
/* keep the pages only while the inode still holds real caps */
return !__ceph_is_any_real_caps(ci);
}
static inline blkcnt_t calc_inode_blocks(u64 size)
{
return (size + (1<<9) - 1) >> 9;
}
/*
* Helpers to fill in size, ctime, mtime, and atime. We have to be
* careful because either the client or MDS may have more up to date
* info, depending on which capabilities are held, and whether
* time_warp_seq or truncate_seq have increased. (Ordinarily, mtime
* and size are monotonically increasing, except when utimes() or
* truncate() increments the corresponding _seq values.)
*/
int ceph_fill_file_size(struct inode *inode, int issued,
u32 truncate_seq, u64 truncate_size, u64 size)
{
struct ceph_client *cl = ceph_inode_to_client(inode);
struct ceph_inode_info *ci = ceph_inode(inode);
int queue_trunc = 0;
loff_t isize = i_size_read(inode);
if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 ||
(truncate_seq == ci->i_truncate_seq && size > isize)) {
doutc(cl, "size %lld -> %llu\n", isize, size);
if (size > 0 && S_ISDIR(inode->i_mode)) {
pr_err_client(cl, "non-zero size for directory\n");
size = 0;
}
i_size_write(inode, size);
inode->i_blocks = calc_inode_blocks(size);
/*
* If we're expanding, then we should be able to just update
* the existing cookie.
*/
if (size > isize)
ceph_fscache_update(inode);
ci->i_reported_size = size;
if (truncate_seq != ci->i_truncate_seq) {
doutc(cl, "truncate_seq %u -> %u\n",
ci->i_truncate_seq, truncate_seq);
ci->i_truncate_seq = truncate_seq;
/* the MDS should have revoked these caps */
WARN_ON_ONCE(issued & (CEPH_CAP_FILE_RD |
CEPH_CAP_FILE_LAZYIO));
/*
* If we hold relevant caps, or in the case where we're
* not the only client referencing this file and we
* don't hold those caps, then we need to check whether
* the file is either opened or mmaped
*/
if ((issued & (CEPH_CAP_FILE_CACHE|
CEPH_CAP_FILE_BUFFER)) ||
mapping_mapped(inode->i_mapping) ||
__ceph_is_file_opened(ci)) {
ci->i_truncate_pending = true;
if (size < isize)
ci->i_truncate_shrank = true;
queue_trunc = 1;
}
}
}
/*
* It's possible that the new sizes of the two consecutive
* size truncations will be in the same fscrypt last block,
* and we need to truncate the corresponding page caches
* anyway.
*/
if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0) {
doutc(cl, "truncate_size %lld -> %llu, encrypted %d\n",
ci->i_truncate_size, truncate_size,
!!IS_ENCRYPTED(inode));
ci->i_truncate_size = truncate_size;
if (IS_ENCRYPTED(inode)) {
doutc(cl, "truncate_pagecache_size %lld -> %llu\n",
ci->i_truncate_pagecache_size, size);
ci->i_truncate_pagecache_size = size;
} else {
ci->i_truncate_pagecache_size = truncate_size;
}
}
return queue_trunc;
}
/*
* Set the subvolume ID for an inode.
*
* The subvolume_id identifies which CephFS subvolume this inode belongs to.
* CEPH_SUBVOLUME_ID_NONE (0) means unknown/unset - the MDS only sends
* non-zero IDs for inodes within subvolumes.
*
* An inode's subvolume membership is immutable - once an inode is created
* in a subvolume, it stays there. Therefore, if we already have a valid
* (non-zero) subvolume_id and receive a different one, that indicates a bug.
*/
void ceph_inode_set_subvolume(struct inode *inode, u64 subvolume_id)
{
struct ceph_inode_info *ci;
u64 old;
if (!inode || subvolume_id == CEPH_SUBVOLUME_ID_NONE)
return;
ci = ceph_inode(inode);
old = READ_ONCE(ci->i_subvolume_id);
if (old == subvolume_id)
return;
if (old != CEPH_SUBVOLUME_ID_NONE) {
/* subvolume_id should not change once set */
WARN_ON_ONCE(1);
return;
}
WRITE_ONCE(ci->i_subvolume_id, subvolume_id);
}
void ceph_fill_file_time(struct inode *inode, int issued,
u32 time_warp_seq, struct timespec64 *ctime,
struct timespec64 *mtime, struct timespec64 *atime)
{
struct ceph_client *cl = ceph_inode_to_client(inode);
struct ceph_inode_info *ci = ceph_inode(inode);
struct timespec64 iatime = inode_get_atime(inode);
struct timespec64 ictime = inode_get_ctime(inode);
struct timespec64 imtime = inode_get_mtime(inode);
int warn = 0;
if (issued & (CEPH_CAP_FILE_EXCL|
CEPH_CAP_FILE_WR|
CEPH_CAP_FILE_BUFFER|
CEPH_CAP_AUTH_EXCL|
CEPH_CAP_XATTR_EXCL)) {