super.h 34 KB

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