1 /* Copyright 2001, 2002, 2003 by Hans Reiser, licensing governed by
4 * Written by Edward Shishkin.
6 * Implementations of inode/file/address_space operations
7 * specific for cryptcompress file plugin which manages
8 * regular files built of compressed and(or) encrypted bodies.
9 * See http://dev.namesys.com/CryptcompressPlugin for details.
12 #include "../../inode.h"
13 #include "../cluster.h"
14 #include "../object.h"
15 #include "../../tree_walk.h"
16 #include "cryptcompress.h"
18 #include <linux/pagevec.h>
19 #include <asm/uaccess.h>
20 #include <linux/swap.h>
21 #include <linux/writeback.h>
22 #include <linux/random.h>
23 #include <linux/scatterlist.h>
26 Managing primary and secondary caches by Reiser4
27 cryptcompress file plugin. Synchronization scheme.
31 +------------------->| tfm stream |
32 | | (compressed data)|
33 flush | +------------------+
36 --+ writepages() | | +-***-+ reiser4 +---+
37 | | +--+ | *** | storage tree | |
38 | | | +-***-+ (primary cache)| |
39 u | write() (secondary| cache) V / | \ | |
40 s | ----> +----+ +----+ +----+ +----+ +-***** ******* **----+ ----> | d |
41 e | | | |page cluster | | | **disk cluster** | | i |
42 r | <---- +----+ +----+ +----+ +----+ +-***** **********----+ <---- | s |
44 | | (->)longterm lock| | page_io()| |
46 --+ readpages() | | +---+
48 | +------------------+
49 +--------------------| tfm stream |
54 /* get cryptcompress specific portion of inode */
55 struct cryptcompress_info
*cryptcompress_inode_data(const struct inode
*inode
)
57 return &reiser4_inode_data(inode
)->file_plugin_data
.cryptcompress_info
;
60 /* plugin->u.file.init_inode_data */
61 void init_inode_data_cryptcompress(struct inode
*inode
,
62 reiser4_object_create_data
* crd
,
65 struct cryptcompress_info
*data
;
67 data
= cryptcompress_inode_data(inode
);
68 assert("edward-685", data
!= NULL
);
70 memset(data
, 0, sizeof(*data
));
72 mutex_init(&data
->checkin_mutex
);
73 data
->trunc_index
= ULONG_MAX
;
74 turn_on_compression(data
);
75 set_lattice_factor(data
, MIN_LATTICE_FACTOR
);
76 init_inode_ordering(inode
, crd
, create
);
79 /* The following is a part of reiser4 cipher key manager
80 which is called when opening/creating a cryptcompress file */
82 /* get/set cipher key info */
83 struct reiser4_crypto_info
* inode_crypto_info (struct inode
* inode
)
85 assert("edward-90", inode
!= NULL
);
86 assert("edward-91", reiser4_inode_data(inode
) != NULL
);
87 return cryptcompress_inode_data(inode
)->crypt
;
90 static void set_inode_crypto_info (struct inode
* inode
,
91 struct reiser4_crypto_info
* info
)
93 cryptcompress_inode_data(inode
)->crypt
= info
;
96 /* allocate a cipher key info */
97 struct reiser4_crypto_info
* reiser4_alloc_crypto_info (struct inode
* inode
)
99 struct reiser4_crypto_info
*info
;
102 info
= kzalloc(sizeof(*info
), reiser4_ctx_gfp_mask_get());
104 return ERR_PTR(-ENOMEM
);
106 fipsize
= inode_digest_plugin(inode
)->fipsize
;
107 info
->keyid
= kmalloc(fipsize
, reiser4_ctx_gfp_mask_get());
110 return ERR_PTR(-ENOMEM
);
117 /* allocate/free low-level info for cipher and digest
119 static int alloc_crypto_tfms(struct reiser4_crypto_info
* info
)
121 struct crypto_blkcipher
* ctfm
= NULL
;
122 struct crypto_hash
* dtfm
= NULL
;
123 cipher_plugin
* cplug
= inode_cipher_plugin(info
->host
);
124 digest_plugin
* dplug
= inode_digest_plugin(info
->host
);
127 ctfm
= cplug
->alloc();
129 warning("edward-1364",
130 "Can not allocate info for %s\n",
132 return RETERR(PTR_ERR(ctfm
));
135 info_set_cipher(info
, ctfm
);
137 dtfm
= dplug
->alloc();
139 warning("edward-1365",
140 "Can not allocate info for %s\n",
142 goto unhappy_with_digest
;
145 info_set_digest(info
, dtfm
);
150 info_set_cipher(info
, NULL
);
152 return RETERR(PTR_ERR(dtfm
));
157 free_crypto_tfms(struct reiser4_crypto_info
* info
)
159 assert("edward-1366", info
!= NULL
);
160 if (!info_get_cipher(info
)) {
161 assert("edward-1601", !info_get_digest(info
));
164 inode_cipher_plugin(info
->host
)->free(info_get_cipher(info
));
165 info_set_cipher(info
, NULL
);
166 inode_digest_plugin(info
->host
)->free(info_get_digest(info
));
167 info_set_digest(info
, NULL
);
172 /* create a key fingerprint for disk stat-data */
173 static int create_keyid (struct reiser4_crypto_info
* info
,
174 struct reiser4_crypto_data
* data
)
180 struct hash_desc ddesc
;
181 struct blkcipher_desc cdesc
;
182 struct scatterlist sg
;
184 assert("edward-1367", info
!= NULL
);
185 assert("edward-1368", info
->keyid
!= NULL
);
187 ddesc
.tfm
= info_get_digest(info
);
189 cdesc
.tfm
= info_get_cipher(info
);
192 dmem
= kmalloc((size_t)crypto_hash_digestsize(ddesc
.tfm
),
193 reiser4_ctx_gfp_mask_get());
197 blk
= crypto_blkcipher_blocksize(cdesc
.tfm
);
199 pad
= data
->keyid_size
% blk
;
200 pad
= (pad
? blk
- pad
: 0);
202 cmem
= kmalloc((size_t)data
->keyid_size
+ pad
,
203 reiser4_ctx_gfp_mask_get());
206 memcpy(cmem
, data
->keyid
, data
->keyid_size
);
207 memset(cmem
+ data
->keyid_size
, 0, pad
);
209 sg_init_one(&sg
, cmem
, data
->keyid_size
+ pad
);
211 ret
= crypto_blkcipher_encrypt(&cdesc
, &sg
, &sg
,
212 data
->keyid_size
+ pad
);
214 warning("edward-1369",
215 "encryption failed flags=%x\n", cdesc
.flags
);
218 ret
= crypto_hash_digest(&ddesc
, &sg
, sg
.length
, dmem
);
220 warning("edward-1602",
221 "digest failed flags=%x\n", ddesc
.flags
);
224 memcpy(info
->keyid
, dmem
, inode_digest_plugin(info
->host
)->fipsize
);
234 static void destroy_keyid(struct reiser4_crypto_info
* info
)
236 assert("edward-1370", info
!= NULL
);
237 assert("edward-1371", info
->keyid
!= NULL
);
242 static void __free_crypto_info (struct inode
* inode
)
244 struct reiser4_crypto_info
* info
= inode_crypto_info(inode
);
245 assert("edward-1372", info
!= NULL
);
247 free_crypto_tfms(info
);
253 static void instantiate_crypto_info(struct reiser4_crypto_info
* info
)
255 assert("edward-1373", info
!= NULL
);
256 assert("edward-1374", info
->inst
== 0);
261 static void uninstantiate_crypto_info(struct reiser4_crypto_info
* info
)
263 assert("edward-1375", info
!= NULL
);
267 static int is_crypto_info_instantiated(struct reiser4_crypto_info
* info
)
272 static int inode_has_cipher_key(struct inode
* inode
)
274 assert("edward-1376", inode
!= NULL
);
275 return inode_crypto_info(inode
) &&
276 is_crypto_info_instantiated(inode_crypto_info(inode
));
279 static void free_crypto_info (struct inode
* inode
)
281 uninstantiate_crypto_info(inode_crypto_info(inode
));
282 __free_crypto_info(inode
);
285 static int need_cipher(struct inode
* inode
)
287 return inode_cipher_plugin(inode
) !=
288 cipher_plugin_by_id(NONE_CIPHER_ID
);
291 /* Parse @data which contains a (uninstantiated) cipher key imported
292 from user space, create a low-level cipher info and attach it to
293 the @object. If success, then info contains an instantiated key */
295 struct reiser4_crypto_info
* create_crypto_info(struct inode
* object
,
296 struct reiser4_crypto_data
* data
)
299 struct reiser4_crypto_info
* info
;
301 assert("edward-1377", data
!= NULL
);
302 assert("edward-1378", need_cipher(object
));
304 if (inode_file_plugin(object
) !=
305 file_plugin_by_id(DIRECTORY_FILE_PLUGIN_ID
))
306 return ERR_PTR(-EINVAL
);
308 info
= reiser4_alloc_crypto_info(object
);
311 ret
= alloc_crypto_tfms(info
);
314 /* instantiating a key */
315 ret
= crypto_blkcipher_setkey(info_get_cipher(info
),
319 warning("edward-1379",
320 "setkey failed flags=%x",
321 crypto_blkcipher_get_flags(info_get_cipher(info
)));
324 info
->keysize
= data
->keysize
;
325 ret
= create_keyid(info
, data
);
328 instantiate_crypto_info(info
);
331 __free_crypto_info(object
);
336 /* increment/decrement a load counter when
337 attaching/detaching the crypto-stat to any object */
338 static void load_crypto_info(struct reiser4_crypto_info
* info
)
340 assert("edward-1380", info
!= NULL
);
341 inc_keyload_count(info
);
344 static void unload_crypto_info(struct inode
* inode
)
346 struct reiser4_crypto_info
* info
= inode_crypto_info(inode
);
347 assert("edward-1381", info
->keyload_count
> 0);
349 dec_keyload_count(inode_crypto_info(inode
));
350 if (info
->keyload_count
== 0)
352 free_crypto_info(inode
);
355 /* attach/detach an existing crypto-stat */
356 void reiser4_attach_crypto_info(struct inode
* inode
,
357 struct reiser4_crypto_info
* info
)
359 assert("edward-1382", inode
!= NULL
);
360 assert("edward-1383", info
!= NULL
);
361 assert("edward-1384", inode_crypto_info(inode
) == NULL
);
363 set_inode_crypto_info(inode
, info
);
364 load_crypto_info(info
);
367 /* returns true, if crypto stat can be attached to the @host */
369 static int host_allows_crypto_info(struct inode
* host
)
372 file_plugin
* fplug
= inode_file_plugin(host
);
374 switch (fplug
->h
.id
) {
375 case CRYPTCOMPRESS_FILE_PLUGIN_ID
:
383 #endif /* REISER4_DEBUG */
385 static void reiser4_detach_crypto_info(struct inode
* inode
)
387 assert("edward-1385", inode
!= NULL
);
388 assert("edward-1386", host_allows_crypto_info(inode
));
390 if (inode_crypto_info(inode
))
391 unload_crypto_info(inode
);
392 set_inode_crypto_info(inode
, NULL
);
397 /* compare fingerprints of @child and @parent */
398 static int keyid_eq(struct reiser4_crypto_info
* child
,
399 struct reiser4_crypto_info
* parent
)
401 return !memcmp(child
->keyid
,
403 info_digest_plugin(parent
)->fipsize
);
406 /* check if a crypto-stat (which is bound to @parent) can be inherited */
407 int can_inherit_crypto_cryptcompress(struct inode
*child
, struct inode
*parent
)
409 if (!need_cipher(child
))
411 /* the child is created */
412 if (!inode_crypto_info(child
))
414 /* the child is looked up */
415 if (!inode_crypto_info(parent
))
417 return (inode_cipher_plugin(child
) == inode_cipher_plugin(parent
) &&
418 inode_digest_plugin(child
) == inode_digest_plugin(parent
) &&
419 inode_crypto_info(child
)->keysize
==
420 inode_crypto_info(parent
)->keysize
&&
421 keyid_eq(inode_crypto_info(child
), inode_crypto_info(parent
)));
425 /* helper functions for ->create() method of the cryptcompress plugin */
426 static int inode_set_crypto(struct inode
* object
)
428 reiser4_inode
* info
;
429 if (!inode_crypto_info(object
)) {
430 if (need_cipher(object
))
431 return RETERR(-EINVAL
);
432 /* the file is not to be encrypted */
435 info
= reiser4_inode_data(object
);
436 info
->extmask
|= (1 << CRYPTO_STAT
);
440 static int inode_init_compression(struct inode
* object
)
443 assert("edward-1461", object
!= NULL
);
444 if (inode_compression_plugin(object
)->init
)
445 result
= inode_compression_plugin(object
)->init();
449 static int inode_check_cluster(struct inode
* object
)
451 assert("edward-696", object
!= NULL
);
453 if (unlikely(inode_cluster_size(object
) < PAGE_CACHE_SIZE
)) {
454 warning("edward-1320", "Can not support '%s' "
455 "logical clusters (less then page size)",
456 inode_cluster_plugin(object
)->h
.label
);
457 return RETERR(-EINVAL
);
459 if (unlikely(inode_cluster_shift(object
)) >= BITS_PER_BYTE
*sizeof(int)){
460 warning("edward-1463", "Can not support '%s' "
461 "logical clusters (too big for transform)",
462 inode_cluster_plugin(object
)->h
.label
);
463 return RETERR(-EINVAL
);
468 /* ->destroy_inode() method of the cryptcompress plugin */
469 void destroy_inode_cryptcompress(struct inode
* inode
)
471 assert("edward-1464", INODE_PGCOUNT(inode
) == 0);
472 reiser4_detach_crypto_info(inode
);
476 /* ->create() method of the cryptcompress plugin
479 . attach crypto info if specified
480 . attach compression info if specified
481 . attach cluster info
483 int create_cryptcompress(struct inode
*object
, struct inode
*parent
,
484 reiser4_object_create_data
* data
)
489 assert("edward-23", object
!= NULL
);
490 assert("edward-24", parent
!= NULL
);
491 assert("edward-30", data
!= NULL
);
492 assert("edward-26", reiser4_inode_get_flag(object
, REISER4_NO_SD
));
493 assert("edward-27", data
->id
== CRYPTCOMPRESS_FILE_PLUGIN_ID
);
495 info
= reiser4_inode_data(object
);
497 assert("edward-29", info
!= NULL
);
500 info
->plugin_mask
|= (1 << PSET_FILE
);
503 result
= inode_set_crypto(object
);
506 /* set compression */
507 result
= inode_init_compression(object
);
511 result
= inode_check_cluster(object
);
515 /* save everything in disk stat-data */
516 result
= write_sd_by_inode_common(object
);
520 reiser4_detach_crypto_info(object
);
524 /* ->open_object() method of the cryptcompress plugin */
525 int open_object_cryptcompress(struct inode
* inode
, struct file
* file
)
528 struct inode
* parent
;
530 assert("edward-1394", inode
!= NULL
);
531 assert("edward-1395", file
!= NULL
);
532 assert("edward-1396", file
!= NULL
);
533 assert("edward-1397", file
->f_dentry
->d_inode
== inode
);
534 assert("edward-1398", file
->f_dentry
->d_parent
!= NULL
);
535 assert("edward-1399", file
->f_dentry
->d_parent
->d_inode
!= NULL
);
537 inode_file_plugin(inode
) ==
538 file_plugin_by_id(CRYPTCOMPRESS_FILE_PLUGIN_ID
));
539 result
= inode_check_cluster(inode
);
542 result
= inode_init_compression(inode
);
545 if (!need_cipher(inode
))
546 /* the file is not to be ciphered */
548 parent
= file
->f_dentry
->d_parent
->d_inode
;
549 if (!inode_has_cipher_key(inode
))
550 return RETERR(-EINVAL
);
554 /* returns a blocksize, the attribute of a cipher algorithm */
556 cipher_blocksize(struct inode
* inode
)
558 assert("edward-758", need_cipher(inode
));
559 assert("edward-1400", inode_crypto_info(inode
) != NULL
);
560 return crypto_blkcipher_blocksize
561 (info_get_cipher(inode_crypto_info(inode
)));
564 /* returns offset translated by scale factor of the crypto-algorithm */
565 static loff_t
inode_scaled_offset (struct inode
* inode
,
566 const loff_t src_off
/* input offset */)
568 assert("edward-97", inode
!= NULL
);
570 if (!need_cipher(inode
) ||
571 src_off
== get_key_offset(reiser4_min_key()) ||
572 src_off
== get_key_offset(reiser4_max_key()))
575 return inode_cipher_plugin(inode
)->scale(inode
,
576 cipher_blocksize(inode
),
580 /* returns disk cluster size */
581 size_t inode_scaled_cluster_size(struct inode
* inode
)
583 assert("edward-110", inode
!= NULL
);
585 return inode_scaled_offset(inode
, inode_cluster_size(inode
));
588 /* set number of cluster pages */
589 static void set_cluster_nrpages(struct cluster_handle
* clust
,
592 struct reiser4_slide
* win
;
594 assert("edward-180", clust
!= NULL
);
595 assert("edward-1040", inode
!= NULL
);
597 clust
->old_nrpages
= size_in_pages(lbytes(clust
->index
, inode
));
600 clust
->nr_pages
= size_in_pages(lbytes(clust
->index
, inode
));
603 assert("edward-1176", clust
->op
!= LC_INVAL
);
604 assert("edward-1064", win
->off
+ win
->count
+ win
->delta
!= 0);
606 if (win
->stat
== HOLE_WINDOW
&&
607 win
->off
== 0 && win
->count
== inode_cluster_size(inode
)) {
608 /* special case: writing a "fake" logical cluster */
612 clust
->nr_pages
= size_in_pages(max(win
->off
+ win
->count
+ win
->delta
,
613 lbytes(clust
->index
, inode
)));
617 /* plugin->key_by_inode()
618 build key of a disk cluster */
619 int key_by_inode_cryptcompress(struct inode
*inode
, loff_t off
,
622 assert("edward-64", inode
!= 0);
624 if (likely(off
!= get_key_offset(reiser4_max_key())))
625 off
= off_to_clust_to_off(off
, inode
);
626 if (inode_crypto_info(inode
))
627 off
= inode_scaled_offset(inode
, off
);
629 key_by_inode_and_offset_common(inode
, 0, key
);
630 set_key_offset(key
, (__u64
)off
);
634 /* plugin->flow_by_inode() */
635 /* flow is used to read/write disk clusters */
636 int flow_by_inode_cryptcompress(struct inode
*inode
, const char __user
* buf
,
637 int user
, /* 1: @buf is of user space,
639 loff_t size
, /* @buf size */
640 loff_t off
, /* offset to start io from */
641 rw_op op
, /* READ or WRITE */
642 flow_t
* f
/* resulting flow */)
644 assert("edward-436", f
!= NULL
);
645 assert("edward-149", inode
!= NULL
);
646 assert("edward-150", inode_file_plugin(inode
) != NULL
);
647 assert("edward-1465", user
== 0); /* we use flow to read/write
648 disk clusters located in
651 memcpy(&f
->data
, &buf
, sizeof(buf
));
655 return key_by_inode_cryptcompress(inode
, off
, &f
->key
);
659 cryptcompress_hint_validate(hint_t
* hint
, const reiser4_key
* key
,
660 znode_lock_mode lock_mode
)
664 assert("edward-704", hint
!= NULL
);
665 assert("edward-1089", !hint_is_valid(hint
));
666 assert("edward-706", hint
->lh
.owner
== NULL
);
668 coord
= &hint
->ext_coord
.coord
;
670 if (!hint
|| !hint_is_set(hint
) || hint
->mode
!= lock_mode
)
671 /* hint either not set or set by different operation */
672 return RETERR(-E_REPEAT
);
674 if (get_key_offset(key
) != hint
->offset
)
675 /* hint is set for different key */
676 return RETERR(-E_REPEAT
);
678 assert("edward-707", reiser4_schedulable());
680 return reiser4_seal_validate(&hint
->seal
, &hint
->ext_coord
.coord
,
681 key
, &hint
->lh
, lock_mode
,
685 /* reserve disk space when writing a logical cluster */
686 static int reserve4cluster(struct inode
*inode
, struct cluster_handle
*clust
)
690 assert("edward-965", reiser4_schedulable());
691 assert("edward-439", inode
!= NULL
);
692 assert("edward-440", clust
!= NULL
);
693 assert("edward-441", clust
->pages
!= NULL
);
695 if (clust
->nr_pages
== 0) {
696 assert("edward-1152", clust
->win
!= NULL
);
697 assert("edward-1153", clust
->win
->stat
== HOLE_WINDOW
);
698 /* don't reserve disk space for fake logical cluster */
701 assert("edward-442", jprivate(clust
->pages
[0]) != NULL
);
703 result
= reiser4_grab_space_force(estimate_insert_cluster(inode
) +
704 estimate_update_cluster(inode
),
709 grabbed2cluster_reserved(estimate_insert_cluster(inode
) +
710 estimate_update_cluster(inode
));
712 clust
->reserved_prepped
= estimate_update_cluster(inode
);
713 clust
->reserved_unprepped
= estimate_insert_cluster(inode
);
715 /* there can be space grabbed by txnmgr_force_commit_all */
719 /* free reserved disk space if writing a logical cluster fails */
720 static void free_reserved4cluster(struct inode
*inode
,
721 struct cluster_handle
*ch
, int count
)
723 assert("edward-967", ch
->reserved
== 1);
725 cluster_reserved2free(count
);
729 /* The core search procedure of the cryptcompress plugin.
730 If returned value is not cbk_errored, then current znode is locked */
731 static int find_cluster_item(hint_t
* hint
,
732 const reiser4_key
* key
, /* key of the item we are
734 znode_lock_mode lock_mode
/* which lock */ ,
735 ra_info_t
* ra_info
, lookup_bias bias
, __u32 flags
)
740 coord_t
*coord
= &hint
->ext_coord
.coord
;
741 coord_t orig
= *coord
;
743 assert("edward-152", hint
!= NULL
);
745 if (!hint_is_valid(hint
)) {
746 result
= cryptcompress_hint_validate(hint
, key
, lock_mode
);
747 if (result
== -E_REPEAT
)
750 assert("edward-1216", 0);
753 hint_set_valid(hint
);
755 assert("edward-709", znode_is_any_locked(coord
->node
));
757 /* In-place lookup is going here, it means we just need to
758 check if next item of the @coord match to the @keyhint) */
760 if (equal_to_rdk(coord
->node
, key
)) {
761 result
= goto_right_neighbor(coord
, &hint
->lh
);
762 if (result
== -E_NO_NEIGHBOR
) {
763 assert("edward-1217", 0);
768 assert("edward-1218", equal_to_ldk(coord
->node
, key
));
773 coord
->between
= AT_UNIT
;
775 result
= zload(coord
->node
);
778 assert("edward-1219", !node_is_empty(coord
->node
));
780 if (!coord_is_existing_item(coord
)) {
784 item_key_by_coord(coord
, &ikey
);
786 if (!keyeq(key
, &ikey
))
788 /* Ok, item is found, update node counts */
790 dclust_inc_extension_ncount(hint
);
791 return CBK_COORD_FOUND
;
794 assert("edward-1220", coord
->item_pos
> 0);
798 ON_DEBUG(coord_update_v(coord
));
799 return CBK_COORD_NOTFOUND
;
802 assert("edward-713", hint
->lh
.owner
== NULL
);
803 assert("edward-714", reiser4_schedulable());
805 reiser4_unset_hint(hint
);
806 dclust_init_extension(hint
);
807 coord_init_zero(coord
);
808 result
= coord_by_key(current_tree
, key
, coord
, &hint
->lh
,
809 lock_mode
, bias
, LEAF_LEVEL
, LEAF_LEVEL
,
810 CBK_UNIQUE
| flags
, ra_info
);
811 if (cbk_errored(result
))
813 if(result
== CBK_COORD_FOUND
)
814 dclust_inc_extension_ncount(hint
);
815 hint_set_valid(hint
);
819 /* This function is called by deflate[inflate] manager when
820 creating a transformed/plain stream to check if we should
821 create/cut some overhead. If this returns true, then @oh
822 contains the size of this overhead.
824 static int need_cut_or_align(struct inode
* inode
,
825 struct cluster_handle
* ch
, rw_op rw
, int * oh
)
827 struct tfm_cluster
* tc
= &ch
->tc
;
829 case WRITE_OP
: /* estimate align */
830 *oh
= tc
->len
% cipher_blocksize(inode
);
834 case READ_OP
: /* estimate cut */
835 *oh
= *(tfm_output_data(ch
) + tc
->len
- 1);
838 impossible("edward-1401", "bad option");
840 return (tc
->len
!= tc
->lsize
);
843 /* create/cut an overhead of transformed/plain stream */
844 static void align_or_cut_overhead(struct inode
* inode
,
845 struct cluster_handle
* ch
, rw_op rw
)
848 cipher_plugin
* cplug
= inode_cipher_plugin(inode
);
850 assert("edward-1402", need_cipher(inode
));
852 if (!need_cut_or_align(inode
, ch
, rw
, &oh
))
855 case WRITE_OP
: /* do align */
857 cplug
->align_stream(tfm_input_data(ch
) +
858 ch
->tc
.len
, ch
->tc
.len
,
859 cipher_blocksize(inode
));
860 *(tfm_input_data(ch
) + ch
->tc
.len
- 1) =
861 cipher_blocksize(inode
) - oh
;
863 case READ_OP
: /* do cut */
864 assert("edward-1403", oh
<= cipher_blocksize(inode
));
868 impossible("edward-1404", "bad option");
873 static unsigned max_cipher_overhead(struct inode
* inode
)
875 if (!need_cipher(inode
) || !inode_cipher_plugin(inode
)->align_stream
)
877 return cipher_blocksize(inode
);
880 static int deflate_overhead(struct inode
*inode
)
882 return (inode_compression_plugin(inode
)->
883 checksum
? DC_CHECKSUM_SIZE
: 0);
886 static unsigned deflate_overrun(struct inode
* inode
, int ilen
)
888 return coa_overrun(inode_compression_plugin(inode
), ilen
);
891 /* Estimating compressibility of a logical cluster by various
892 policies represented by compression mode plugin.
893 If this returns false, then compressor won't be called for
894 the cluster of index @index.
896 static int should_compress(struct tfm_cluster
* tc
, cloff_t index
,
899 compression_plugin
*cplug
= inode_compression_plugin(inode
);
900 compression_mode_plugin
*mplug
= inode_compression_mode_plugin(inode
);
902 assert("edward-1321", tc
->len
!= 0);
903 assert("edward-1322", cplug
!= NULL
);
904 assert("edward-1323", mplug
!= NULL
);
906 return /* estimate by size */
907 (cplug
->min_size_deflate
?
908 tc
->len
>= cplug
->min_size_deflate() :
910 /* estimate by compression mode plugin */
911 (mplug
->should_deflate
?
912 mplug
->should_deflate(inode
, index
) :
916 /* Evaluating results of compression transform.
917 Returns true, if we need to accept this results */
918 static int save_compressed(int size_before
, int size_after
, struct inode
*inode
)
920 return (size_after
+ deflate_overhead(inode
) +
921 max_cipher_overhead(inode
) < size_before
);
924 /* Guess result of the evaluation above */
925 static int need_inflate(struct cluster_handle
* ch
, struct inode
* inode
,
926 int encrypted
/* is cluster encrypted */ )
928 struct tfm_cluster
* tc
= &ch
->tc
;
930 assert("edward-142", tc
!= 0);
931 assert("edward-143", inode
!= NULL
);
935 inode_scaled_offset(inode
, tc
->lsize
) :
939 /* If results of compression were accepted, then we add
940 a checksum to catch possible disk cluster corruption.
941 The following is a format of the data stored in disk clusters:
943 data This is (transformed) logical cluster.
944 cipher_overhead This is created by ->align() method
945 of cipher plugin. May be absent.
946 checksum (4) This is created by ->checksum method
947 of compression plugin to check
948 integrity. May be absent.
950 Crypto overhead format:
953 control_byte (1) contains aligned overhead size:
954 1 <= overhead <= cipher_blksize
956 /* Append a checksum at the end of a transformed stream */
957 static void dc_set_checksum(compression_plugin
* cplug
, struct tfm_cluster
* tc
)
961 assert("edward-1309", tc
!= NULL
);
962 assert("edward-1310", tc
->len
> 0);
963 assert("edward-1311", cplug
->checksum
!= NULL
);
965 checksum
= cplug
->checksum(tfm_stream_data(tc
, OUTPUT_STREAM
), tc
->len
);
966 put_unaligned(cpu_to_le32(checksum
),
967 (d32
*)(tfm_stream_data(tc
, OUTPUT_STREAM
) + tc
->len
));
968 tc
->len
+= (int)DC_CHECKSUM_SIZE
;
971 /* Check a disk cluster checksum.
972 Returns 0 if checksum is correct, otherwise returns 1 */
973 static int dc_check_checksum(compression_plugin
* cplug
, struct tfm_cluster
* tc
)
975 assert("edward-1312", tc
!= NULL
);
976 assert("edward-1313", tc
->len
> (int)DC_CHECKSUM_SIZE
);
977 assert("edward-1314", cplug
->checksum
!= NULL
);
979 if (cplug
->checksum(tfm_stream_data(tc
, INPUT_STREAM
),
980 tc
->len
- (int)DC_CHECKSUM_SIZE
) !=
981 le32_to_cpu(get_unaligned((d32
*)
982 (tfm_stream_data(tc
, INPUT_STREAM
)
983 + tc
->len
- (int)DC_CHECKSUM_SIZE
)))) {
984 warning("edward-156",
985 "Bad disk cluster checksum %d, (should be %d) Fsck?\n",
987 (get_unaligned((d32
*)
988 (tfm_stream_data(tc
, INPUT_STREAM
) +
989 tc
->len
- (int)DC_CHECKSUM_SIZE
))),
991 (tfm_stream_data(tc
, INPUT_STREAM
),
992 tc
->len
- (int)DC_CHECKSUM_SIZE
));
995 tc
->len
-= (int)DC_CHECKSUM_SIZE
;
999 /* get input/output stream for some transform action */
1000 int grab_tfm_stream(struct inode
* inode
, struct tfm_cluster
* tc
,
1003 size_t size
= inode_scaled_cluster_size(inode
);
1005 assert("edward-901", tc
!= NULL
);
1006 assert("edward-1027", inode_compression_plugin(inode
) != NULL
);
1008 if (cluster_get_tfm_act(tc
) == TFMA_WRITE
)
1009 size
+= deflate_overrun(inode
, inode_cluster_size(inode
));
1011 if (!get_tfm_stream(tc
, id
) && id
== INPUT_STREAM
)
1012 alternate_streams(tc
);
1013 if (!get_tfm_stream(tc
, id
))
1014 return alloc_tfm_stream(tc
, size
, id
);
1016 assert("edward-902", tfm_stream_is_set(tc
, id
));
1018 if (tfm_stream_size(tc
, id
) < size
)
1019 return realloc_tfm_stream(tc
, size
, id
);
1023 /* Common deflate manager */
1024 int reiser4_deflate_cluster(struct cluster_handle
* clust
, struct inode
* inode
)
1029 struct tfm_cluster
* tc
= &clust
->tc
;
1030 compression_plugin
* coplug
;
1032 assert("edward-401", inode
!= NULL
);
1033 assert("edward-903", tfm_stream_is_set(tc
, INPUT_STREAM
));
1034 assert("edward-1348", cluster_get_tfm_act(tc
) == TFMA_WRITE
);
1035 assert("edward-498", !tfm_cluster_is_uptodate(tc
));
1037 coplug
= inode_compression_plugin(inode
);
1038 if (should_compress(tc
, clust
->index
, inode
)) {
1039 /* try to compress, discard bad results */
1041 compression_mode_plugin
* mplug
=
1042 inode_compression_mode_plugin(inode
);
1043 assert("edward-602", coplug
!= NULL
);
1044 assert("edward-1423", coplug
->compress
!= NULL
);
1046 result
= grab_coa(tc
, coplug
);
1048 warning("edward-1424",
1049 "alloc_coa failed with ret=%d, skipped compression",
1053 result
= grab_tfm_stream(inode
, tc
, OUTPUT_STREAM
);
1055 warning("edward-1425",
1056 "alloc stream failed with ret=%d, skipped compression",
1060 dst_len
= tfm_stream_size(tc
, OUTPUT_STREAM
);
1061 coplug
->compress(get_coa(tc
, coplug
->h
.id
, tc
->act
),
1062 tfm_input_data(clust
), tc
->len
,
1063 tfm_output_data(clust
), &dst_len
);
1064 /* make sure we didn't overwrite extra bytes */
1065 assert("edward-603",
1066 dst_len
<= tfm_stream_size(tc
, OUTPUT_STREAM
));
1068 /* evaluate results of compression transform */
1069 if (save_compressed(tc
->len
, dst_len
, inode
)) {
1070 /* good result, accept */
1072 if (mplug
->accept_hook
!= NULL
) {
1073 result
= mplug
->accept_hook(inode
, clust
->index
);
1075 warning("edward-1426",
1076 "accept_hook failed with ret=%d",
1082 /* bad result, discard */
1084 if (cluster_is_complete(clust
, inode
))
1085 warning("edward-1496",
1086 "incompressible cluster %lu (inode %llu)",
1088 (unsigned long long)get_inode_oid(inode
));
1090 if (mplug
->discard_hook
!= NULL
&&
1091 cluster_is_complete(clust
, inode
)) {
1092 result
= mplug
->discard_hook(inode
,
1095 warning("edward-1427",
1096 "discard_hook failed with ret=%d",
1102 if (need_cipher(inode
)) {
1103 cipher_plugin
* ciplug
;
1104 struct blkcipher_desc desc
;
1105 struct scatterlist src
;
1106 struct scatterlist dst
;
1108 ciplug
= inode_cipher_plugin(inode
);
1109 desc
.tfm
= info_get_cipher(inode_crypto_info(inode
));
1112 alternate_streams(tc
);
1113 result
= grab_tfm_stream(inode
, tc
, OUTPUT_STREAM
);
1117 align_or_cut_overhead(inode
, clust
, WRITE_OP
);
1118 sg_init_one(&src
, tfm_input_data(clust
), tc
->len
);
1119 sg_init_one(&dst
, tfm_output_data(clust
), tc
->len
);
1121 result
= crypto_blkcipher_encrypt(&desc
, &dst
, &src
, tc
->len
);
1123 warning("edward-1405",
1124 "encryption failed flags=%x\n", desc
.flags
);
1129 if (compressed
&& coplug
->checksum
!= NULL
)
1130 dc_set_checksum(coplug
, tc
);
1131 if (!compressed
&& !encrypted
)
1132 alternate_streams(tc
);
1136 /* Common inflate manager. */
1137 int reiser4_inflate_cluster(struct cluster_handle
* clust
, struct inode
* inode
)
1140 int transformed
= 0;
1141 struct tfm_cluster
* tc
= &clust
->tc
;
1142 compression_plugin
* coplug
;
1144 assert("edward-905", inode
!= NULL
);
1145 assert("edward-1178", clust
->dstat
== PREP_DISK_CLUSTER
);
1146 assert("edward-906", tfm_stream_is_set(&clust
->tc
, INPUT_STREAM
));
1147 assert("edward-1349", tc
->act
== TFMA_READ
);
1148 assert("edward-907", !tfm_cluster_is_uptodate(tc
));
1150 /* Handle a checksum (if any) */
1151 coplug
= inode_compression_plugin(inode
);
1152 if (need_inflate(clust
, inode
, need_cipher(inode
)) &&
1153 coplug
->checksum
!= NULL
) {
1154 result
= dc_check_checksum(coplug
, tc
);
1155 if (unlikely(result
)) {
1156 warning("edward-1460",
1157 "Inode %llu: disk cluster %lu looks corrupted",
1158 (unsigned long long)get_inode_oid(inode
),
1160 return RETERR(-EIO
);
1163 if (need_cipher(inode
)) {
1164 cipher_plugin
* ciplug
;
1165 struct blkcipher_desc desc
;
1166 struct scatterlist src
;
1167 struct scatterlist dst
;
1169 ciplug
= inode_cipher_plugin(inode
);
1170 desc
.tfm
= info_get_cipher(inode_crypto_info(inode
));
1172 result
= grab_tfm_stream(inode
, tc
, OUTPUT_STREAM
);
1175 assert("edward-909", tfm_cluster_is_set(tc
));
1177 sg_init_one(&src
, tfm_input_data(clust
), tc
->len
);
1178 sg_init_one(&dst
, tfm_output_data(clust
), tc
->len
);
1180 result
= crypto_blkcipher_decrypt(&desc
, &dst
, &src
, tc
->len
);
1182 warning("edward-1600", "decrypt failed flags=%x\n",
1186 align_or_cut_overhead(inode
, clust
, READ_OP
);
1189 if (need_inflate(clust
, inode
, 0)) {
1190 unsigned dst_len
= inode_cluster_size(inode
);
1192 alternate_streams(tc
);
1194 result
= grab_tfm_stream(inode
, tc
, OUTPUT_STREAM
);
1197 assert("edward-1305", coplug
->decompress
!= NULL
);
1198 assert("edward-910", tfm_cluster_is_set(tc
));
1200 coplug
->decompress(get_coa(tc
, coplug
->h
.id
, tc
->act
),
1201 tfm_input_data(clust
), tc
->len
,
1202 tfm_output_data(clust
), &dst_len
);
1205 assert("edward-157", dst_len
== tc
->lsize
);
1209 alternate_streams(tc
);
1213 /* This is implementation of readpage method of struct
1214 address_space_operations for cryptcompress plugin. */
1215 int readpage_cryptcompress(struct file
*file
, struct page
*page
)
1217 reiser4_context
*ctx
;
1218 struct cluster_handle clust
;
1222 assert("edward-88", PageLocked(page
));
1223 assert("vs-976", !PageUptodate(page
));
1224 assert("edward-89", page
->mapping
&& page
->mapping
->host
);
1226 ctx
= reiser4_init_context(page
->mapping
->host
->i_sb
);
1229 return PTR_ERR(ctx
);
1231 assert("edward-113",
1233 page
->mapping
== file
->f_dentry
->d_inode
->i_mapping
));
1235 if (PageUptodate(page
)) {
1236 warning("edward-1338", "page is already uptodate\n");
1238 reiser4_exit_context(ctx
);
1241 cluster_init_read(&clust
, NULL
);
1243 iplug
= item_plugin_by_id(CTAIL_ID
);
1244 if (!iplug
->s
.file
.readpage
) {
1246 put_cluster_handle(&clust
);
1247 reiser4_exit_context(ctx
);
1250 result
= iplug
->s
.file
.readpage(&clust
, page
);
1252 put_cluster_handle(&clust
);
1253 reiser4_txn_restart(ctx
);
1254 reiser4_exit_context(ctx
);
1258 /* number of pages to check in */
1259 static int get_new_nrpages(struct cluster_handle
* clust
)
1261 switch (clust
->op
) {
1263 return clust
->nr_pages
;
1265 assert("edward-1179", clust
->win
!= NULL
);
1266 return size_in_pages(clust
->win
->off
+ clust
->win
->count
);
1268 impossible("edward-1180", "bad page cluster option");
1273 static void set_cluster_pages_dirty(struct cluster_handle
* clust
,
1274 struct inode
* inode
)
1278 int nrpages
= get_new_nrpages(clust
);
1280 for (i
= 0; i
< nrpages
; i
++) {
1282 pg
= clust
->pages
[i
];
1283 assert("edward-968", pg
!= NULL
);
1285 assert("edward-1065", PageUptodate(pg
));
1286 reiser4_set_page_dirty_internal(pg
);
1288 mark_page_accessed(pg
);
1292 /* Grab a page cluster for read/write operations.
1293 Attach a jnode for write operations (when preparing for modifications, which
1294 are supposed to be committed).
1296 We allocate only one jnode per page cluster; this jnode is binded to the
1297 first page of this cluster, so we have an extra-reference that will be put
1298 as soon as jnode is evicted from memory), other references will be cleaned
1299 up in flush time (assume that check in page cluster was successful).
1301 int grab_page_cluster(struct inode
* inode
,
1302 struct cluster_handle
* clust
, rw_op rw
)
1308 assert("edward-182", clust
!= NULL
);
1309 assert("edward-183", clust
->pages
!= NULL
);
1310 assert("edward-1466", clust
->node
== NULL
);
1311 assert("edward-1428", inode
!= NULL
);
1312 assert("edward-1429", inode
->i_mapping
!= NULL
);
1313 assert("edward-184", clust
->nr_pages
<= cluster_nrpages(inode
));
1315 if (clust
->nr_pages
== 0)
1318 for (i
= 0; i
< clust
->nr_pages
; i
++) {
1320 assert("edward-1044", clust
->pages
[i
] == NULL
);
1323 find_or_create_page(inode
->i_mapping
,
1324 clust_to_pg(clust
->index
, inode
) + i
,
1325 reiser4_ctx_gfp_mask_get());
1326 if (!clust
->pages
[i
]) {
1327 result
= RETERR(-ENOMEM
);
1330 if (i
== 0 && rw
== WRITE_OP
) {
1331 node
= jnode_of_page(clust
->pages
[i
]);
1333 result
= PTR_ERR(node
);
1334 unlock_page(clust
->pages
[i
]);
1337 JF_SET(node
, JNODE_CLUSTER_PAGE
);
1338 assert("edward-920", jprivate(clust
->pages
[0]));
1340 INODE_PGCOUNT_INC(inode
);
1341 unlock_page(clust
->pages
[i
]);
1343 if (unlikely(result
)) {
1345 put_cluster_page(clust
->pages
[--i
]);
1346 INODE_PGCOUNT_DEC(inode
);
1348 if (node
&& !IS_ERR(node
))
1356 static void truncate_page_cluster_range(struct inode
* inode
,
1357 struct page
** pages
,
1359 int from
, int count
,
1362 assert("edward-1467", count
> 0);
1363 reiser4_invalidate_pages(inode
->i_mapping
,
1364 clust_to_pg(index
, inode
) + from
,
1368 /* Put @count pages starting from @from offset */
1369 static void __put_page_cluster(int from
, int count
,
1370 struct page
** pages
, struct inode
* inode
)
1373 assert("edward-1468", pages
!= NULL
);
1374 assert("edward-1469", inode
!= NULL
);
1375 assert("edward-1470", from
>= 0 && count
>= 0);
1377 for (i
= 0; i
< count
; i
++) {
1378 assert("edward-1471", pages
[from
+ i
] != NULL
);
1379 assert("edward-1472",
1380 pages
[from
+ i
]->index
== pages
[from
]->index
+ i
);
1382 put_cluster_page(pages
[from
+ i
]);
1383 INODE_PGCOUNT_DEC(inode
);
1388 * This is dual to grab_page_cluster,
1389 * however if @rw == WRITE_OP, then we call this function
1390 * only if something is failed before checkin page cluster.
1392 void put_page_cluster(struct cluster_handle
* clust
,
1393 struct inode
* inode
, rw_op rw
)
1395 assert("edward-445", clust
!= NULL
);
1396 assert("edward-922", clust
->pages
!= NULL
);
1397 assert("edward-446",
1398 ergo(clust
->nr_pages
!= 0, clust
->pages
[0] != NULL
));
1400 __put_page_cluster(0, clust
->nr_pages
, clust
->pages
, inode
);
1401 if (rw
== WRITE_OP
) {
1402 if (unlikely(clust
->node
)) {
1403 assert("edward-447",
1404 clust
->node
== jprivate(clust
->pages
[0]));
1412 int cryptcompress_inode_ok(struct inode
*inode
)
1414 if (!(reiser4_inode_data(inode
)->plugin_mask
& (1 << PSET_FILE
)))
1416 if (!cluster_shift_ok(inode_cluster_shift(inode
)))
1421 static int window_ok(struct reiser4_slide
* win
, struct inode
*inode
)
1423 assert("edward-1115", win
!= NULL
);
1424 assert("edward-1116", ergo(win
->delta
, win
->stat
== HOLE_WINDOW
));
1426 return (win
->off
!= inode_cluster_size(inode
)) &&
1427 (win
->off
+ win
->count
+ win
->delta
<= inode_cluster_size(inode
));
1430 static int cluster_ok(struct cluster_handle
* clust
, struct inode
*inode
)
1432 assert("edward-279", clust
!= NULL
);
1436 return (clust
->win
? window_ok(clust
->win
, inode
) : 1);
1439 static int pages_truncate_ok(struct inode
*inode
, pgoff_t start
)
1444 found
= find_get_pages(inode
->i_mapping
, start
, 1, &page
);
1446 put_cluster_page(page
);
1450 #define pages_truncate_ok(inode, start) 1
1453 static int jnode_truncate_ok(struct inode
*inode
, cloff_t index
)
1456 node
= jlookup(current_tree
, get_inode_oid(inode
),
1457 clust_to_pg(index
, inode
));
1464 static int find_fake_appended(struct inode
*inode
, cloff_t
* index
);
1466 static int body_truncate_ok(struct inode
*inode
, cloff_t aidx
)
1471 result
= find_fake_appended(inode
, &raidx
);
1472 return !result
&& (aidx
== raidx
);
1476 /* guess next window stat */
1477 static inline window_stat
next_window_stat(struct reiser4_slide
* win
)
1479 assert("edward-1130", win
!= NULL
);
1480 return ((win
->stat
== HOLE_WINDOW
&& win
->delta
== 0) ?
1481 HOLE_WINDOW
: DATA_WINDOW
);
1484 /* guess and set next cluster index and window params */
1485 static void move_update_window(struct inode
* inode
,
1486 struct cluster_handle
* clust
,
1487 loff_t file_off
, loff_t to_file
)
1489 struct reiser4_slide
* win
;
1491 assert("edward-185", clust
!= NULL
);
1492 assert("edward-438", clust
->pages
!= NULL
);
1493 assert("edward-281", cluster_ok(clust
, inode
));
1499 switch (win
->stat
) {
1503 win
->stat
= DATA_WINDOW
;
1505 win
->count
= min((loff_t
)inode_cluster_size(inode
), to_file
);
1508 switch (next_window_stat(win
)) {
1511 clust
->index
= off_to_clust(file_off
, inode
);
1512 win
->stat
= HOLE_WINDOW
;
1514 win
->count
= off_to_cloff(file_off
, inode
);
1515 win
->delta
= min((loff_t
)(inode_cluster_size(inode
) -
1516 win
->count
), to_file
);
1520 win
->stat
= DATA_WINDOW
;
1521 /* off+count+delta=inv */
1522 win
->off
= win
->off
+ win
->count
;
1523 win
->count
= win
->delta
;
1527 impossible("edward-282", "wrong next window state");
1531 impossible("edward-283", "wrong current window state");
1533 assert("edward-1068", cluster_ok(clust
, inode
));
1536 static int update_sd_cryptcompress(struct inode
*inode
)
1540 assert("edward-978", reiser4_schedulable());
1542 result
= reiser4_grab_space_force(/* one for stat data update */
1543 estimate_update_common(inode
),
1547 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
1548 result
= reiser4_update_sd(inode
);
1553 static void uncapture_cluster_jnode(jnode
* node
)
1557 assert_spin_locked(&(node
->guard
));
1559 atom
= jnode_get_atom(node
);
1561 assert("jmacd-7111", !JF_ISSET(node
, JNODE_DIRTY
));
1562 spin_unlock_jnode(node
);
1565 reiser4_uncapture_block(node
);
1566 spin_unlock_atom(atom
);
1570 static void put_found_pages(struct page
**pages
, int nr
)
1573 for (i
= 0; i
< nr
; i
++) {
1574 assert("edward-1045", pages
[i
] != NULL
);
1575 put_cluster_page(pages
[i
]);
1579 /* Lifecycle of a logical cluster in the system.
1582 * Logical cluster of a cryptcompress file is represented in the system by
1583 * . page cluster (in memory, primary cache, contains plain text);
1584 * . disk cluster (in memory, secondary cache, contains transformed text).
1585 * Primary cache is to reduce number of transform operations (compression,
1586 * encryption), i.e. to implement transform-caching strategy.
1587 * Secondary cache is to reduce number of I/O operations, i.e. for usual
1588 * write-caching strategy. Page cluster is a set of pages, i.e. mapping of
1589 * a logical cluster to the primary cache. Disk cluster is a set of items
1590 * of the same type defined by some reiser4 item plugin id.
1592 * 1. Performing modifications
1594 * Every modification of a cryptcompress file is considered as a set of
1595 * operations performed on file's logical clusters. Every such "atomic"
1596 * modification is truncate, append and(or) overwrite some bytes of a
1597 * logical cluster performed in the primary cache with the following
1598 * synchronization with the secondary cache (in flush time). Disk clusters,
1599 * which live in the secondary cache, are supposed to be synchronized with
1600 * disk. The mechanism of synchronization of primary and secondary caches
1601 * includes so-called checkin/checkout technique described below.
1603 * 2. Submitting modifications
1605 * Each page cluster has associated jnode (a special in-memory header to
1606 * keep a track of transactions in reiser4), which is attached to its first
1607 * page when grabbing page cluster for modifications (see grab_page_cluster).
1608 * Submitting modifications (see checkin_logical_cluster) is going per logical
1609 * cluster and includes:
1610 * . checkin_cluster_size;
1611 * . checkin_page_cluster.
1612 * checkin_cluster_size() is resolved to file size update (which completely
1613 * defines new size of logical cluster (number of file's bytes in a logical
1615 * checkin_page_cluster() captures jnode of a page cluster and installs
1616 * jnode's dirty flag (if needed) to indicate that modifications are
1617 * successfully checked in.
1619 * 3. Checking out modifications
1621 * Is going per logical cluster in flush time (see checkout_logical_cluster).
1622 * This is the time of synchronizing primary and secondary caches.
1623 * checkout_logical_cluster() includes:
1624 * . checkout_page_cluster (retrieving checked in pages).
1625 * . uncapture jnode (including clear dirty flag and unlock)
1627 * 4. Committing modifications
1629 * Proceeding a synchronization of primary and secondary caches. When checking
1630 * out page cluster (the phase above) pages are locked/flushed/unlocked
1631 * one-by-one in ascending order of their indexes to contiguous stream, which
1632 * is supposed to be transformed (compressed, encrypted), chopped up into items
1633 * and committed to disk as a disk cluster.
1635 * 5. Managing page references
1637 * Every checked in page have a special additional "control" reference,
1638 * which is dropped at checkout. We need this to avoid unexpected evicting
1639 * pages from memory before checkout. Control references are managed so
1640 * they are not accumulated with every checkin:
1651 * Every page cluster has its own unique "cluster lock". Update/drop
1652 * references are serialized via this lock. Number of checked in cluster
1653 * pages is calculated by i_size under cluster lock. File size is updated
1654 * at every checkin action also under cluster lock (except cases of
1655 * appending/truncating fake logical clusters).
1657 * Proof of correctness:
1659 * Since we update file size under cluster lock, in the case of non-fake
1660 * logical cluster with its lock held we do have expected number of checked
1661 * in pages. On the other hand, append/truncate of fake logical clusters
1662 * doesn't change number of checked in pages of any cluster.
1664 * NOTE-EDWARD: As cluster lock we use guard (spinlock_t) of its jnode.
1665 * Currently, I don't see any reason to create a special lock for those
1669 static inline void lock_cluster(jnode
* node
)
1671 spin_lock_jnode(node
);
1674 static inline void unlock_cluster(jnode
* node
)
1676 spin_unlock_jnode(node
);
1679 static inline void unlock_cluster_uncapture(jnode
* node
)
1681 uncapture_cluster_jnode(node
);
1684 /* Set new file size by window. Cluster lock is required. */
1685 static void checkin_file_size(struct cluster_handle
* clust
,
1686 struct inode
* inode
)
1689 struct reiser4_slide
* win
;
1691 assert("edward-1181", clust
!= NULL
);
1692 assert("edward-1182", inode
!= NULL
);
1693 assert("edward-1473", clust
->pages
!= NULL
);
1694 assert("edward-1474", clust
->pages
[0] != NULL
);
1695 assert("edward-1475", jprivate(clust
->pages
[0]) != NULL
);
1696 assert_spin_locked(&(jprivate(clust
->pages
[0])->guard
));
1700 assert("edward-1183", win
!= NULL
);
1702 new_size
= clust_to_off(clust
->index
, inode
) + win
->off
;
1704 switch (clust
->op
) {
1706 if (new_size
+ win
->count
<= i_size_read(inode
))
1707 /* overwrite only */
1709 new_size
+= win
->count
;
1714 impossible("edward-1184", "bad page cluster option");
1717 inode_check_scale_nolock(inode
, i_size_read(inode
), new_size
);
1718 i_size_write(inode
, new_size
);
1722 static inline void checkin_cluster_size(struct cluster_handle
* clust
,
1723 struct inode
* inode
)
1726 checkin_file_size(clust
, inode
);
1729 static int checkin_page_cluster(struct cluster_handle
* clust
,
1730 struct inode
* inode
)
1734 int old_nrpages
= clust
->old_nrpages
;
1735 int new_nrpages
= get_new_nrpages(clust
);
1739 assert("edward-221", node
!= NULL
);
1740 assert("edward-971", clust
->reserved
== 1);
1741 assert("edward-1263",
1742 clust
->reserved_prepped
== estimate_update_cluster(inode
));
1743 assert("edward-1264", clust
->reserved_unprepped
== 0);
1745 if (JF_ISSET(node
, JNODE_DIRTY
)) {
1747 * page cluster was checked in, but not yet
1748 * checked out, so release related resources
1750 free_reserved4cluster(inode
, clust
,
1751 estimate_update_cluster(inode
));
1752 __put_page_cluster(0, clust
->old_nrpages
,
1753 clust
->pages
, inode
);
1755 result
= capture_cluster_jnode(node
);
1756 if (unlikely(result
)) {
1757 unlock_cluster(node
);
1760 jnode_make_dirty_locked(node
);
1761 clust
->reserved
= 0;
1763 unlock_cluster(node
);
1765 if (new_nrpages
< old_nrpages
) {
1766 /* truncate >= 1 complete pages */
1767 __put_page_cluster(new_nrpages
,
1768 old_nrpages
- new_nrpages
,
1769 clust
->pages
, inode
);
1770 truncate_page_cluster_range(inode
,
1771 clust
->pages
, clust
->index
,
1773 old_nrpages
- new_nrpages
,
1777 clust
->reserved_prepped
-= estimate_update_cluster(inode
);
1782 /* Submit modifications of a logical cluster */
1783 static int checkin_logical_cluster(struct cluster_handle
* clust
,
1784 struct inode
*inode
)
1791 assert("edward-1035", node
!= NULL
);
1792 assert("edward-1029", clust
!= NULL
);
1793 assert("edward-1030", clust
->reserved
== 1);
1794 assert("edward-1031", clust
->nr_pages
!= 0);
1795 assert("edward-1032", clust
->pages
!= NULL
);
1796 assert("edward-1033", clust
->pages
[0] != NULL
);
1797 assert("edward-1446", jnode_is_cluster_page(node
));
1798 assert("edward-1476", node
== jprivate(clust
->pages
[0]));
1801 checkin_cluster_size(clust
, inode
);
1802 /* this will unlock cluster */
1803 result
= checkin_page_cluster(clust
, inode
);
1810 * Retrieve size of logical cluster that was checked in at
1811 * the latest modifying session (cluster lock is required)
1813 static inline void checkout_cluster_size(struct cluster_handle
* clust
,
1814 struct inode
* inode
)
1816 struct tfm_cluster
*tc
= &clust
->tc
;
1818 tc
->len
= lbytes(clust
->index
, inode
);
1819 assert("edward-1478", tc
->len
!= 0);
1823 * Retrieve a page cluster with the latest submitted modifications
1824 * and flush its pages to previously allocated contiguous stream.
1826 static void checkout_page_cluster(struct cluster_handle
* clust
,
1827 jnode
* node
, struct inode
* inode
)
1832 struct tfm_cluster
*tc
= &clust
->tc
;
1834 /* find and put checked in pages: cluster is locked,
1835 * so we must get expected number (to_put) of pages
1837 to_put
= size_in_pages(lbytes(clust
->index
, inode
));
1838 found
= find_get_pages(inode
->i_mapping
,
1839 clust_to_pg(clust
->index
, inode
),
1840 to_put
, clust
->pages
);
1841 BUG_ON(found
!= to_put
);
1843 __put_page_cluster(0, to_put
, clust
->pages
, inode
);
1844 unlock_cluster_uncapture(node
);
1846 /* Flush found pages.
1848 * Note, that we don't disable modifications while flushing,
1849 * moreover, some found pages can be truncated, as we have
1850 * released cluster lock.
1852 for (i
= 0; i
< found
; i
++) {
1855 assert("edward-1479",
1856 clust
->pages
[i
]->index
== clust
->pages
[0]->index
+ i
);
1858 lock_page(clust
->pages
[i
]);
1859 if (!PageUptodate(clust
->pages
[i
])) {
1860 /* page was truncated */
1861 assert("edward-1480",
1862 i_size_read(inode
) <= page_offset(clust
->pages
[i
]));
1863 assert("edward-1481",
1864 clust
->pages
[i
]->mapping
!= inode
->i_mapping
);
1865 unlock_page(clust
->pages
[i
]);
1868 /* Update the number of bytes in the logical cluster,
1869 * as it could be partially truncated. Note, that only
1870 * partial truncate is possible (complete truncate can
1871 * not go here, as it is performed via ->kill_hook()
1872 * called by cut_file_items(), and the last one must
1873 * wait for znode locked with parent coord).
1875 checkout_cluster_size(clust
, inode
);
1877 /* this can be zero, as new file size is
1878 checked in before truncating pages */
1879 in_page
= __mbp(tc
->len
, i
);
1881 data
= kmap(clust
->pages
[i
]);
1882 memcpy(tfm_stream_data(tc
, INPUT_STREAM
) + pg_to_off(i
),
1884 kunmap(clust
->pages
[i
]);
1886 if (PageDirty(clust
->pages
[i
]))
1887 cancel_dirty_page(clust
->pages
[i
], PAGE_CACHE_SIZE
);
1889 unlock_page(clust
->pages
[i
]);
1891 if (in_page
< PAGE_CACHE_SIZE
)
1892 /* end of the file */
1895 put_found_pages(clust
->pages
, found
); /* find_get_pages */
1896 tc
->lsize
= tc
->len
;
1900 /* Check out modifications of a logical cluster */
1901 int checkout_logical_cluster(struct cluster_handle
* clust
,
1902 jnode
* node
, struct inode
*inode
)
1905 struct tfm_cluster
*tc
= &clust
->tc
;
1907 assert("edward-980", node
!= NULL
);
1908 assert("edward-236", inode
!= NULL
);
1909 assert("edward-237", clust
!= NULL
);
1910 assert("edward-240", !clust
->win
);
1911 assert("edward-241", reiser4_schedulable());
1912 assert("edward-718", cryptcompress_inode_ok(inode
));
1914 result
= grab_tfm_stream(inode
, tc
, INPUT_STREAM
);
1916 warning("edward-1430", "alloc stream failed with ret=%d",
1918 return RETERR(-E_REPEAT
);
1922 if (unlikely(!JF_ISSET(node
, JNODE_DIRTY
))) {
1923 /* race with another flush */
1924 warning("edward-982",
1925 "checking out logical cluster %lu of inode %llu: "
1926 "jnode is not dirty", clust
->index
,
1927 (unsigned long long)get_inode_oid(inode
));
1928 unlock_cluster(node
);
1929 return RETERR(-E_REPEAT
);
1931 cluster_reserved2grabbed(estimate_update_cluster(inode
));
1933 /* this will unlock cluster */
1934 checkout_page_cluster(clust
, node
, inode
);
1938 /* set hint for the cluster of the index @index */
1939 static void set_hint_cluster(struct inode
*inode
, hint_t
* hint
,
1940 cloff_t index
, znode_lock_mode mode
)
1943 assert("edward-722", cryptcompress_inode_ok(inode
));
1944 assert("edward-723",
1945 inode_file_plugin(inode
) ==
1946 file_plugin_by_id(CRYPTCOMPRESS_FILE_PLUGIN_ID
));
1948 inode_file_plugin(inode
)->key_by_inode(inode
,
1949 clust_to_off(index
, inode
),
1952 reiser4_seal_init(&hint
->seal
, &hint
->ext_coord
.coord
, &key
);
1953 hint
->offset
= get_key_offset(&key
);
1957 void invalidate_hint_cluster(struct cluster_handle
* clust
)
1959 assert("edward-1291", clust
!= NULL
);
1960 assert("edward-1292", clust
->hint
!= NULL
);
1962 done_lh(&clust
->hint
->lh
);
1963 hint_clr_valid(clust
->hint
);
1966 static void put_hint_cluster(struct cluster_handle
* clust
,
1967 struct inode
*inode
, znode_lock_mode mode
)
1969 assert("edward-1286", clust
!= NULL
);
1970 assert("edward-1287", clust
->hint
!= NULL
);
1972 set_hint_cluster(inode
, clust
->hint
, clust
->index
+ 1, mode
);
1973 invalidate_hint_cluster(clust
);
1976 static int balance_dirty_page_cluster(struct cluster_handle
* clust
,
1977 struct inode
*inode
, loff_t off
,
1981 struct cryptcompress_info
* info
;
1983 assert("edward-724", inode
!= NULL
);
1984 assert("edward-725", cryptcompress_inode_ok(inode
));
1986 /* set next window params */
1987 move_update_window(inode
, clust
, off
, to_file
);
1989 result
= update_sd_cryptcompress(inode
);
1992 assert("edward-726", clust
->hint
->lh
.owner
== NULL
);
1993 info
= cryptcompress_inode_data(inode
);
1995 mutex_unlock(&info
->checkin_mutex
);
1996 reiser4_throttle_write(inode
);
1997 mutex_lock(&info
->checkin_mutex
);
2001 /* set zeroes to the page cluster, proceed it, and maybe, try to capture
2003 static int write_hole(struct inode
*inode
, struct cluster_handle
* clust
,
2004 loff_t file_off
, loff_t to_file
)
2007 unsigned cl_off
, cl_count
= 0;
2008 unsigned to_pg
, pg_off
;
2009 struct reiser4_slide
* win
;
2011 assert("edward-190", clust
!= NULL
);
2012 assert("edward-1069", clust
->win
!= NULL
);
2013 assert("edward-191", inode
!= NULL
);
2014 assert("edward-727", cryptcompress_inode_ok(inode
));
2015 assert("edward-1171", clust
->dstat
!= INVAL_DISK_CLUSTER
);
2016 assert("edward-1154",
2017 ergo(clust
->dstat
!= FAKE_DISK_CLUSTER
, clust
->reserved
== 1));
2021 assert("edward-1070", win
!= NULL
);
2022 assert("edward-201", win
->stat
== HOLE_WINDOW
);
2023 assert("edward-192", cluster_ok(clust
, inode
));
2025 if (win
->off
== 0 && win
->count
== inode_cluster_size(inode
)) {
2026 /* This part of the hole will be represented by "fake"
2027 * logical cluster, i.e. which doesn't have appropriate
2028 * disk cluster until someone modify this logical cluster
2029 * and make it dirty.
2030 * So go forward here..
2032 move_update_window(inode
, clust
, file_off
, to_file
);
2035 cl_count
= win
->count
; /* number of zeroes to write */
2037 pg_off
= off_to_pgoff(win
->off
);
2041 page
= clust
->pages
[off_to_pg(cl_off
)];
2043 assert("edward-284", page
!= NULL
);
2045 to_pg
= min((typeof(pg_off
))PAGE_CACHE_SIZE
- pg_off
, cl_count
);
2047 zero_user(page
, pg_off
, to_pg
);
2048 SetPageUptodate(page
);
2049 reiser4_set_page_dirty_internal(page
);
2050 mark_page_accessed(page
);
2058 /* only zeroes in this window, try to capture
2060 result
= checkin_logical_cluster(clust
, inode
);
2063 put_hint_cluster(clust
, inode
, ZNODE_WRITE_LOCK
);
2065 balance_dirty_page_cluster(clust
, inode
, file_off
, to_file
);
2067 move_update_window(inode
, clust
, file_off
, to_file
);
2072 The main disk search procedure for cryptcompress plugin, which
2073 . scans all items of disk cluster with the lock mode @mode
2074 . maybe reads each one (if @read)
2075 . maybe makes its znode dirty (if write lock mode was specified)
2077 NOTE-EDWARD: Callers should handle the case when disk cluster
2078 is incomplete (-EIO)
2080 int find_disk_cluster(struct cluster_handle
* clust
,
2081 struct inode
*inode
, int read
, znode_lock_mode mode
)
2090 struct tfm_cluster
*tc
;
2091 struct cryptcompress_info
* info
;
2093 assert("edward-138", clust
!= NULL
);
2094 assert("edward-728", clust
->hint
!= NULL
);
2095 assert("edward-226", reiser4_schedulable());
2096 assert("edward-137", inode
!= NULL
);
2097 assert("edward-729", cryptcompress_inode_ok(inode
));
2100 fplug
= inode_file_plugin(inode
);
2101 was_grabbed
= get_current_context()->grabbed_blocks
;
2102 info
= cryptcompress_inode_data(inode
);
2105 assert("edward-462", !tfm_cluster_is_uptodate(tc
));
2106 assert("edward-461", ergo(read
, tfm_stream_is_set(tc
, INPUT_STREAM
)));
2108 dclust_init_extension(hint
);
2110 /* set key of the first disk cluster item */
2111 fplug
->flow_by_inode(inode
,
2112 (read
? (char __user
*)tfm_stream_data(tc
, INPUT_STREAM
) : NULL
),
2113 0 /* kernel space */ ,
2114 inode_scaled_cluster_size(inode
),
2115 clust_to_off(clust
->index
, inode
), READ_OP
, &f
);
2116 if (mode
== ZNODE_WRITE_LOCK
) {
2117 /* reserve for flush to make dirty all the leaf nodes
2118 which contain disk cluster */
2120 reiser4_grab_space_force(estimate_dirty_cluster(inode
),
2126 ra_info
.key_to_stop
= f
.key
;
2127 set_key_offset(&ra_info
.key_to_stop
, get_key_offset(reiser4_max_key()));
2130 result
= find_cluster_item(hint
, &f
.key
, mode
,
2132 (mode
== ZNODE_WRITE_LOCK
?
2133 CBK_FOR_INSERT
: 0));
2135 case CBK_COORD_NOTFOUND
:
2137 if (inode_scaled_offset
2138 (inode
, clust_to_off(clust
->index
, inode
)) ==
2139 get_key_offset(&f
.key
)) {
2140 /* first item not found, this is treated
2141 as disk cluster is absent */
2142 clust
->dstat
= FAKE_DISK_CLUSTER
;
2145 /* we are outside the cluster, stop search here */
2146 assert("edward-146",
2147 f
.length
!= inode_scaled_cluster_size(inode
));
2149 case CBK_COORD_FOUND
:
2150 assert("edward-148",
2151 hint
->ext_coord
.coord
.between
== AT_UNIT
);
2152 assert("edward-460",
2153 hint
->ext_coord
.coord
.unit_pos
== 0);
2155 coord_clear_iplug(&hint
->ext_coord
.coord
);
2156 result
= zload_ra(hint
->ext_coord
.coord
.node
, &ra_info
);
2157 if (unlikely(result
))
2159 iplug
= item_plugin_by_coord(&hint
->ext_coord
.coord
);
2160 assert("edward-147",
2161 item_id_by_coord(&hint
->ext_coord
.coord
) ==
2164 result
= iplug
->s
.file
.read(NULL
, &f
, hint
);
2166 zrelse(hint
->ext_coord
.coord
.node
);
2169 if (mode
== ZNODE_WRITE_LOCK
) {
2170 /* Don't make dirty more nodes then it was
2171 estimated (see comments before
2172 estimate_dirty_cluster). Missed nodes will be
2173 read up in flush time if they are evicted from
2175 if (dclust_get_extension_ncount(hint
) <=
2176 estimate_dirty_cluster(inode
))
2177 znode_make_dirty(hint
->ext_coord
.coord
.node
);
2179 znode_set_convertible(hint
->ext_coord
.coord
.
2182 zrelse(hint
->ext_coord
.coord
.node
);
2189 /* at least one item was found */
2190 /* NOTE-EDWARD: Callers should handle the case
2191 when disk cluster is incomplete (-EIO) */
2192 tc
->len
= inode_scaled_cluster_size(inode
) - f
.length
;
2193 tc
->lsize
= lbytes(clust
->index
, inode
);
2194 assert("edward-1196", tc
->len
> 0);
2195 assert("edward-1406", tc
->lsize
> 0);
2197 if (hint_is_unprepped_dclust(clust
->hint
)) {
2198 clust
->dstat
= UNPR_DISK_CLUSTER
;
2199 } else if (clust
->index
== info
->trunc_index
) {
2200 clust
->dstat
= TRNC_DISK_CLUSTER
;
2202 clust
->dstat
= PREP_DISK_CLUSTER
;
2203 dclust_set_extension_dsize(clust
->hint
, tc
->len
);
2206 assert("edward-1339",
2207 get_current_context()->grabbed_blocks
>= was_grabbed
);
2208 grabbed2free(get_current_context(),
2209 get_current_super_private(),
2210 get_current_context()->grabbed_blocks
- was_grabbed
);
2214 int get_disk_cluster_locked(struct cluster_handle
* clust
, struct inode
*inode
,
2215 znode_lock_mode lock_mode
)
2220 assert("edward-730", reiser4_schedulable());
2221 assert("edward-731", clust
!= NULL
);
2222 assert("edward-732", inode
!= NULL
);
2224 if (hint_is_valid(clust
->hint
)) {
2225 assert("edward-1293", clust
->dstat
!= INVAL_DISK_CLUSTER
);
2226 assert("edward-1294",
2227 znode_is_write_locked(clust
->hint
->lh
.node
));
2228 /* already have a valid locked position */
2229 return (clust
->dstat
==
2230 FAKE_DISK_CLUSTER
? CBK_COORD_NOTFOUND
:
2233 key_by_inode_cryptcompress(inode
, clust_to_off(clust
->index
, inode
),
2235 ra_info
.key_to_stop
= key
;
2236 set_key_offset(&ra_info
.key_to_stop
, get_key_offset(reiser4_max_key()));
2238 return find_cluster_item(clust
->hint
, &key
, lock_mode
, NULL
, FIND_EXACT
,
2242 /* Read needed cluster pages before modifying.
2243 If success, @clust->hint contains locked position in the tree.
2245 . find and set disk cluster state
2246 . make disk cluster dirty if its state is not FAKE_DISK_CLUSTER.
2248 static int read_some_cluster_pages(struct inode
* inode
,
2249 struct cluster_handle
* clust
)
2254 struct reiser4_slide
* win
= clust
->win
;
2255 znode_lock_mode mode
= ZNODE_WRITE_LOCK
;
2257 iplug
= item_plugin_by_id(CTAIL_ID
);
2259 assert("edward-924", !tfm_cluster_is_uptodate(&clust
->tc
));
2262 if (clust
->nr_pages
== 0) {
2263 /* start write hole from fake disk cluster */
2264 assert("edward-1117", win
!= NULL
);
2265 assert("edward-1118", win
->stat
== HOLE_WINDOW
);
2266 assert("edward-1119", new_logical_cluster(clust
, inode
));
2269 if (new_logical_cluster(clust
, inode
)) {
2271 new page cluster is about to be written, nothing to read,
2273 assert("edward-734", reiser4_schedulable());
2274 assert("edward-735", clust
->hint
->lh
.owner
== NULL
);
2276 if (clust
->nr_pages
) {
2279 assert("edward-1419", clust
->pages
!= NULL
);
2280 pg
= clust
->pages
[clust
->nr_pages
- 1];
2281 assert("edward-1420", pg
!= NULL
);
2282 off
= off_to_pgoff(win
->off
+win
->count
+win
->delta
);
2285 zero_user_segment(pg
, off
, PAGE_CACHE_SIZE
);
2289 clust
->dstat
= FAKE_DISK_CLUSTER
;
2293 Here we should search for disk cluster to figure out its real state.
2294 Also there is one more important reason to do disk search: we need
2295 to make disk cluster _dirty_ if it exists
2298 /* if windows is specified, read the only pages
2299 that will be modified partially */
2301 for (i
= 0; i
< clust
->nr_pages
; i
++) {
2302 struct page
*pg
= clust
->pages
[i
];
2305 if (PageUptodate(pg
)) {
2312 i
>= size_in_pages(win
->off
) &&
2313 i
< off_to_pg(win
->off
+ win
->count
+ win
->delta
))
2314 /* page will be completely overwritten */
2317 if (win
&& (i
== clust
->nr_pages
- 1) &&
2321 (size_in_pages(i_size_read(inode
)) <= pg
->index
)) {
2323 so set zeroes to the rest */
2326 assert("edward-1260",
2327 size_in_pages(win
->off
+ win
->count
+
2328 win
->delta
) - 1 == i
);
2331 off_to_pgoff(win
->off
+ win
->count
+ win
->delta
);
2332 zero_user_segment(pg
, offset
, PAGE_CACHE_SIZE
);
2334 /* still not uptodate */
2338 result
= do_readpage_ctail(inode
, clust
, pg
, mode
);
2340 assert("edward-1526", ergo(!result
, PageUptodate(pg
)));
2343 warning("edward-219", "do_readpage_ctail failed");
2347 if (!tfm_cluster_is_uptodate(&clust
->tc
)) {
2348 /* disk cluster unclaimed, but we need to make its znodes dirty
2349 * to make flush update convert its content
2351 result
= find_disk_cluster(clust
, inode
,
2352 0 /* do not read items */,
2356 tfm_cluster_clr_uptodate(&clust
->tc
);
2360 static int should_create_unprepped_cluster(struct cluster_handle
* clust
,
2361 struct inode
* inode
)
2363 assert("edward-737", clust
!= NULL
);
2365 switch (clust
->dstat
) {
2366 case PREP_DISK_CLUSTER
:
2367 case UNPR_DISK_CLUSTER
:
2369 case FAKE_DISK_CLUSTER
:
2371 clust
->win
->stat
== HOLE_WINDOW
&& clust
->nr_pages
== 0) {
2372 assert("edward-1172",
2373 new_logical_cluster(clust
, inode
));
2378 impossible("edward-1173", "bad disk cluster state");
2383 static int cryptcompress_make_unprepped_cluster(struct cluster_handle
* clust
,
2384 struct inode
*inode
)
2388 assert("edward-1123", reiser4_schedulable());
2389 assert("edward-737", clust
!= NULL
);
2390 assert("edward-738", inode
!= NULL
);
2391 assert("edward-739", cryptcompress_inode_ok(inode
));
2392 assert("edward-1053", clust
->hint
!= NULL
);
2394 if (!should_create_unprepped_cluster(clust
, inode
)) {
2395 if (clust
->reserved
) {
2396 cluster_reserved2free(estimate_insert_cluster(inode
));
2398 assert("edward-1267",
2399 clust
->reserved_unprepped
==
2400 estimate_insert_cluster(inode
));
2401 clust
->reserved_unprepped
-=
2402 estimate_insert_cluster(inode
);
2407 assert("edward-1268", clust
->reserved
);
2408 cluster_reserved2grabbed(estimate_insert_cluster(inode
));
2410 assert("edward-1441",
2411 clust
->reserved_unprepped
== estimate_insert_cluster(inode
));
2412 clust
->reserved_unprepped
-= estimate_insert_cluster(inode
);
2414 result
= ctail_insert_unprepped_cluster(clust
, inode
);
2418 inode_add_bytes(inode
, inode_cluster_size(inode
));
2420 assert("edward-743", cryptcompress_inode_ok(inode
));
2421 assert("edward-744", znode_is_write_locked(clust
->hint
->lh
.node
));
2423 clust
->dstat
= UNPR_DISK_CLUSTER
;
2427 /* . Grab page cluster for read, write, setattr, etc. operations;
2428 * . Truncate its complete pages, if needed;
2430 int prepare_page_cluster(struct inode
* inode
, struct cluster_handle
* clust
,
2433 assert("edward-177", inode
!= NULL
);
2434 assert("edward-741", cryptcompress_inode_ok(inode
));
2435 assert("edward-740", clust
->pages
!= NULL
);
2437 set_cluster_nrpages(clust
, inode
);
2438 reset_cluster_pgset(clust
, cluster_nrpages(inode
));
2439 return grab_page_cluster(inode
, clust
, rw
);
2442 /* Truncate complete page cluster of index @index.
2443 * This is called by ->kill_hook() method of item
2444 * plugin when deleting a disk cluster of such index.
2446 void truncate_complete_page_cluster(struct inode
*inode
, cloff_t index
,
2452 struct page
*pages
[MAX_CLUSTER_NRPAGES
];
2454 node
= jlookup(current_tree
, get_inode_oid(inode
),
2455 clust_to_pg(index
, inode
));
2456 nr_pages
= size_in_pages(lbytes(index
, inode
));
2457 assert("edward-1483", nr_pages
!= 0);
2460 found
= find_get_pages(inode
->i_mapping
,
2461 clust_to_pg(index
, inode
),
2462 cluster_nrpages(inode
), pages
);
2464 assert("edward-1484", jnode_truncate_ok(inode
, index
));
2469 if (reiser4_inode_get_flag(inode
, REISER4_FILE_CONV_IN_PROGRESS
)
2471 /* converting to unix_file is in progress */
2472 JF_CLR(node
, JNODE_CLUSTER_PAGE
);
2473 if (JF_ISSET(node
, JNODE_DIRTY
)) {
2475 * @nr_pages were checked in, but not yet checked out -
2476 * we need to release them. (also there can be pages
2477 * attached to page cache by read(), etc. - don't take
2478 * them into account).
2480 assert("edward-1198", found
>= nr_pages
);
2482 /* free disk space grabbed for disk cluster converting */
2483 cluster_reserved2grabbed(estimate_update_cluster(inode
));
2484 grabbed2free(get_current_context(),
2485 get_current_super_private(),
2486 estimate_update_cluster(inode
));
2487 __put_page_cluster(0, nr_pages
, pages
, inode
);
2489 /* This will clear dirty bit, uncapture and unlock jnode */
2490 unlock_cluster_uncapture(node
);
2492 unlock_cluster(node
);
2493 jput(node
); /* jlookup */
2494 put_found_pages(pages
, found
); /* find_get_pages */
2496 if (reiser4_inode_get_flag(inode
, REISER4_FILE_CONV_IN_PROGRESS
) &&
2499 truncate_page_cluster_range(inode
, pages
, index
, 0,
2500 cluster_nrpages(inode
),
2502 assert("edward-1201",
2503 ergo(!reiser4_inode_get_flag(inode
,
2504 REISER4_FILE_CONV_IN_PROGRESS
),
2505 jnode_truncate_ok(inode
, index
)));
2510 * Set cluster handle @clust of a logical cluster before
2511 * modifications which are supposed to be committed.
2513 * . grab cluster pages;
2514 * . reserve disk space;
2515 * . maybe read pages from disk and set the disk cluster dirty;
2516 * . maybe write hole and check in (partially zeroed) logical cluster;
2517 * . create 'unprepped' disk cluster for new or fake logical one.
2519 static int prepare_logical_cluster(struct inode
*inode
,
2520 loff_t file_off
, /* write position
2522 loff_t to_file
, /* bytes of users data
2523 to write to the file */
2524 struct cluster_handle
* clust
,
2525 logical_cluster_op op
)
2528 struct reiser4_slide
* win
= clust
->win
;
2530 reset_cluster_params(clust
);
2531 cluster_set_tfm_act(&clust
->tc
, TFMA_READ
);
2533 clust
->ctx
= get_current_context();
2535 assert("edward-1190", op
!= LC_INVAL
);
2539 result
= prepare_page_cluster(inode
, clust
, WRITE_OP
);
2542 assert("edward-1447",
2543 ergo(clust
->nr_pages
!= 0, jprivate(clust
->pages
[0])));
2544 assert("edward-1448",
2545 ergo(clust
->nr_pages
!= 0,
2546 jnode_is_cluster_page(jprivate(clust
->pages
[0]))));
2548 result
= reserve4cluster(inode
, clust
);
2551 result
= read_some_cluster_pages(inode
, clust
);
2553 free_reserved4cluster(inode
,
2555 estimate_update_cluster(inode
) +
2556 estimate_insert_cluster(inode
));
2559 assert("edward-1124", clust
->dstat
!= INVAL_DISK_CLUSTER
);
2561 result
= cryptcompress_make_unprepped_cluster(clust
, inode
);
2564 if (win
&& win
->stat
== HOLE_WINDOW
) {
2565 result
= write_hole(inode
, clust
, file_off
, to_file
);
2571 free_reserved4cluster(inode
, clust
,
2572 estimate_update_cluster(inode
));
2574 put_page_cluster(clust
, inode
, WRITE_OP
);
2575 assert("edward-1125", result
== -ENOSPC
);
2579 /* set window by two offsets */
2580 static void set_window(struct cluster_handle
* clust
,
2581 struct reiser4_slide
* win
, struct inode
*inode
,
2582 loff_t o1
, loff_t o2
)
2584 assert("edward-295", clust
!= NULL
);
2585 assert("edward-296", inode
!= NULL
);
2586 assert("edward-1071", win
!= NULL
);
2587 assert("edward-297", o1
<= o2
);
2589 clust
->index
= off_to_clust(o1
, inode
);
2591 win
->off
= off_to_cloff(o1
, inode
);
2592 win
->count
= min((loff_t
)(inode_cluster_size(inode
) - win
->off
),
2599 static int set_cluster_by_window(struct inode
*inode
,
2600 struct cluster_handle
* clust
,
2601 struct reiser4_slide
* win
, size_t length
,
2606 assert("edward-197", clust
!= NULL
);
2607 assert("edward-1072", win
!= NULL
);
2608 assert("edward-198", inode
!= NULL
);
2610 result
= alloc_cluster_pgset(clust
, cluster_nrpages(inode
));
2614 if (file_off
> i_size_read(inode
)) {
2615 /* Uhmm, hole in cryptcompress file... */
2617 hole_size
= file_off
- inode
->i_size
;
2619 set_window(clust
, win
, inode
, inode
->i_size
, file_off
);
2620 win
->stat
= HOLE_WINDOW
;
2621 if (win
->off
+ hole_size
< inode_cluster_size(inode
))
2622 /* there is also user's data to append to the hole */
2623 win
->delta
= min(inode_cluster_size(inode
) -
2624 (win
->off
+ win
->count
), length
);
2627 set_window(clust
, win
, inode
, file_off
, file_off
+ length
);
2628 win
->stat
= DATA_WINDOW
;
2632 int set_cluster_by_page(struct cluster_handle
* clust
, struct page
* page
,
2636 int (*setting_actor
)(struct cluster_handle
* clust
, int count
);
2638 assert("edward-1358", clust
!= NULL
);
2639 assert("edward-1359", page
!= NULL
);
2640 assert("edward-1360", page
->mapping
!= NULL
);
2641 assert("edward-1361", page
->mapping
->host
!= NULL
);
2644 (clust
->pages
? reset_cluster_pgset
: alloc_cluster_pgset
);
2645 result
= setting_actor(clust
, count
);
2646 clust
->index
= pg_to_clust(page
->index
, page
->mapping
->host
);
2650 /* reset all the params that not get updated */
2651 void reset_cluster_params(struct cluster_handle
* clust
)
2653 assert("edward-197", clust
!= NULL
);
2655 clust
->dstat
= INVAL_DISK_CLUSTER
;
2656 clust
->tc
.uptodate
= 0;
2660 static loff_t
do_write_cryptcompress(struct file
*file
, struct inode
*inode
,
2661 const char __user
*buf
, size_t to_write
,
2662 loff_t pos
, int *conv_occured
)
2668 struct reiser4_slide win
;
2669 struct cluster_handle clust
;
2670 struct cryptcompress_info
* info
;
2672 assert("edward-161", reiser4_schedulable());
2673 assert("edward-748", cryptcompress_inode_ok(inode
));
2674 assert("edward-159", current_blocksize
== PAGE_CACHE_SIZE
);
2675 assert("edward-1274", get_current_context()->grabbed_blocks
== 0);
2677 hint
= kmalloc(sizeof(*hint
), reiser4_ctx_gfp_mask_get());
2679 return RETERR(-ENOMEM
);
2681 result
= load_file_hint(file
, hint
);
2688 reiser4_slide_init(&win
);
2689 cluster_init_read(&clust
, &win
);
2691 info
= cryptcompress_inode_data(inode
);
2693 mutex_lock(&info
->checkin_mutex
);
2695 result
= set_cluster_by_window(inode
, &clust
, &win
, to_write
, pos
);
2699 if (next_window_stat(&win
) == HOLE_WINDOW
) {
2700 /* write hole in this iteration
2701 separated from the loop below */
2702 result
= write_conversion_hook(file
, inode
,
2708 result
= prepare_logical_cluster(inode
, pos
, count
, &clust
,
2714 const char __user
* src
;
2715 unsigned page_off
, to_page
;
2717 assert("edward-750", reiser4_schedulable());
2719 result
= write_conversion_hook(file
, inode
,
2720 pos
+ to_write
- count
,
2723 if (result
|| *conv_occured
)
2725 result
= prepare_logical_cluster(inode
, pos
, count
, &clust
,
2730 assert("edward-751", cryptcompress_inode_ok(inode
));
2731 assert("edward-204", win
.stat
== DATA_WINDOW
);
2732 assert("edward-1288", hint_is_valid(clust
.hint
));
2733 assert("edward-752",
2734 znode_is_write_locked(hint
->ext_coord
.coord
.node
));
2735 put_hint_cluster(&clust
, inode
, ZNODE_WRITE_LOCK
);
2737 /* set write position in page */
2738 page_off
= off_to_pgoff(win
.off
);
2740 /* copy user's data to cluster pages */
2741 for (i
= off_to_pg(win
.off
), src
= buf
;
2742 i
< size_in_pages(win
.off
+ win
.count
);
2743 i
++, src
+= to_page
) {
2744 to_page
= __mbp(win
.off
+ win
.count
, i
) - page_off
;
2745 assert("edward-1039",
2746 page_off
+ to_page
<= PAGE_CACHE_SIZE
);
2747 assert("edward-287", clust
.pages
[i
] != NULL
);
2749 fault_in_pages_readable(src
, to_page
);
2751 lock_page(clust
.pages
[i
]);
2753 __copy_from_user((char *)kmap(clust
.pages
[i
]) +
2754 page_off
, src
, to_page
);
2755 kunmap(clust
.pages
[i
]);
2756 if (unlikely(result
)) {
2757 unlock_page(clust
.pages
[i
]);
2761 SetPageUptodate(clust
.pages
[i
]);
2762 reiser4_set_page_dirty_internal(clust
.pages
[i
]);
2763 flush_dcache_page(clust
.pages
[i
]);
2764 mark_page_accessed(clust
.pages
[i
]);
2765 unlock_page(clust
.pages
[i
]);
2768 assert("edward-753", cryptcompress_inode_ok(inode
));
2770 result
= checkin_logical_cluster(&clust
, inode
);
2777 result
= balance_dirty_page_cluster(&clust
, inode
, 0, count
);
2780 assert("edward-755", hint
->lh
.owner
== NULL
);
2781 reset_cluster_params(&clust
);
2784 put_page_cluster(&clust
, inode
, WRITE_OP
);
2787 free_reserved4cluster(inode
,
2789 estimate_update_cluster(inode
));
2794 * NOTE: at this point file may have
2795 * another (unix-file) plugin installed
2798 if (result
== -EEXIST
)
2799 warning("edward-1407", "write returns EEXIST!\n");
2801 put_cluster_handle(&clust
);
2802 save_file_hint(file
, hint
);
2805 * don't release cryptcompress-specific
2806 * checkin_mutex, if conversion occured
2808 if (*conv_occured
== 0)
2809 mutex_unlock(&info
->checkin_mutex
);
2811 /* if nothing were written - there must be an error */
2812 assert("edward-195", ergo((to_write
== count
),
2813 (result
< 0 || *conv_occured
)));
2814 return (to_write
- count
) ? (to_write
- count
) : result
;
2820 * write_cryptcompress - write of struct file_operations
2821 * @file: file to write to
2822 * @buf: address of user-space buffer
2823 * @read_amount: number of bytes to write
2824 * @off: position in file to write to
2826 * This is implementation of vfs's write method of struct file_operations for
2827 * cryptcompress plugin.
2829 ssize_t
write_cryptcompress(struct file
*file
, const char __user
*buf
,
2830 size_t count
, loff_t
*off
, int *conv
)
2833 struct inode
*inode
;
2834 reiser4_context
*ctx
;
2836 struct cryptcompress_info
*info
;
2838 assert("edward-1449", *conv
== 0);
2840 inode
= file
->f_dentry
->d_inode
;
2841 assert("edward-196", cryptcompress_inode_ok(inode
));
2843 info
= cryptcompress_inode_data(inode
);
2845 ctx
= reiser4_init_context(inode
->i_sb
);
2847 return PTR_ERR(ctx
);
2849 mutex_lock(&inode
->i_mutex
);
2851 result
= generic_write_checks(file
, &pos
, &count
, 0);
2852 if (unlikely(result
!= 0))
2854 if (unlikely(count
== 0))
2856 result
= remove_suid(file
->f_dentry
);
2857 if (unlikely(result
!= 0))
2859 /* remove_suid might create a transaction */
2860 reiser4_txn_restart(ctx
);
2862 result
= do_write_cryptcompress(file
, inode
, buf
, count
, pos
, conv
);
2866 /* update position in a file */
2867 *off
= pos
+ result
;
2869 mutex_unlock(&inode
->i_mutex
);
2871 context_set_commit_async(ctx
);
2872 reiser4_exit_context(ctx
);
2876 int readpages_cryptcompress(struct file
*file
, struct address_space
*mapping
,
2877 struct list_head
*pages
, unsigned nr_pages
)
2879 reiser4_context
* ctx
;
2882 ctx
= reiser4_init_context(mapping
->host
->i_sb
);
2887 /* cryptcompress file can be built of ctail items only */
2888 ret
= readpages_ctail(file
, mapping
, pages
);
2889 reiser4_txn_restart(ctx
);
2890 reiser4_exit_context(ctx
);
2893 put_pages_list(pages
);
2898 static reiser4_block_nr
cryptcompress_estimate_read(struct inode
*inode
)
2900 /* reserve one block to update stat data item */
2901 assert("edward-1193",
2902 inode_file_plugin(inode
)->estimate
.update
==
2903 estimate_update_common
);
2904 return estimate_update_common(inode
);
2908 * read_cryptcompress - read of struct file_operations
2909 * @file: file to read from
2910 * @buf: address of user-space buffer
2911 * @read_amount: number of bytes to read
2912 * @off: position in file to read from
2914 * This is implementation of vfs's read method of struct file_operations for
2915 * cryptcompress plugin.
2917 ssize_t
read_cryptcompress(struct file
* file
, char __user
*buf
, size_t size
,
2921 struct inode
*inode
;
2922 reiser4_context
*ctx
;
2923 struct cryptcompress_info
*info
;
2924 reiser4_block_nr needed
;
2926 inode
= file
->f_dentry
->d_inode
;
2927 assert("edward-1194", !reiser4_inode_get_flag(inode
, REISER4_NO_SD
));
2929 ctx
= reiser4_init_context(inode
->i_sb
);
2931 return PTR_ERR(ctx
);
2933 info
= cryptcompress_inode_data(inode
);
2934 needed
= cryptcompress_estimate_read(inode
);
2936 result
= reiser4_grab_space(needed
, BA_CAN_COMMIT
);
2938 reiser4_exit_context(ctx
);
2941 result
= do_sync_read(file
, buf
, size
, off
);
2943 context_set_commit_async(ctx
);
2944 reiser4_exit_context(ctx
);
2949 /* Look for a disk cluster and keep lookup result in @found.
2950 * If @index > 0, then find disk cluster of the index (@index - 1);
2951 * If @index == 0, then find the rightmost disk cluster.
2952 * Keep incremented index of the found disk cluster in @found.
2953 * @found == 0 means that disk cluster was not found (in the last
2954 * case (@index == 0) it means that file doesn't have disk clusters).
2956 static int lookup_disk_cluster(struct inode
*inode
, cloff_t
* found
,
2968 assert("edward-1131", inode
!= NULL
);
2969 assert("edward-95", cryptcompress_inode_ok(inode
));
2971 hint
= kmalloc(sizeof(*hint
), reiser4_ctx_gfp_mask_get());
2973 return RETERR(-ENOMEM
);
2974 hint_init_zero(hint
);
2977 bias
= (index
? FIND_EXACT
: FIND_MAX_NOT_MORE_THAN
);
2979 (index
? clust_to_off(index
, inode
) -
2980 1 : get_key_offset(reiser4_max_key()));
2982 key_by_inode_cryptcompress(inode
, offset
, &key
);
2984 /* find the last item of this object */
2986 find_cluster_item(hint
, &key
, ZNODE_READ_LOCK
, NULL
/* ra_info */,
2988 if (cbk_errored(result
)) {
2993 if (result
== CBK_COORD_NOTFOUND
) {
2994 /* no real disk clusters */
3000 /* disk cluster is found */
3001 coord
= &hint
->ext_coord
.coord
;
3002 coord_clear_iplug(coord
);
3003 result
= zload(coord
->node
);
3004 if (unlikely(result
)) {
3009 iplug
= item_plugin_by_coord(coord
);
3010 assert("edward-277", iplug
== item_plugin_by_id(CTAIL_ID
));
3011 assert("edward-1202", ctail_ok(coord
));
3013 item_key_by_coord(coord
, &key
);
3014 *found
= off_to_clust(get_key_offset(&key
), inode
) + 1;
3016 assert("edward-1132", ergo(index
, index
== *found
));
3018 zrelse(coord
->node
);
3024 static int find_fake_appended(struct inode
*inode
, cloff_t
* index
)
3026 return lookup_disk_cluster(inode
, index
,
3027 0 /* find last real one */ );
3030 /* Set left coord when unit is not found after node_lookup()
3031 This takes into account that there can be holes in a sequence
3034 static void adjust_left_coord(coord_t
* left_coord
)
3036 switch (left_coord
->between
) {
3038 left_coord
->between
= AFTER_ITEM
;
3043 impossible("edward-1204", "bad left coord to cut");
3048 #define CRC_CUT_TREE_MIN_ITERATIONS 64
3049 int cut_tree_worker_cryptcompress(tap_t
* tap
, const reiser4_key
* from_key
,
3050 const reiser4_key
* to_key
,
3051 reiser4_key
* smallest_removed
,
3052 struct inode
*object
, int truncate
,
3055 lock_handle next_node_lock
;
3059 assert("edward-1158", tap
->coord
->node
!= NULL
);
3060 assert("edward-1159", znode_is_write_locked(tap
->coord
->node
));
3061 assert("edward-1160", znode_get_level(tap
->coord
->node
) == LEAF_LEVEL
);
3064 init_lh(&next_node_lock
);
3067 znode
*node
; /* node from which items are cut */
3068 node_plugin
*nplug
; /* node plugin for @node */
3070 node
= tap
->coord
->node
;
3072 /* Move next_node_lock to the next node on the left. */
3074 reiser4_get_left_neighbor(&next_node_lock
, node
,
3076 GN_CAN_USE_UPPER_LEVELS
);
3077 if (result
!= 0 && result
!= -E_NO_NEIGHBOR
)
3079 /* FIXME-EDWARD: Check can we delete the node as a whole. */
3080 result
= reiser4_tap_load(tap
);
3084 /* Prepare the second (right) point for cut_node() */
3086 coord_init_last_unit(tap
->coord
, node
);
3088 else if (item_plugin_by_coord(tap
->coord
)->b
.lookup
== NULL
)
3089 /* set rightmost unit for the items without lookup method */
3090 tap
->coord
->unit_pos
= coord_last_unit_pos(tap
->coord
);
3092 nplug
= node
->nplug
;
3094 assert("edward-1161", nplug
);
3095 assert("edward-1162", nplug
->lookup
);
3097 /* left_coord is leftmost unit cut from @node */
3098 result
= nplug
->lookup(node
, from_key
, FIND_EXACT
, &left_coord
);
3100 if (IS_CBKERR(result
))
3103 if (result
== CBK_COORD_NOTFOUND
)
3104 adjust_left_coord(&left_coord
);
3106 /* adjust coordinates so that they are set to existing units */
3107 if (coord_set_to_right(&left_coord
)
3108 || coord_set_to_left(tap
->coord
)) {
3113 if (coord_compare(&left_coord
, tap
->coord
) ==
3114 COORD_CMP_ON_RIGHT
) {
3115 /* keys from @from_key to @to_key are not in the tree */
3120 /* cut data from one node */
3121 *smallest_removed
= *reiser4_min_key();
3122 result
= kill_node_content(&left_coord
,
3127 next_node_lock
.node
,
3129 reiser4_tap_relse(tap
);
3136 /* Check whether all items with keys >= from_key were removed
3138 if (keyle(smallest_removed
, from_key
))
3142 if (next_node_lock
.node
== NULL
)
3145 result
= reiser4_tap_move(tap
, &next_node_lock
);
3146 done_lh(&next_node_lock
);
3150 /* Break long cut_tree operation (deletion of a large file) if
3151 * atom requires commit. */
3152 if (*progress
> CRC_CUT_TREE_MIN_ITERATIONS
3153 && current_atom_should_commit()) {
3158 done_lh(&next_node_lock
);
3162 /* Append or expand hole in two steps:
3163 * 1) set zeroes to the rightmost page of the rightmost non-fake
3165 * 2) expand hole via fake logical clusters (just increase i_size)
3167 static int cryptcompress_append_hole(struct inode
*inode
/* with old size */,
3175 struct reiser4_slide win
;
3176 struct cluster_handle clust
;
3178 assert("edward-1133", inode
->i_size
< new_size
);
3179 assert("edward-1134", reiser4_schedulable());
3180 assert("edward-1135", cryptcompress_inode_ok(inode
));
3181 assert("edward-1136", current_blocksize
== PAGE_CACHE_SIZE
);
3182 assert("edward-1333", off_to_cloff(inode
->i_size
, inode
) != 0);
3184 hint
= kmalloc(sizeof(*hint
), reiser4_ctx_gfp_mask_get());
3186 return RETERR(-ENOMEM
);
3187 hint_init_zero(hint
);
3190 reiser4_slide_init(&win
);
3191 cluster_init_read(&clust
, &win
);
3194 result
= alloc_cluster_pgset(&clust
, cluster_nrpages(inode
));
3197 if (off_to_cloff(inode
->i_size
, inode
) == 0)
3199 hole_size
= new_size
- inode
->i_size
;
3201 inode_cluster_size(inode
) - off_to_cloff(inode
->i_size
, inode
);
3202 if (hole_size
< nr_zeroes
)
3203 nr_zeroes
= hole_size
;
3204 set_window(&clust
, &win
, inode
, inode
->i_size
,
3205 inode
->i_size
+ nr_zeroes
);
3206 win
.stat
= HOLE_WINDOW
;
3208 assert("edward-1137",
3209 clust
.index
== off_to_clust(inode
->i_size
, inode
));
3211 result
= prepare_logical_cluster(inode
, 0, 0, &clust
, LC_APPOV
);
3213 assert("edward-1271", !result
|| result
== -ENOSPC
);
3216 assert("edward-1139",
3217 clust
.dstat
== PREP_DISK_CLUSTER
||
3218 clust
.dstat
== UNPR_DISK_CLUSTER
);
3220 assert("edward-1431", hole_size
>= nr_zeroes
);
3221 if (hole_size
== nr_zeroes
)
3222 /* nothing to append anymore */
3225 INODE_SET_SIZE(inode
, new_size
);
3229 put_cluster_handle(&clust
);
3234 update_cryptcompress_size(struct inode
*inode
, reiser4_key
* key
, int update_sd
)
3236 return (get_key_offset(key
) & ((loff_t
) (inode_cluster_size(inode
)) - 1)
3237 ? 0 : reiser4_update_file_size(inode
, key
, update_sd
));
3240 /* Prune cryptcompress file in two steps:
3241 * 1) cut all nominated logical clusters except the leftmost one which
3242 * is to be partially truncated. Note, that there can be "holes"
3243 * represented by fake logical clusters.
3244 * 2) set zeroes and capture leftmost partially truncated logical
3245 * cluster, if it is not fake; otherwise prune fake logical cluster
3246 * (just decrease i_size).
3248 static int prune_cryptcompress(struct inode
*inode
, loff_t new_size
,
3249 int update_sd
, cloff_t aidx
)
3259 struct reiser4_slide win
;
3260 struct cluster_handle clust
;
3262 assert("edward-1140", inode
->i_size
>= new_size
);
3263 assert("edward-1141", reiser4_schedulable());
3264 assert("edward-1142", cryptcompress_inode_ok(inode
));
3265 assert("edward-1143", current_blocksize
== PAGE_CACHE_SIZE
);
3267 old_size
= inode
->i_size
;
3269 hint
= kmalloc(sizeof(*hint
), reiser4_ctx_gfp_mask_get());
3271 return RETERR(-ENOMEM
);
3272 hint_init_zero(hint
);
3275 reiser4_slide_init(&win
);
3276 cluster_init_read(&clust
, &win
);
3279 /* calculate index of the rightmost logical cluster
3280 that will be completely truncated */
3281 ridx
= size_in_lc(new_size
, inode
);
3283 /* truncate all disk clusters starting from @ridx */
3284 assert("edward-1174", ridx
<= aidx
);
3285 old_size
= inode
->i_size
;
3287 struct cryptcompress_info
* info
;
3288 info
= cryptcompress_inode_data(inode
);
3289 result
= cut_file_items(inode
,
3290 clust_to_off(ridx
, inode
),
3292 clust_to_off(aidx
, inode
),
3293 update_cryptcompress_size
);
3294 info
->trunc_index
= ULONG_MAX
;
3299 * there can be pages of fake logical clusters, truncate them
3301 truncate_inode_pages(inode
->i_mapping
, clust_to_off(ridx
, inode
));
3302 assert("edward-1524",
3303 pages_truncate_ok(inode
, clust_to_pg(ridx
, inode
)));
3305 * now perform partial truncate of last logical cluster
3307 if (!off_to_cloff(new_size
, inode
)) {
3308 /* no partial truncate is needed */
3309 assert("edward-1145", inode
->i_size
== new_size
);
3312 assert("edward-1146", new_size
< inode
->i_size
);
3314 to_prune
= inode
->i_size
- new_size
;
3316 /* check if the last logical cluster is fake */
3317 result
= lookup_disk_cluster(inode
, &aidx
, ridx
);
3321 /* yup, this is fake one */
3324 assert("edward-1148", aidx
== ridx
);
3326 /* do partial truncate of the last page cluster,
3327 and try to capture this one */
3328 result
= alloc_cluster_pgset(&clust
, cluster_nrpages(inode
));
3331 nr_zeroes
= (off_to_pgoff(new_size
) ?
3332 PAGE_CACHE_SIZE
- off_to_pgoff(new_size
) : 0);
3333 set_window(&clust
, &win
, inode
, new_size
, new_size
+ nr_zeroes
);
3334 win
.stat
= HOLE_WINDOW
;
3336 assert("edward-1149", clust
.index
== ridx
- 1);
3338 result
= prepare_logical_cluster(inode
, 0, 0, &clust
, LC_TRUNC
);
3341 assert("edward-1151",
3342 clust
.dstat
== PREP_DISK_CLUSTER
||
3343 clust
.dstat
== UNPR_DISK_CLUSTER
);
3345 assert("edward-1191", inode
->i_size
== new_size
);
3346 assert("edward-1206", body_truncate_ok(inode
, ridx
));
3348 /* drop all the pages that don't have jnodes (i.e. pages
3349 which can not be truncated by cut_file_items() because
3350 of holes represented by fake disk clusters) including
3351 the pages of partially truncated cluster which was
3352 released by prepare_logical_cluster() */
3353 INODE_SET_SIZE(inode
, new_size
);
3354 truncate_inode_pages(inode
->i_mapping
, new_size
);
3356 assert("edward-1334", !result
|| result
== -ENOSPC
);
3357 assert("edward-1497",
3358 pages_truncate_ok(inode
, size_in_pages(new_size
)));
3362 put_cluster_handle(&clust
);
3366 /* Prepare cryptcompress file for truncate:
3367 * prune or append rightmost fake logical clusters (if any)
3369 static int start_truncate_fake(struct inode
*inode
, cloff_t aidx
,
3370 loff_t new_size
, int update_sd
)
3375 if (new_size
> inode
->i_size
) {
3377 if (inode
->i_size
< clust_to_off(aidx
, inode
))
3380 bytes
= new_size
- inode
->i_size
;
3381 INODE_SET_SIZE(inode
, inode
->i_size
+ bytes
);
3384 if (inode
->i_size
<= clust_to_off(aidx
, inode
))
3387 bytes
= inode
->i_size
-
3388 max(new_size
, clust_to_off(aidx
, inode
));
3391 INODE_SET_SIZE(inode
, inode
->i_size
- bytes
);
3392 /* In the case of fake prune we need to drop page cluster.
3393 There are only 2 cases for partially truncated page:
3394 1. If is is dirty, therefore it is anonymous
3395 (was dirtied via mmap), and will be captured
3396 later via ->capture().
3397 2. If is clean, therefore it is filled by zeroes.
3398 In both cases we don't need to make it dirty and
3401 truncate_inode_pages(inode
->i_mapping
, inode
->i_size
);
3404 result
= update_sd_cryptcompress(inode
);
3408 /* This is called in setattr_cryptcompress when it is used to truncate,
3409 * and in delete_cryptcompress */
3410 static int cryptcompress_truncate(struct inode
*inode
, /* old size */
3411 loff_t new_size
, /* new size */
3417 result
= find_fake_appended(inode
, &aidx
);
3420 assert("edward-1208",
3421 ergo(aidx
> 0, inode
->i_size
> clust_to_off(aidx
- 1, inode
)));
3423 result
= start_truncate_fake(inode
, aidx
, new_size
, update_sd
);
3426 if (inode
->i_size
== new_size
)
3427 /* nothing to truncate anymore */
3429 result
= (inode
->i_size
< new_size
?
3430 cryptcompress_append_hole(inode
, new_size
) :
3431 prune_cryptcompress(inode
, new_size
, update_sd
, aidx
));
3432 if (!result
&& update_sd
)
3433 result
= update_sd_cryptcompress(inode
);
3437 /* Capture an anonymous pager cluster. (Page cluser is
3438 * anonymous if it contains at least one anonymous page
3440 static int capture_anon_page_cluster(struct cluster_handle
* clust
,
3441 struct inode
* inode
)
3445 assert("edward-1073", clust
!= NULL
);
3446 assert("edward-1074", inode
!= NULL
);
3447 assert("edward-1075", clust
->dstat
== INVAL_DISK_CLUSTER
);
3449 result
= prepare_logical_cluster(inode
, 0, 0, clust
, LC_APPOV
);
3452 set_cluster_pages_dirty(clust
, inode
);
3453 result
= checkin_logical_cluster(clust
, inode
);
3454 put_hint_cluster(clust
, inode
, ZNODE_WRITE_LOCK
);
3455 if (unlikely(result
))
3456 put_page_cluster(clust
, inode
, WRITE_OP
);
3460 /* Starting from @index find tagged pages of the same page cluster.
3461 * Clear the tag for each of them. Return number of found pages.
3463 static int find_anon_page_cluster(struct address_space
* mapping
,
3464 pgoff_t
* index
, struct page
** pages
)
3468 write_lock_irq(&mapping
->tree_lock
);
3470 /* looking for one page */
3471 found
= radix_tree_gang_lookup_tag(&mapping
->page_tree
,
3474 PAGECACHE_TAG_REISER4_MOVED
);
3477 if (!same_page_cluster(pages
[0], pages
[i
]))
3481 page_cache_get(pages
[i
]);
3482 *index
= pages
[i
]->index
+ 1;
3484 radix_tree_tag_clear(&mapping
->page_tree
,
3486 PAGECACHE_TAG_REISER4_MOVED
);
3487 if (last_page_in_cluster(pages
[i
++]))
3490 write_unlock_irq(&mapping
->tree_lock
);
3494 #define MAX_PAGES_TO_CAPTURE (1024)
3496 /* Capture anonymous page clusters */
3497 static int capture_anon_pages(struct address_space
* mapping
, pgoff_t
* index
,
3505 struct inode
* inode
;
3506 struct cluster_handle clust
;
3507 struct page
* pages
[MAX_CLUSTER_NRPAGES
];
3509 assert("edward-1127", mapping
!= NULL
);
3510 assert("edward-1128", mapping
->host
!= NULL
);
3511 assert("edward-1440", mapping
->host
->i_mapping
== mapping
);
3513 inode
= mapping
->host
;
3514 hint
= kmalloc(sizeof(*hint
), reiser4_ctx_gfp_mask_get());
3516 return RETERR(-ENOMEM
);
3517 hint_init_zero(hint
);
3520 cluster_init_read(&clust
, NULL
);
3523 result
= alloc_cluster_pgset(&clust
, cluster_nrpages(inode
));
3527 while (to_capture
> 0) {
3528 found
= find_anon_page_cluster(mapping
, index
, pages
);
3530 *index
= (pgoff_t
) - 1;
3533 move_cluster_forward(&clust
, inode
, pages
[0]->index
);
3534 result
= capture_anon_page_cluster(&clust
, inode
);
3536 put_found_pages(pages
, found
); /* find_anon_page_cluster */
3539 to_capture
-= clust
.nr_pages
;
3540 count
+= clust
.nr_pages
;
3543 warning("edward-1077",
3544 "Capture failed (inode %llu, result=%i, captured=%d)\n",
3545 (unsigned long long)get_inode_oid(inode
), result
, count
);
3547 assert("edward-1078", ergo(found
> 0, count
> 0));
3548 if (to_capture
<= 0)
3549 /* there may be left more pages */
3550 __mark_inode_dirty(inode
, I_DIRTY_PAGES
);
3556 put_cluster_handle(&clust
);
3560 /* Returns true if inode's mapping has dirty pages
3561 which do not belong to any atom */
3562 static int cryptcompress_inode_has_anon_pages(struct inode
*inode
)
3565 read_lock_irq(&inode
->i_mapping
->tree_lock
);
3566 result
= radix_tree_tagged(&inode
->i_mapping
->page_tree
,
3567 PAGECACHE_TAG_REISER4_MOVED
);
3568 read_unlock_irq(&inode
->i_mapping
->tree_lock
);
3572 /* This is implementation of vfs's writepages method of struct
3573 address_space_operations */
3574 int writepages_cryptcompress(struct address_space
*mapping
,
3575 struct writeback_control
*wbc
)
3581 struct inode
*inode
;
3582 struct cryptcompress_info
*info
;
3584 inode
= mapping
->host
;
3585 if (!cryptcompress_inode_has_anon_pages(inode
))
3587 info
= cryptcompress_inode_data(inode
);
3588 nrpages
= size_in_pages(i_size_read(inode
));
3590 if (wbc
->sync_mode
!= WB_SYNC_ALL
)
3591 to_capture
= min(wbc
->nr_to_write
, (long)MAX_PAGES_TO_CAPTURE
);
3593 to_capture
= MAX_PAGES_TO_CAPTURE
;
3595 reiser4_context
*ctx
;
3597 ctx
= reiser4_init_context(inode
->i_sb
);
3599 result
= PTR_ERR(ctx
);
3602 /* avoid recursive calls to ->sync_inodes */
3605 assert("edward-1079",
3606 lock_stack_isclean(get_current_lock_stack()));
3608 reiser4_txn_restart_current();
3610 if (get_current_context()->entd
) {
3611 if (mutex_trylock(&info
->checkin_mutex
) == 0) {
3612 /* the mutex might be occupied by
3614 result
= RETERR(-EBUSY
);
3615 reiser4_exit_context(ctx
);
3619 mutex_lock(&info
->checkin_mutex
);
3621 result
= capture_anon_pages(inode
->i_mapping
, &index
,
3623 mutex_unlock(&info
->checkin_mutex
);
3626 reiser4_exit_context(ctx
);
3629 wbc
->nr_to_write
-= result
;
3630 if (wbc
->sync_mode
!= WB_SYNC_ALL
) {
3631 reiser4_exit_context(ctx
);
3634 result
= txnmgr_force_commit_all(inode
->i_sb
, 0);
3635 reiser4_exit_context(ctx
);
3636 } while (result
>= 0 && index
< nrpages
);
3639 if (is_in_reiser4_context()) {
3640 if (get_current_context()->nr_captured
>= CAPTURE_APAGE_BURST
) {
3641 /* there are already pages to flush, flush them out,
3642 do not delay until end of reiser4_sync_inodes */
3643 reiser4_writeout(inode
->i_sb
, wbc
);
3644 get_current_context()->nr_captured
= 0;
3650 /* plugin->u.file.mmap */
3651 int mmap_cryptcompress(struct file
*file
, struct vm_area_struct
*vma
)
3654 struct inode
*inode
;
3655 reiser4_context
*ctx
;
3657 inode
= file
->f_dentry
->d_inode
;
3658 ctx
= reiser4_init_context(inode
->i_sb
);
3660 return PTR_ERR(ctx
);
3662 * generic_file_mmap will do update_atime. Grab space for stat data
3665 result
= reiser4_grab_space_force
3666 (inode_file_plugin(inode
)->estimate
.update(inode
),
3669 reiser4_exit_context(ctx
);
3672 result
= generic_file_mmap(file
, vma
);
3673 reiser4_exit_context(ctx
);
3677 /* plugin->u.file.release */
3678 /* plugin->u.file.get_block */
3680 /* this is implementation of delete method of file plugin for
3681 * cryptcompress objects
3683 int delete_object_cryptcompress(struct inode
*inode
)
3686 struct cryptcompress_info
* info
;
3688 assert("edward-429", inode
->i_nlink
== 0);
3690 reiser4_txn_restart_current();
3691 info
= cryptcompress_inode_data(inode
);
3693 mutex_lock(&info
->checkin_mutex
);
3694 result
= cryptcompress_truncate(inode
, 0, 0);
3695 mutex_unlock(&info
->checkin_mutex
);
3698 warning("edward-430",
3699 "cannot truncate cryptcompress file %lli: %i",
3700 (unsigned long long)get_inode_oid(inode
),
3703 truncate_inode_pages(inode
->i_mapping
, 0);
3704 assert("edward-1487", pages_truncate_ok(inode
, 0));
3705 /* and remove stat data */
3706 return reiser4_delete_object_common(inode
);
3709 /* plugin->u.file.setattr method
3710 This implements actual truncate (see comments in reiser4/page_cache.c) */
3711 int setattr_cryptcompress(struct dentry
*dentry
, struct iattr
*attr
)
3714 struct inode
*inode
;
3715 struct cryptcompress_info
* info
;
3717 inode
= dentry
->d_inode
;
3718 info
= cryptcompress_inode_data(inode
);
3720 if (attr
->ia_valid
& ATTR_SIZE
) {
3721 if (i_size_read(inode
) != attr
->ia_size
) {
3722 reiser4_context
*ctx
;
3725 ctx
= reiser4_init_context(dentry
->d_inode
->i_sb
);
3727 return PTR_ERR(ctx
);
3729 old_size
= i_size_read(inode
);
3730 inode_check_scale(inode
, old_size
, attr
->ia_size
);
3732 mutex_lock(&info
->checkin_mutex
);
3733 result
= cryptcompress_truncate(inode
,
3736 mutex_unlock(&info
->checkin_mutex
);
3738 warning("edward-1192",
3739 "truncate_cryptcompress failed: oid %lli, "
3740 "old size %lld, new size %lld, retval %d",
3741 (unsigned long long)
3742 get_inode_oid(inode
), old_size
,
3743 attr
->ia_size
, result
);
3745 context_set_commit_async(ctx
);
3746 reiser4_exit_context(ctx
);
3750 result
= reiser4_setattr_common(dentry
, attr
);
3755 * release_cryptcompress - release of struct file_operations
3756 * @inode: inode of released file
3757 * @file: file to release
3759 int release_cryptcompress(struct inode
*inode
, struct file
*file
)
3761 reiser4_context
*ctx
= reiser4_init_context(inode
->i_sb
);
3764 return PTR_ERR(ctx
);
3765 reiser4_free_file_fsdata(file
);
3766 reiser4_exit_context(ctx
);
3771 int prepare_write_cryptcompress(struct file
*file
, struct page
*page
,
3772 unsigned from
, unsigned to
)
3774 return prepare_write_common(file
, page
, from
, to
);
3781 c-indentation-style: "K&R"