operations.txt 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. ================================
  2. ASYNCHRONOUS OPERATIONS HANDLING
  3. ================================
  4. By: David Howells <dhowells@redhat.com>
  5. Contents:
  6. (*) Overview.
  7. (*) Operation record initialisation.
  8. (*) Parameters.
  9. (*) Procedure.
  10. (*) Asynchronous callback.
  11. ========
  12. OVERVIEW
  13. ========
  14. FS-Cache has an asynchronous operations handling facility that it uses for its
  15. data storage and retrieval routines. Its operations are represented by
  16. fscache_operation structs, though these are usually embedded into some other
  17. structure.
  18. This facility is available to and expected to be be used by the cache backends,
  19. and FS-Cache will create operations and pass them off to the appropriate cache
  20. backend for completion.
  21. To make use of this facility, <linux/fscache-cache.h> should be #included.
  22. ===============================
  23. OPERATION RECORD INITIALISATION
  24. ===============================
  25. An operation is recorded in an fscache_operation struct:
  26. struct fscache_operation {
  27. union {
  28. struct work_struct fast_work;
  29. struct slow_work slow_work;
  30. };
  31. unsigned long flags;
  32. fscache_operation_processor_t processor;
  33. ...
  34. };
  35. Someone wanting to issue an operation should allocate something with this
  36. struct embedded in it. They should initialise it by calling:
  37. void fscache_operation_init(struct fscache_operation *op,
  38. fscache_operation_release_t release);
  39. with the operation to be initialised and the release function to use.
  40. The op->flags parameter should be set to indicate the CPU time provision and
  41. the exclusivity (see the Parameters section).
  42. The op->fast_work, op->slow_work and op->processor flags should be set as
  43. appropriate for the CPU time provision (see the Parameters section).
  44. FSCACHE_OP_WAITING may be set in op->flags prior to each submission of the
  45. operation and waited for afterwards.
  46. ==========
  47. PARAMETERS
  48. ==========
  49. There are a number of parameters that can be set in the operation record's flag
  50. parameter. There are three options for the provision of CPU time in these
  51. operations:
  52. (1) The operation may be done synchronously (FSCACHE_OP_MYTHREAD). A thread
  53. may decide it wants to handle an operation itself without deferring it to
  54. another thread.
  55. This is, for example, used in read operations for calling readpages() on
  56. the backing filesystem in CacheFiles. Although readpages() does an
  57. asynchronous data fetch, the determination of whether pages exist is done
  58. synchronously - and the netfs does not proceed until this has been
  59. determined.
  60. If this option is to be used, FSCACHE_OP_WAITING must be set in op->flags
  61. before submitting the operation, and the operating thread must wait for it
  62. to be cleared before proceeding:
  63. wait_on_bit(&op->flags, FSCACHE_OP_WAITING,
  64. TASK_UNINTERRUPTIBLE);
  65. (2) The operation may be fast asynchronous (FSCACHE_OP_FAST), in which case it
  66. will be given to keventd to process. Such an operation is not permitted
  67. to sleep on I/O.
  68. This is, for example, used by CacheFiles to copy data from a backing fs
  69. page to a netfs page after the backing fs has read the page in.
  70. If this option is used, op->fast_work and op->processor must be
  71. initialised before submitting the operation:
  72. INIT_WORK(&op->fast_work, do_some_work);
  73. (3) The operation may be slow asynchronous (FSCACHE_OP_SLOW), in which case it
  74. will be given to the slow work facility to process. Such an operation is
  75. permitted to sleep on I/O.
  76. This is, for example, used by FS-Cache to handle background writes of
  77. pages that have just been fetched from a remote server.
  78. If this option is used, op->slow_work and op->processor must be
  79. initialised before submitting the operation:
  80. fscache_operation_init_slow(op, processor)
  81. Furthermore, operations may be one of two types:
  82. (1) Exclusive (FSCACHE_OP_EXCLUSIVE). Operations of this type may not run in
  83. conjunction with any other operation on the object being operated upon.
  84. An example of this is the attribute change operation, in which the file
  85. being written to may need truncation.
  86. (2) Shareable. Operations of this type may be running simultaneously. It's
  87. up to the operation implementation to prevent interference between other
  88. operations running at the same time.
  89. =========
  90. PROCEDURE
  91. =========
  92. Operations are used through the following procedure:
  93. (1) The submitting thread must allocate the operation and initialise it
  94. itself. Normally this would be part of a more specific structure with the
  95. generic op embedded within.
  96. (2) The submitting thread must then submit the operation for processing using
  97. one of the following two functions:
  98. int fscache_submit_op(struct fscache_object *object,
  99. struct fscache_operation *op);
  100. int fscache_submit_exclusive_op(struct fscache_object *object,
  101. struct fscache_operation *op);
  102. The first function should be used to submit non-exclusive ops and the
  103. second to submit exclusive ones. The caller must still set the
  104. FSCACHE_OP_EXCLUSIVE flag.
  105. If successful, both functions will assign the operation to the specified
  106. object and return 0. -ENOBUFS will be returned if the object specified is
  107. permanently unavailable.
  108. The operation manager will defer operations on an object that is still
  109. undergoing lookup or creation. The operation will also be deferred if an
  110. operation of conflicting exclusivity is in progress on the object.
  111. If the operation is asynchronous, the manager will retain a reference to
  112. it, so the caller should put their reference to it by passing it to:
  113. void fscache_put_operation(struct fscache_operation *op);
  114. (3) If the submitting thread wants to do the work itself, and has marked the
  115. operation with FSCACHE_OP_MYTHREAD, then it should monitor
  116. FSCACHE_OP_WAITING as described above and check the state of the object if
  117. necessary (the object might have died whilst the thread was waiting).
  118. When it has finished doing its processing, it should call
  119. fscache_op_complete() and fscache_put_operation() on it.
  120. (4) The operation holds an effective lock upon the object, preventing other
  121. exclusive ops conflicting until it is released. The operation can be
  122. enqueued for further immediate asynchronous processing by adjusting the
  123. CPU time provisioning option if necessary, eg:
  124. op->flags &= ~FSCACHE_OP_TYPE;
  125. op->flags |= ~FSCACHE_OP_FAST;
  126. and calling:
  127. void fscache_enqueue_operation(struct fscache_operation *op)
  128. This can be used to allow other things to have use of the worker thread
  129. pools.
  130. =====================
  131. ASYNCHRONOUS CALLBACK
  132. =====================
  133. When used in asynchronous mode, the worker thread pool will invoke the
  134. processor method with a pointer to the operation. This should then get at the
  135. container struct by using container_of():
  136. static void fscache_write_op(struct fscache_operation *_op)
  137. {
  138. struct fscache_storage *op =
  139. container_of(_op, struct fscache_storage, op);
  140. ...
  141. }
  142. The caller holds a reference on the operation, and will invoke
  143. fscache_put_operation() when the processor function returns. The processor
  144. function is at liberty to call fscache_enqueue_operation() or to take extra
  145. references.