super.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _FS_CEPH_SUPER_H
  3. #define _FS_CEPH_SUPER_H
  4. #include <linux/ceph/ceph_debug.h>
  5. #include <asm/unaligned.h>
  6. #include <linux/backing-dev.h>
  7. #include <linux/completion.h>
  8. #include <linux/exportfs.h>
  9. #include <linux/fs.h>
  10. #include <linux/mempool.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/wait.h>
  13. #include <linux/writeback.h>
  14. #include <linux/slab.h>
  15. #include <linux/posix_acl.h>
  16. #include <linux/refcount.h>
  17. #include <linux/ceph/libceph.h>
  18. #ifdef CONFIG_CEPH_FSCACHE
  19. #include <linux/fscache.h>
  20. #endif
  21. /* f_type in struct statfs */
  22. #define CEPH_SUPER_MAGIC 0x00c36400
  23. /* large granularity for statfs utilization stats to facilitate
  24. * large volume sizes on 32-bit machines. */
  25. #define CEPH_BLOCK_SHIFT 22 /* 4 MB */
  26. #define CEPH_BLOCK (1 << CEPH_BLOCK_SHIFT)
  27. #define CEPH_MOUNT_OPT_DIRSTAT (1<<4) /* `cat dirname` for stats */
  28. #define CEPH_MOUNT_OPT_RBYTES (1<<5) /* dir st_bytes = rbytes */
  29. #define CEPH_MOUNT_OPT_NOASYNCREADDIR (1<<7) /* no dcache readdir */
  30. #define CEPH_MOUNT_OPT_INO32 (1<<8) /* 32 bit inos */
  31. #define CEPH_MOUNT_OPT_DCACHE (1<<9) /* use dcache for readdir etc */
  32. #define CEPH_MOUNT_OPT_FSCACHE (1<<10) /* use fscache */
  33. #define CEPH_MOUNT_OPT_NOPOOLPERM (1<<11) /* no pool permission check */
  34. #define CEPH_MOUNT_OPT_MOUNTWAIT (1<<12) /* mount waits if no mds is up */
  35. #define CEPH_MOUNT_OPT_NOQUOTADF (1<<13) /* no root dir quota in statfs */
  36. #define CEPH_MOUNT_OPT_DEFAULT CEPH_MOUNT_OPT_DCACHE
  37. #define ceph_set_mount_opt(fsc, opt) \
  38. (fsc)->mount_options->flags |= CEPH_MOUNT_OPT_##opt;
  39. #define ceph_test_mount_opt(fsc, opt) \
  40. (!!((fsc)->mount_options->flags & CEPH_MOUNT_OPT_##opt))
  41. /* max size of osd read request, limited by libceph */
  42. #define CEPH_MAX_READ_SIZE CEPH_MSG_MAX_DATA_LEN
  43. /* osd has a configurable limitaion of max write size.
  44. * CEPH_MSG_MAX_DATA_LEN should be small enough. */
  45. #define CEPH_MAX_WRITE_SIZE CEPH_MSG_MAX_DATA_LEN
  46. #define CEPH_RASIZE_DEFAULT (8192*1024) /* max readahead */
  47. #define CEPH_MAX_READDIR_DEFAULT 1024
  48. #define CEPH_MAX_READDIR_BYTES_DEFAULT (512*1024)
  49. #define CEPH_SNAPDIRNAME_DEFAULT ".snap"
  50. /*
  51. * Delay telling the MDS we no longer want caps, in case we reopen
  52. * the file. Delay a minimum amount of time, even if we send a cap
  53. * message for some other reason. Otherwise, take the oppotunity to
  54. * update the mds to avoid sending another message later.
  55. */
  56. #define CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT 5 /* cap release delay */
  57. #define CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT 60 /* cap release delay */
  58. struct ceph_mount_options {
  59. int flags;
  60. int sb_flags;
  61. int wsize; /* max write size */
  62. int rsize; /* max read size */
  63. int rasize; /* max readahead */
  64. int congestion_kb; /* max writeback in flight */
  65. int caps_wanted_delay_min, caps_wanted_delay_max;
  66. int max_readdir; /* max readdir result (entires) */
  67. int max_readdir_bytes; /* max readdir result (bytes) */
  68. /*
  69. * everything above this point can be memcmp'd; everything below
  70. * is handled in compare_mount_options()
  71. */
  72. char *snapdir_name; /* default ".snap" */
  73. char *mds_namespace; /* default NULL */
  74. char *server_path; /* default NULL (means "/") */
  75. char *fscache_uniq; /* default NULL */
  76. };
  77. struct ceph_fs_client {
  78. struct super_block *sb;
  79. struct ceph_mount_options *mount_options;
  80. struct ceph_client *client;
  81. unsigned long mount_state;
  82. int min_caps; /* min caps i added */
  83. loff_t max_file_size;
  84. struct ceph_mds_client *mdsc;
  85. /* writeback */
  86. mempool_t *wb_pagevec_pool;
  87. struct workqueue_struct *wb_wq;
  88. struct workqueue_struct *pg_inv_wq;
  89. struct workqueue_struct *trunc_wq;
  90. atomic_long_t writeback_count;
  91. #ifdef CONFIG_DEBUG_FS
  92. struct dentry *debugfs_dentry_lru, *debugfs_caps;
  93. struct dentry *debugfs_congestion_kb;
  94. struct dentry *debugfs_bdi;
  95. struct dentry *debugfs_mdsc, *debugfs_mdsmap;
  96. struct dentry *debugfs_mds_sessions;
  97. #endif
  98. #ifdef CONFIG_CEPH_FSCACHE
  99. struct fscache_cookie *fscache;
  100. #endif
  101. };
  102. /*
  103. * File i/o capability. This tracks shared state with the metadata
  104. * server that allows us to cache or writeback attributes or to read
  105. * and write data. For any given inode, we should have one or more
  106. * capabilities, one issued by each metadata server, and our
  107. * cumulative access is the OR of all issued capabilities.
  108. *
  109. * Each cap is referenced by the inode's i_caps rbtree and by per-mds
  110. * session capability lists.
  111. */
  112. struct ceph_cap {
  113. struct ceph_inode_info *ci;
  114. struct rb_node ci_node; /* per-ci cap tree */
  115. struct ceph_mds_session *session;
  116. struct list_head session_caps; /* per-session caplist */
  117. u64 cap_id; /* unique cap id (mds provided) */
  118. union {
  119. /* in-use caps */
  120. struct {
  121. int issued; /* latest, from the mds */
  122. int implemented; /* implemented superset of
  123. issued (for revocation) */
  124. int mds, mds_wanted;
  125. };
  126. /* caps to release */
  127. struct {
  128. u64 cap_ino;
  129. int queue_release;
  130. };
  131. };
  132. u32 seq, issue_seq, mseq;
  133. u32 cap_gen; /* active/stale cycle */
  134. unsigned long last_used;
  135. struct list_head caps_item;
  136. };
  137. #define CHECK_CAPS_NODELAY 1 /* do not delay any further */
  138. #define CHECK_CAPS_AUTHONLY 2 /* only check auth cap */
  139. #define CHECK_CAPS_FLUSH 4 /* flush any dirty caps */
  140. struct ceph_cap_flush {
  141. u64 tid;
  142. int caps; /* 0 means capsnap */
  143. bool wake; /* wake up flush waiters when finish ? */
  144. struct list_head g_list; // global
  145. struct list_head i_list; // per inode
  146. };
  147. /*
  148. * Snapped cap state that is pending flush to mds. When a snapshot occurs,
  149. * we first complete any in-process sync writes and writeback any dirty
  150. * data before flushing the snapped state (tracked here) back to the MDS.
  151. */
  152. struct ceph_cap_snap {
  153. refcount_t nref;
  154. struct list_head ci_item;
  155. struct ceph_cap_flush cap_flush;
  156. u64 follows;
  157. int issued, dirty;
  158. struct ceph_snap_context *context;
  159. umode_t mode;
  160. kuid_t uid;
  161. kgid_t gid;
  162. struct ceph_buffer *xattr_blob;
  163. u64 xattr_version;
  164. u64 size;
  165. struct timespec64 mtime, atime, ctime;
  166. u64 time_warp_seq;
  167. u64 truncate_size;
  168. u32 truncate_seq;
  169. int writing; /* a sync write is still in progress */
  170. int dirty_pages; /* dirty pages awaiting writeback */
  171. bool inline_data;
  172. bool need_flush;
  173. };
  174. static inline void ceph_put_cap_snap(struct ceph_cap_snap *capsnap)
  175. {
  176. if (refcount_dec_and_test(&capsnap->nref)) {
  177. if (capsnap->xattr_blob)
  178. ceph_buffer_put(capsnap->xattr_blob);
  179. kfree(capsnap);
  180. }
  181. }
  182. /*
  183. * The frag tree describes how a directory is fragmented, potentially across
  184. * multiple metadata servers. It is also used to indicate points where
  185. * metadata authority is delegated, and whether/where metadata is replicated.
  186. *
  187. * A _leaf_ frag will be present in the i_fragtree IFF there is
  188. * delegation info. That is, if mds >= 0 || ndist > 0.
  189. */
  190. #define CEPH_MAX_DIRFRAG_REP 4
  191. struct ceph_inode_frag {
  192. struct rb_node node;
  193. /* fragtree state */
  194. u32 frag;
  195. int split_by; /* i.e. 2^(split_by) children */
  196. /* delegation and replication info */
  197. int mds; /* -1 if same authority as parent */
  198. int ndist; /* >0 if replicated */
  199. int dist[CEPH_MAX_DIRFRAG_REP];
  200. };
  201. /*
  202. * We cache inode xattrs as an encoded blob until they are first used,
  203. * at which point we parse them into an rbtree.
  204. */
  205. struct ceph_inode_xattr {
  206. struct rb_node node;
  207. const char *name;
  208. int name_len;
  209. const char *val;
  210. int val_len;
  211. int dirty;
  212. int should_free_name;
  213. int should_free_val;
  214. };
  215. /*
  216. * Ceph dentry state
  217. */
  218. struct ceph_dentry_info {
  219. struct ceph_mds_session *lease_session;
  220. int lease_shared_gen;
  221. u32 lease_gen;
  222. u32 lease_seq;
  223. unsigned long lease_renew_after, lease_renew_from;
  224. struct list_head lru;
  225. struct dentry *dentry;
  226. unsigned long time;
  227. u64 offset;
  228. };
  229. struct ceph_inode_xattrs_info {
  230. /*
  231. * (still encoded) xattr blob. we avoid the overhead of parsing
  232. * this until someone actually calls getxattr, etc.
  233. *
  234. * blob->vec.iov_len == 4 implies there are no xattrs; blob ==
  235. * NULL means we don't know.
  236. */
  237. struct ceph_buffer *blob, *prealloc_blob;
  238. struct rb_root index;
  239. bool dirty;
  240. int count;
  241. int names_size;
  242. int vals_size;
  243. u64 version, index_version;
  244. };
  245. /*
  246. * Ceph inode.
  247. */
  248. struct ceph_inode_info {
  249. struct ceph_vino i_vino; /* ceph ino + snap */
  250. spinlock_t i_ceph_lock;
  251. u64 i_version;
  252. u64 i_inline_version;
  253. u32 i_time_warp_seq;
  254. unsigned i_ceph_flags;
  255. atomic64_t i_release_count;
  256. atomic64_t i_ordered_count;
  257. atomic64_t i_complete_seq[2];
  258. struct ceph_dir_layout i_dir_layout;
  259. struct ceph_file_layout i_layout;
  260. char *i_symlink;
  261. /* for dirs */
  262. struct timespec64 i_rctime;
  263. u64 i_rbytes, i_rfiles, i_rsubdirs;
  264. u64 i_files, i_subdirs;
  265. /* quotas */
  266. u64 i_max_bytes, i_max_files;
  267. struct rb_root i_fragtree;
  268. int i_fragtree_nsplits;
  269. struct mutex i_fragtree_mutex;
  270. struct ceph_inode_xattrs_info i_xattrs;
  271. /* capabilities. protected _both_ by i_ceph_lock and cap->session's
  272. * s_mutex. */
  273. struct rb_root i_caps; /* cap list */
  274. struct ceph_cap *i_auth_cap; /* authoritative cap, if any */
  275. unsigned i_dirty_caps, i_flushing_caps; /* mask of dirtied fields */
  276. struct list_head i_dirty_item, i_flushing_item;
  277. /* we need to track cap writeback on a per-cap-bit basis, to allow
  278. * overlapping, pipelined cap flushes to the mds. we can probably
  279. * reduce the tid to 8 bits if we're concerned about inode size. */
  280. struct ceph_cap_flush *i_prealloc_cap_flush;
  281. struct list_head i_cap_flush_list;
  282. wait_queue_head_t i_cap_wq; /* threads waiting on a capability */
  283. unsigned long i_hold_caps_min; /* jiffies */
  284. unsigned long i_hold_caps_max; /* jiffies */
  285. struct list_head i_cap_delay_list; /* for delayed cap release to mds */
  286. struct ceph_cap_reservation i_cap_migration_resv;
  287. struct list_head i_cap_snaps; /* snapped state pending flush to mds */
  288. struct ceph_snap_context *i_head_snapc; /* set if wr_buffer_head > 0 or
  289. dirty|flushing caps */
  290. unsigned i_snap_caps; /* cap bits for snapped files */
  291. int i_nr_by_mode[CEPH_FILE_MODE_BITS]; /* open file counts */
  292. struct mutex i_truncate_mutex;
  293. u32 i_truncate_seq; /* last truncate to smaller size */
  294. u64 i_truncate_size; /* and the size we last truncated down to */
  295. int i_truncate_pending; /* still need to call vmtruncate */
  296. u64 i_max_size; /* max file size authorized by mds */
  297. u64 i_reported_size; /* (max_)size reported to or requested of mds */
  298. u64 i_wanted_max_size; /* offset we'd like to write too */
  299. u64 i_requested_max_size; /* max_size we've requested */
  300. /* held references to caps */
  301. int i_pin_ref;
  302. int i_rd_ref, i_rdcache_ref, i_wr_ref, i_wb_ref;
  303. int i_wrbuffer_ref, i_wrbuffer_ref_head;
  304. atomic_t i_filelock_ref;
  305. atomic_t i_shared_gen; /* increment each time we get FILE_SHARED */
  306. u32 i_rdcache_gen; /* incremented each time we get FILE_CACHE. */
  307. u32 i_rdcache_revoking; /* RDCACHE gen to async invalidate, if any */
  308. struct list_head i_unsafe_dirops; /* uncommitted mds dir ops */
  309. struct list_head i_unsafe_iops; /* uncommitted mds inode ops */
  310. spinlock_t i_unsafe_lock;
  311. struct ceph_snap_realm *i_snap_realm; /* snap realm (if caps) */
  312. int i_snap_realm_counter; /* snap realm (if caps) */
  313. struct list_head i_snap_realm_item;
  314. struct list_head i_snap_flush_item;
  315. struct work_struct i_wb_work; /* writeback work */
  316. struct work_struct i_pg_inv_work; /* page invalidation work */
  317. struct work_struct i_vmtruncate_work;
  318. #ifdef CONFIG_CEPH_FSCACHE
  319. struct fscache_cookie *fscache;
  320. u32 i_fscache_gen;
  321. #endif
  322. struct inode vfs_inode; /* at end */
  323. };
  324. static inline struct ceph_inode_info *ceph_inode(struct inode *inode)
  325. {
  326. return container_of(inode, struct ceph_inode_info, vfs_inode);
  327. }
  328. static inline struct ceph_fs_client *ceph_inode_to_client(struct inode *inode)
  329. {
  330. return (struct ceph_fs_client *)inode->i_sb->s_fs_info;
  331. }
  332. static inline struct ceph_fs_client *ceph_sb_to_client(struct super_block *sb)
  333. {
  334. return (struct ceph_fs_client *)sb->s_fs_info;
  335. }
  336. static inline struct ceph_vino ceph_vino(struct inode *inode)
  337. {
  338. return ceph_inode(inode)->i_vino;
  339. }
  340. /*
  341. * ino_t is <64 bits on many architectures, blech.
  342. *
  343. * i_ino (kernel inode) st_ino (userspace)
  344. * i386 32 32
  345. * x86_64+ino32 64 32
  346. * x86_64 64 64
  347. */
  348. static inline u32 ceph_ino_to_ino32(__u64 vino)
  349. {
  350. u32 ino = vino & 0xffffffff;
  351. ino ^= vino >> 32;
  352. if (!ino)
  353. ino = 2;
  354. return ino;
  355. }
  356. /*
  357. * kernel i_ino value
  358. */
  359. static inline ino_t ceph_vino_to_ino(struct ceph_vino vino)
  360. {
  361. #if BITS_PER_LONG == 32
  362. return ceph_ino_to_ino32(vino.ino);
  363. #else
  364. return (ino_t)vino.ino;
  365. #endif
  366. }
  367. /*
  368. * user-visible ino (stat, filldir)
  369. */
  370. #if BITS_PER_LONG == 32
  371. static inline ino_t ceph_translate_ino(struct super_block *sb, ino_t ino)
  372. {
  373. return ino;
  374. }
  375. #else
  376. static inline ino_t ceph_translate_ino(struct super_block *sb, ino_t ino)
  377. {
  378. if (ceph_test_mount_opt(ceph_sb_to_client(sb), INO32))
  379. ino = ceph_ino_to_ino32(ino);
  380. return ino;
  381. }
  382. #endif
  383. /* for printf-style formatting */
  384. #define ceph_vinop(i) ceph_inode(i)->i_vino.ino, ceph_inode(i)->i_vino.snap
  385. static inline u64 ceph_ino(struct inode *inode)
  386. {
  387. return ceph_inode(inode)->i_vino.ino;
  388. }
  389. static inline u64 ceph_snap(struct inode *inode)
  390. {
  391. return ceph_inode(inode)->i_vino.snap;
  392. }
  393. static inline int ceph_ino_compare(struct inode *inode, void *data)
  394. {
  395. struct ceph_vino *pvino = (struct ceph_vino *)data;
  396. struct ceph_inode_info *ci = ceph_inode(inode);
  397. return ci->i_vino.ino == pvino->ino &&
  398. ci->i_vino.snap == pvino->snap;
  399. }
  400. static inline struct inode *ceph_find_inode(struct super_block *sb,
  401. struct ceph_vino vino)
  402. {
  403. ino_t t = ceph_vino_to_ino(vino);
  404. return ilookup5(sb, t, ceph_ino_compare, &vino);
  405. }
  406. /*
  407. * Ceph inode.
  408. */
  409. #define CEPH_I_DIR_ORDERED (1 << 0) /* dentries in dir are ordered */
  410. #define CEPH_I_NODELAY (1 << 1) /* do not delay cap release */
  411. #define CEPH_I_FLUSH (1 << 2) /* do not delay flush of dirty metadata */
  412. #define CEPH_I_NOFLUSH (1 << 3) /* do not flush dirty caps */
  413. #define CEPH_I_POOL_PERM (1 << 4) /* pool rd/wr bits are valid */
  414. #define CEPH_I_POOL_RD (1 << 5) /* can read from pool */
  415. #define CEPH_I_POOL_WR (1 << 6) /* can write to pool */
  416. #define CEPH_I_SEC_INITED (1 << 7) /* security initialized */
  417. #define CEPH_I_CAP_DROPPED (1 << 8) /* caps were forcibly dropped */
  418. #define CEPH_I_KICK_FLUSH (1 << 9) /* kick flushing caps */
  419. #define CEPH_I_FLUSH_SNAPS (1 << 10) /* need flush snapss */
  420. #define CEPH_I_ERROR_WRITE (1 << 11) /* have seen write errors */
  421. #define CEPH_I_ERROR_FILELOCK (1 << 12) /* have seen file lock errors */
  422. /*
  423. * We set the ERROR_WRITE bit when we start seeing write errors on an inode
  424. * and then clear it when they start succeeding. Note that we do a lockless
  425. * check first, and only take the lock if it looks like it needs to be changed.
  426. * The write submission code just takes this as a hint, so we're not too
  427. * worried if a few slip through in either direction.
  428. */
  429. static inline void ceph_set_error_write(struct ceph_inode_info *ci)
  430. {
  431. if (!(READ_ONCE(ci->i_ceph_flags) & CEPH_I_ERROR_WRITE)) {
  432. spin_lock(&ci->i_ceph_lock);
  433. ci->i_ceph_flags |= CEPH_I_ERROR_WRITE;
  434. spin_unlock(&ci->i_ceph_lock);
  435. }
  436. }
  437. static inline void ceph_clear_error_write(struct ceph_inode_info *ci)
  438. {
  439. if (READ_ONCE(ci->i_ceph_flags) & CEPH_I_ERROR_WRITE) {
  440. spin_lock(&ci->i_ceph_lock);
  441. ci->i_ceph_flags &= ~CEPH_I_ERROR_WRITE;
  442. spin_unlock(&ci->i_ceph_lock);
  443. }
  444. }
  445. static inline void __ceph_dir_set_complete(struct ceph_inode_info *ci,
  446. long long release_count,
  447. long long ordered_count)
  448. {
  449. /*
  450. * Makes sure operations that setup readdir cache (update page
  451. * cache and i_size) are strongly ordered w.r.t. the following
  452. * atomic64_set() operations.
  453. */
  454. smp_mb();
  455. atomic64_set(&ci->i_complete_seq[0], release_count);
  456. atomic64_set(&ci->i_complete_seq[1], ordered_count);
  457. }
  458. static inline void __ceph_dir_clear_complete(struct ceph_inode_info *ci)
  459. {
  460. atomic64_inc(&ci->i_release_count);
  461. }
  462. static inline void __ceph_dir_clear_ordered(struct ceph_inode_info *ci)
  463. {
  464. atomic64_inc(&ci->i_ordered_count);
  465. }
  466. static inline bool __ceph_dir_is_complete(struct ceph_inode_info *ci)
  467. {
  468. return atomic64_read(&ci->i_complete_seq[0]) ==
  469. atomic64_read(&ci->i_release_count);
  470. }
  471. static inline bool __ceph_dir_is_complete_ordered(struct ceph_inode_info *ci)
  472. {
  473. return atomic64_read(&ci->i_complete_seq[0]) ==
  474. atomic64_read(&ci->i_release_count) &&
  475. atomic64_read(&ci->i_complete_seq[1]) ==
  476. atomic64_read(&ci->i_ordered_count);
  477. }
  478. static inline void ceph_dir_clear_complete(struct inode *inode)
  479. {
  480. __ceph_dir_clear_complete(ceph_inode(inode));
  481. }
  482. static inline void ceph_dir_clear_ordered(struct inode *inode)
  483. {
  484. __ceph_dir_clear_ordered(ceph_inode(inode));
  485. }
  486. static inline bool ceph_dir_is_complete_ordered(struct inode *inode)
  487. {
  488. bool ret = __ceph_dir_is_complete_ordered(ceph_inode(inode));
  489. smp_rmb();
  490. return ret;
  491. }
  492. /* find a specific frag @f */
  493. extern struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci,
  494. u32 f);
  495. /*
  496. * choose fragment for value @v. copy frag content to pfrag, if leaf
  497. * exists
  498. */
  499. extern u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v,
  500. struct ceph_inode_frag *pfrag,
  501. int *found);
  502. static inline struct ceph_dentry_info *ceph_dentry(struct dentry *dentry)
  503. {
  504. return (struct ceph_dentry_info *)dentry->d_fsdata;
  505. }
  506. /*
  507. * caps helpers
  508. */
  509. static inline bool __ceph_is_any_real_caps(struct ceph_inode_info *ci)
  510. {
  511. return !RB_EMPTY_ROOT(&ci->i_caps);
  512. }
  513. extern int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented);
  514. extern int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int t);
  515. extern int __ceph_caps_issued_other(struct ceph_inode_info *ci,
  516. struct ceph_cap *cap);
  517. static inline int ceph_caps_issued(struct ceph_inode_info *ci)
  518. {
  519. int issued;
  520. spin_lock(&ci->i_ceph_lock);
  521. issued = __ceph_caps_issued(ci, NULL);
  522. spin_unlock(&ci->i_ceph_lock);
  523. return issued;
  524. }
  525. static inline int ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask,
  526. int touch)
  527. {
  528. int r;
  529. spin_lock(&ci->i_ceph_lock);
  530. r = __ceph_caps_issued_mask(ci, mask, touch);
  531. spin_unlock(&ci->i_ceph_lock);
  532. return r;
  533. }
  534. static inline int __ceph_caps_dirty(struct ceph_inode_info *ci)
  535. {
  536. return ci->i_dirty_caps | ci->i_flushing_caps;
  537. }
  538. extern struct ceph_cap_flush *ceph_alloc_cap_flush(void);
  539. extern void ceph_free_cap_flush(struct ceph_cap_flush *cf);
  540. extern int __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask,
  541. struct ceph_cap_flush **pcf);
  542. extern int __ceph_caps_revoking_other(struct ceph_inode_info *ci,
  543. struct ceph_cap *ocap, int mask);
  544. extern int ceph_caps_revoking(struct ceph_inode_info *ci, int mask);
  545. extern int __ceph_caps_used(struct ceph_inode_info *ci);
  546. extern int __ceph_caps_file_wanted(struct ceph_inode_info *ci);
  547. /*
  548. * wanted, by virtue of open file modes AND cap refs (buffered/cached data)
  549. */
  550. static inline int __ceph_caps_wanted(struct ceph_inode_info *ci)
  551. {
  552. int w = __ceph_caps_file_wanted(ci) | __ceph_caps_used(ci);
  553. if (w & CEPH_CAP_FILE_BUFFER)
  554. w |= CEPH_CAP_FILE_EXCL; /* we want EXCL if dirty data */
  555. return w;
  556. }
  557. /* what the mds thinks we want */
  558. extern int __ceph_caps_mds_wanted(struct ceph_inode_info *ci, bool check);
  559. extern void ceph_caps_init(struct ceph_mds_client *mdsc);
  560. extern void ceph_caps_finalize(struct ceph_mds_client *mdsc);
  561. extern void ceph_adjust_min_caps(struct ceph_mds_client *mdsc, int delta);
  562. extern int ceph_reserve_caps(struct ceph_mds_client *mdsc,
  563. struct ceph_cap_reservation *ctx, int need);
  564. extern void ceph_unreserve_caps(struct ceph_mds_client *mdsc,
  565. struct ceph_cap_reservation *ctx);
  566. extern void ceph_reservation_status(struct ceph_fs_client *client,
  567. int *total, int *avail, int *used,
  568. int *reserved, int *min);
  569. /*
  570. * we keep buffered readdir results attached to file->private_data
  571. */
  572. #define CEPH_F_SYNC 1
  573. #define CEPH_F_ATEND 2
  574. struct ceph_file_info {
  575. short fmode; /* initialized on open */
  576. short flags; /* CEPH_F_* */
  577. spinlock_t rw_contexts_lock;
  578. struct list_head rw_contexts;
  579. };
  580. struct ceph_dir_file_info {
  581. struct ceph_file_info file_info;
  582. /* readdir: position within the dir */
  583. u32 frag;
  584. struct ceph_mds_request *last_readdir;
  585. /* readdir: position within a frag */
  586. unsigned next_offset; /* offset of next chunk (last_name's + 1) */
  587. char *last_name; /* last entry in previous chunk */
  588. long long dir_release_count;
  589. long long dir_ordered_count;
  590. int readdir_cache_idx;
  591. /* used for -o dirstat read() on directory thing */
  592. char *dir_info;
  593. int dir_info_len;
  594. };
  595. struct ceph_rw_context {
  596. struct list_head list;
  597. struct task_struct *thread;
  598. int caps;
  599. };
  600. #define CEPH_DEFINE_RW_CONTEXT(_name, _caps) \
  601. struct ceph_rw_context _name = { \
  602. .thread = current, \
  603. .caps = _caps, \
  604. }
  605. static inline void ceph_add_rw_context(struct ceph_file_info *cf,
  606. struct ceph_rw_context *ctx)
  607. {
  608. spin_lock(&cf->rw_contexts_lock);
  609. list_add(&ctx->list, &cf->rw_contexts);
  610. spin_unlock(&cf->rw_contexts_lock);
  611. }
  612. static inline void ceph_del_rw_context(struct ceph_file_info *cf,
  613. struct ceph_rw_context *ctx)
  614. {
  615. spin_lock(&cf->rw_contexts_lock);
  616. list_del(&ctx->list);
  617. spin_unlock(&cf->rw_contexts_lock);
  618. }
  619. static inline struct ceph_rw_context*
  620. ceph_find_rw_context(struct ceph_file_info *cf)
  621. {
  622. struct ceph_rw_context *ctx, *found = NULL;
  623. spin_lock(&cf->rw_contexts_lock);
  624. list_for_each_entry(ctx, &cf->rw_contexts, list) {
  625. if (ctx->thread == current) {
  626. found = ctx;
  627. break;
  628. }
  629. }
  630. spin_unlock(&cf->rw_contexts_lock);
  631. return found;
  632. }
  633. struct ceph_readdir_cache_control {
  634. struct page *page;
  635. struct dentry **dentries;
  636. int index;
  637. };
  638. /*
  639. * A "snap realm" describes a subset of the file hierarchy sharing
  640. * the same set of snapshots that apply to it. The realms themselves
  641. * are organized into a hierarchy, such that children inherit (some of)
  642. * the snapshots of their parents.
  643. *
  644. * All inodes within the realm that have capabilities are linked into a
  645. * per-realm list.
  646. */
  647. struct ceph_snap_realm {
  648. u64 ino;
  649. struct inode *inode;
  650. atomic_t nref;
  651. struct rb_node node;
  652. u64 created, seq;
  653. u64 parent_ino;
  654. u64 parent_since; /* snapid when our current parent became so */
  655. u64 *prior_parent_snaps; /* snaps inherited from any parents we */
  656. u32 num_prior_parent_snaps; /* had prior to parent_since */
  657. u64 *snaps; /* snaps specific to this realm */
  658. u32 num_snaps;
  659. struct ceph_snap_realm *parent;
  660. struct list_head children; /* list of child realms */
  661. struct list_head child_item;
  662. struct list_head empty_item; /* if i have ref==0 */
  663. struct list_head dirty_item; /* if realm needs new context */
  664. /* the current set of snaps for this realm */
  665. struct ceph_snap_context *cached_context;
  666. struct list_head inodes_with_caps;
  667. spinlock_t inodes_with_caps_lock;
  668. };
  669. static inline int default_congestion_kb(void)
  670. {
  671. int congestion_kb;
  672. /*
  673. * Copied from NFS
  674. *
  675. * congestion size, scale with available memory.
  676. *
  677. * 64MB: 8192k
  678. * 128MB: 11585k
  679. * 256MB: 16384k
  680. * 512MB: 23170k
  681. * 1GB: 32768k
  682. * 2GB: 46340k
  683. * 4GB: 65536k
  684. * 8GB: 92681k
  685. * 16GB: 131072k
  686. *
  687. * This allows larger machines to have larger/more transfers.
  688. * Limit the default to 256M
  689. */
  690. congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
  691. if (congestion_kb > 256*1024)
  692. congestion_kb = 256*1024;
  693. return congestion_kb;
  694. }
  695. /* snap.c */
  696. struct ceph_snap_realm *ceph_lookup_snap_realm(struct ceph_mds_client *mdsc,
  697. u64 ino);
  698. extern void ceph_get_snap_realm(struct ceph_mds_client *mdsc,
  699. struct ceph_snap_realm *realm);
  700. extern void ceph_put_snap_realm(struct ceph_mds_client *mdsc,
  701. struct ceph_snap_realm *realm);
  702. extern int ceph_update_snap_trace(struct ceph_mds_client *m,
  703. void *p, void *e, bool deletion,
  704. struct ceph_snap_realm **realm_ret);
  705. extern void ceph_handle_snap(struct ceph_mds_client *mdsc,
  706. struct ceph_mds_session *session,
  707. struct ceph_msg *msg);
  708. extern void ceph_queue_cap_snap(struct ceph_inode_info *ci);
  709. extern int __ceph_finish_cap_snap(struct ceph_inode_info *ci,
  710. struct ceph_cap_snap *capsnap);
  711. extern void ceph_cleanup_empty_realms(struct ceph_mds_client *mdsc);
  712. /*
  713. * a cap_snap is "pending" if it is still awaiting an in-progress
  714. * sync write (that may/may not still update size, mtime, etc.).
  715. */
  716. static inline bool __ceph_have_pending_cap_snap(struct ceph_inode_info *ci)
  717. {
  718. return !list_empty(&ci->i_cap_snaps) &&
  719. list_last_entry(&ci->i_cap_snaps, struct ceph_cap_snap,
  720. ci_item)->writing;
  721. }
  722. /* inode.c */
  723. extern const struct inode_operations ceph_file_iops;
  724. extern struct inode *ceph_alloc_inode(struct super_block *sb);
  725. extern void ceph_evict_inode(struct inode *inode);
  726. extern void ceph_destroy_inode(struct inode *inode);
  727. extern int ceph_drop_inode(struct inode *inode);
  728. extern struct inode *ceph_get_inode(struct super_block *sb,
  729. struct ceph_vino vino);
  730. extern struct inode *ceph_get_snapdir(struct inode *parent);
  731. extern int ceph_fill_file_size(struct inode *inode, int issued,
  732. u32 truncate_seq, u64 truncate_size, u64 size);
  733. extern void ceph_fill_file_time(struct inode *inode, int issued,
  734. u64 time_warp_seq, struct timespec64 *ctime,
  735. struct timespec64 *mtime,
  736. struct timespec64 *atime);
  737. extern int ceph_fill_trace(struct super_block *sb,
  738. struct ceph_mds_request *req);
  739. extern int ceph_readdir_prepopulate(struct ceph_mds_request *req,
  740. struct ceph_mds_session *session);
  741. extern int ceph_inode_holds_cap(struct inode *inode, int mask);
  742. extern bool ceph_inode_set_size(struct inode *inode, loff_t size);
  743. extern void __ceph_do_pending_vmtruncate(struct inode *inode);
  744. extern void ceph_queue_vmtruncate(struct inode *inode);
  745. extern void ceph_queue_invalidate(struct inode *inode);
  746. extern void ceph_queue_writeback(struct inode *inode);
  747. extern int __ceph_do_getattr(struct inode *inode, struct page *locked_page,
  748. int mask, bool force);
  749. static inline int ceph_do_getattr(struct inode *inode, int mask, bool force)
  750. {
  751. return __ceph_do_getattr(inode, NULL, mask, force);
  752. }
  753. extern int ceph_permission(struct inode *inode, int mask);
  754. extern int __ceph_setattr(struct inode *inode, struct iattr *attr);
  755. extern int ceph_setattr(struct dentry *dentry, struct iattr *attr);
  756. extern int ceph_getattr(const struct path *path, struct kstat *stat,
  757. u32 request_mask, unsigned int flags);
  758. /* xattr.c */
  759. int __ceph_setxattr(struct inode *, const char *, const void *, size_t, int);
  760. ssize_t __ceph_getxattr(struct inode *, const char *, void *, size_t);
  761. extern ssize_t ceph_listxattr(struct dentry *, char *, size_t);
  762. extern struct ceph_buffer *__ceph_build_xattrs_blob(struct ceph_inode_info *ci);
  763. extern void __ceph_destroy_xattrs(struct ceph_inode_info *ci);
  764. extern void __init ceph_xattr_init(void);
  765. extern void ceph_xattr_exit(void);
  766. extern const struct xattr_handler *ceph_xattr_handlers[];
  767. #ifdef CONFIG_SECURITY
  768. extern bool ceph_security_xattr_deadlock(struct inode *in);
  769. extern bool ceph_security_xattr_wanted(struct inode *in);
  770. #else
  771. static inline bool ceph_security_xattr_deadlock(struct inode *in)
  772. {
  773. return false;
  774. }
  775. static inline bool ceph_security_xattr_wanted(struct inode *in)
  776. {
  777. return false;
  778. }
  779. #endif
  780. /* acl.c */
  781. struct ceph_acls_info {
  782. void *default_acl;
  783. void *acl;
  784. struct ceph_pagelist *pagelist;
  785. };
  786. #ifdef CONFIG_CEPH_FS_POSIX_ACL
  787. struct posix_acl *ceph_get_acl(struct inode *, int);
  788. int ceph_set_acl(struct inode *inode, struct posix_acl *acl, int type);
  789. int ceph_pre_init_acls(struct inode *dir, umode_t *mode,
  790. struct ceph_acls_info *info);
  791. void ceph_init_inode_acls(struct inode *inode, struct ceph_acls_info *info);
  792. void ceph_release_acls_info(struct ceph_acls_info *info);
  793. static inline void ceph_forget_all_cached_acls(struct inode *inode)
  794. {
  795. forget_all_cached_acls(inode);
  796. }
  797. #else
  798. #define ceph_get_acl NULL
  799. #define ceph_set_acl NULL
  800. static inline int ceph_pre_init_acls(struct inode *dir, umode_t *mode,
  801. struct ceph_acls_info *info)
  802. {
  803. return 0;
  804. }
  805. static inline void ceph_init_inode_acls(struct inode *inode,
  806. struct ceph_acls_info *info)
  807. {
  808. }
  809. static inline void ceph_release_acls_info(struct ceph_acls_info *info)
  810. {
  811. }
  812. static inline int ceph_acl_chmod(struct dentry *dentry, struct inode *inode)
  813. {
  814. return 0;
  815. }
  816. static inline void ceph_forget_all_cached_acls(struct inode *inode)
  817. {
  818. }
  819. #endif
  820. /* caps.c */
  821. extern const char *ceph_cap_string(int c);
  822. extern void ceph_handle_caps(struct ceph_mds_session *session,
  823. struct ceph_msg *msg);
  824. extern struct ceph_cap *ceph_get_cap(struct ceph_mds_client *mdsc,
  825. struct ceph_cap_reservation *ctx);
  826. extern void ceph_add_cap(struct inode *inode,
  827. struct ceph_mds_session *session, u64 cap_id,
  828. int fmode, unsigned issued, unsigned wanted,
  829. unsigned cap, unsigned seq, u64 realmino, int flags,
  830. struct ceph_cap **new_cap);
  831. extern void __ceph_remove_cap(struct ceph_cap *cap, bool queue_release);
  832. extern void ceph_put_cap(struct ceph_mds_client *mdsc,
  833. struct ceph_cap *cap);
  834. extern int ceph_is_any_caps(struct inode *inode);
  835. extern void ceph_queue_caps_release(struct inode *inode);
  836. extern int ceph_write_inode(struct inode *inode, struct writeback_control *wbc);
  837. extern int ceph_fsync(struct file *file, loff_t start, loff_t end,
  838. int datasync);
  839. extern void ceph_early_kick_flushing_caps(struct ceph_mds_client *mdsc,
  840. struct ceph_mds_session *session);
  841. extern void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc,
  842. struct ceph_mds_session *session);
  843. extern struct ceph_cap *ceph_get_cap_for_mds(struct ceph_inode_info *ci,
  844. int mds);
  845. extern int ceph_get_cap_mds(struct inode *inode);
  846. extern void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps);
  847. extern void ceph_put_cap_refs(struct ceph_inode_info *ci, int had);
  848. extern void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr,
  849. struct ceph_snap_context *snapc);
  850. extern void ceph_flush_snaps(struct ceph_inode_info *ci,
  851. struct ceph_mds_session **psession);
  852. extern bool __ceph_should_report_size(struct ceph_inode_info *ci);
  853. extern void ceph_check_caps(struct ceph_inode_info *ci, int flags,
  854. struct ceph_mds_session *session);
  855. extern void ceph_check_delayed_caps(struct ceph_mds_client *mdsc);
  856. extern void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc);
  857. extern int ceph_drop_caps_for_unlink(struct inode *inode);
  858. extern int ceph_encode_inode_release(void **p, struct inode *inode,
  859. int mds, int drop, int unless, int force);
  860. extern int ceph_encode_dentry_release(void **p, struct dentry *dn,
  861. struct inode *dir,
  862. int mds, int drop, int unless);
  863. extern int ceph_get_caps(struct ceph_inode_info *ci, int need, int want,
  864. loff_t endoff, int *got, struct page **pinned_page);
  865. extern int ceph_try_get_caps(struct ceph_inode_info *ci,
  866. int need, int want, int *got);
  867. /* for counting open files by mode */
  868. extern void __ceph_get_fmode(struct ceph_inode_info *ci, int mode);
  869. extern void ceph_put_fmode(struct ceph_inode_info *ci, int mode);
  870. /* addr.c */
  871. extern const struct address_space_operations ceph_aops;
  872. extern int ceph_mmap(struct file *file, struct vm_area_struct *vma);
  873. extern int ceph_uninline_data(struct file *filp, struct page *locked_page);
  874. extern int ceph_pool_perm_check(struct ceph_inode_info *ci, int need);
  875. extern void ceph_pool_perm_destroy(struct ceph_mds_client* mdsc);
  876. /* file.c */
  877. extern const struct file_operations ceph_file_fops;
  878. extern int ceph_renew_caps(struct inode *inode);
  879. extern int ceph_open(struct inode *inode, struct file *file);
  880. extern int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
  881. struct file *file, unsigned flags, umode_t mode);
  882. extern int ceph_release(struct inode *inode, struct file *filp);
  883. extern void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
  884. char *data, size_t len);
  885. /* dir.c */
  886. extern const struct file_operations ceph_dir_fops;
  887. extern const struct file_operations ceph_snapdir_fops;
  888. extern const struct inode_operations ceph_dir_iops;
  889. extern const struct inode_operations ceph_snapdir_iops;
  890. extern const struct dentry_operations ceph_dentry_ops;
  891. extern loff_t ceph_make_fpos(unsigned high, unsigned off, bool hash_order);
  892. extern int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry);
  893. extern int ceph_handle_snapdir(struct ceph_mds_request *req,
  894. struct dentry *dentry, int err);
  895. extern struct dentry *ceph_finish_lookup(struct ceph_mds_request *req,
  896. struct dentry *dentry, int err);
  897. extern void ceph_dentry_lru_add(struct dentry *dn);
  898. extern void ceph_dentry_lru_touch(struct dentry *dn);
  899. extern void ceph_dentry_lru_del(struct dentry *dn);
  900. extern void ceph_invalidate_dentry_lease(struct dentry *dentry);
  901. extern unsigned ceph_dentry_hash(struct inode *dir, struct dentry *dn);
  902. extern void ceph_readdir_cache_release(struct ceph_readdir_cache_control *ctl);
  903. /* ioctl.c */
  904. extern long ceph_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  905. /* export.c */
  906. extern const struct export_operations ceph_export_ops;
  907. /* locks.c */
  908. extern __init void ceph_flock_init(void);
  909. extern int ceph_lock(struct file *file, int cmd, struct file_lock *fl);
  910. extern int ceph_flock(struct file *file, int cmd, struct file_lock *fl);
  911. extern void ceph_count_locks(struct inode *inode, int *p_num, int *f_num);
  912. extern int ceph_encode_locks_to_buffer(struct inode *inode,
  913. struct ceph_filelock *flocks,
  914. int num_fcntl_locks,
  915. int num_flock_locks);
  916. extern int ceph_locks_to_pagelist(struct ceph_filelock *flocks,
  917. struct ceph_pagelist *pagelist,
  918. int num_fcntl_locks, int num_flock_locks);
  919. /* debugfs.c */
  920. extern int ceph_fs_debugfs_init(struct ceph_fs_client *client);
  921. extern void ceph_fs_debugfs_cleanup(struct ceph_fs_client *client);
  922. /* quota.c */
  923. static inline bool __ceph_has_any_quota(struct ceph_inode_info *ci)
  924. {
  925. return ci->i_max_files || ci->i_max_bytes;
  926. }
  927. extern void ceph_adjust_quota_realms_count(struct inode *inode, bool inc);
  928. static inline void __ceph_update_quota(struct ceph_inode_info *ci,
  929. u64 max_bytes, u64 max_files)
  930. {
  931. bool had_quota, has_quota;
  932. had_quota = __ceph_has_any_quota(ci);
  933. ci->i_max_bytes = max_bytes;
  934. ci->i_max_files = max_files;
  935. has_quota = __ceph_has_any_quota(ci);
  936. if (had_quota != has_quota)
  937. ceph_adjust_quota_realms_count(&ci->vfs_inode, has_quota);
  938. }
  939. extern void ceph_handle_quota(struct ceph_mds_client *mdsc,
  940. struct ceph_mds_session *session,
  941. struct ceph_msg *msg);
  942. extern bool ceph_quota_is_max_files_exceeded(struct inode *inode);
  943. extern bool ceph_quota_is_same_realm(struct inode *old, struct inode *new);
  944. extern bool ceph_quota_is_max_bytes_exceeded(struct inode *inode,
  945. loff_t newlen);
  946. extern bool ceph_quota_is_max_bytes_approaching(struct inode *inode,
  947. loff_t newlen);
  948. extern bool ceph_quota_update_statfs(struct ceph_fs_client *fsc,
  949. struct kstatfs *buf);
  950. #endif /* _FS_CEPH_SUPER_H */