<commit_before>/* $Id$ */
/*
 * %PSC_START_COPYRIGHT%
 * -----------------------------------------------------------------------------
 * Copyright (c) 2006-2010, Pittsburgh Supercomputing Center (PSC).
 *
 * Permission to use, copy, and modify this software and its documentation
 * without fee for personal use or non-commercial use within your organization
 * is hereby granted, provided that the above copyright notice is preserved in
 * all copies and that the copyright and this permission notice appear in
 * supporting documentation.  Permission to redistribute this software to other
 * organizations or individuals is not permitted without the written permission
 * of the Pittsburgh Supercomputing Center.  PSC makes no representations about
 * the suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 * -----------------------------------------------------------------------------
 * %PSC_END_COPYRIGHT%
 */

#include "dircache.h"
#include "fidcache.h"
#include "sltypes.h"

/* Note:  These macros must match the ones used by the server in
 *  zfs_operations_slash.c
 */
#define SRT_NAME_OFFSET ((unsigned long)((struct srt_dirent *) 0)->name)
#define SRT_DIRENT_ALIGN(x) (((x) + sizeof(uint64_t) - 1) &	\
			     ~(sizeof(uint64_t) - 1))
#define SRT_DIRENT_SIZE(d)					\
	SRT_DIRENT_ALIGN(SRT_NAME_OFFSET + (d)->namelen)

static __inline size_t
srt_dirent_size(size_t namelen)
{
	return SRT_DIRENT_ALIGN(SRT_NAME_OFFSET + namelen);
}

void
dircache_init(struct dircache_mgr *m, const char *name, size_t maxsz)
{
	m->dcm_maxsz = maxsz;
	m->dcm_alloc = 0;

	LOCK_INIT(&m->dcm_lock);
	lc_reginit(&m->dcm_lc, struct dircache_ents, de_lentry2, name);
}

void
dircache_init_info(struct dircache_info *i, struct fidc_membh *f,
	   struct dircache_mgr *m)
{
	i->di_dcm = m;
	i->di_fcmh = f;
	INIT_PSCLIST_HEAD(&i->di_list);
	LOCK_INIT(&i->di_lock);
}

static void
dircache_rls_ents(struct dircache_ents *e)
{
	struct dircache_info *i = e->de_info;
	struct dircache_mgr *m = i->di_dcm;
	int locked;

	DEBUG_FCMH(PLL_DEBUG, i->di_fcmh,
		   "rls dircache_ents %p cachesz=%"PRId64, e, m->dcm_alloc);

	locked = reqlock(&m->dcm_lock);
	m->dcm_alloc -= e->de_sz;
	lc_remove(&i->di_dcm->dcm_lc, e);

	spinlock(&i->di_lock);
	psclist_del(&e->de_lentry1);
	freelock(&i->di_lock);

	ureqlock(&m->dcm_lock, locked);

	PSCFREE(e);

	fcmh_op_done_type(i->di_fcmh, FCMH_OPCNT_DIRENTBUF);
}

void
dircache_setfreeable_ents(struct dircache_ents *e)
{
	e->de_freeable = 1;
	if (!e->de_remlookup) 
		dircache_rls_ents(e);	
}


slfid_t
dircache_lookup(struct dircache_info *i, const char *name, int flag)
{
	struct dircache_ents *e;
	struct dircache_desc desc, *d;
	struct srt_dirent *dirent;
	slfid_t ino = FID_ANY;
	int found=0, pos;

	desc.dd_hash = psc_str_hashify(name);
	desc.dd_len  = strnlen(name, NAME_MAX);
	desc.dd_name = name;

	spinlock(&i->di_lock);
	psclist_for_each_entry(e, &i->di_list, de_lentry1) {
		/* The return code for psc_dynarray_bsearch() isn't quite
		 *    right for our purposes but either way the strings
		 *    must still be compared.
		 */
		pos = psc_dynarray_bsearch(&e->de_dents,
		    &desc, dirent_cmp);
		if (pos >= psc_dynarray_len(&e->de_dents))
			break;<commit_msg>space<commit_after>/* $Id$ */
/*
 * %PSC_START_COPYRIGHT%
 * -----------------------------------------------------------------------------
 * Copyright (c) 2006-2010, Pittsburgh Supercomputing Center (PSC).
 *
 * Permission to use, copy, and modify this software and its documentation
 * without fee for personal use or non-commercial use within your organization
 * is hereby granted, provided that the above copyright notice is preserved in
 * all copies and that the copyright and this permission notice appear in
 * supporting documentation.  Permission to redistribute this software to other
 * organizations or individuals is not permitted without the written permission
 * of the Pittsburgh Supercomputing Center.  PSC makes no representations about
 * the suitability of this software for any purpose.  It is provided "as is"
 * without express or implied warranty.
 * -----------------------------------------------------------------------------
 * %PSC_END_COPYRIGHT%
 */

#include "dircache.h"
#include "fidcache.h"
#include "sltypes.h"

/* Note:  These macros must match the ones used by the server in
 *  zfs_operations_slash.c
 */
#define SRT_NAME_OFFSET ((unsigned long)((struct srt_dirent *) 0)->name)
#define SRT_DIRENT_ALIGN(x) (((x) + sizeof(uint64_t) - 1) &	\
			     ~(sizeof(uint64_t) - 1))
#define SRT_DIRENT_SIZE(d)					\
	SRT_DIRENT_ALIGN(SRT_NAME_OFFSET + (d)->namelen)

static __inline size_t
srt_dirent_size(size_t namelen)
{
	return SRT_DIRENT_ALIGN(SRT_NAME_OFFSET + namelen);
}

void
dircache_init(struct dircache_mgr *m, const char *name, size_t maxsz)
{
	m->dcm_maxsz = maxsz;
	m->dcm_alloc = 0;

	LOCK_INIT(&m->dcm_lock);
	lc_reginit(&m->dcm_lc, struct dircache_ents, de_lentry2, name);
}

void
dircache_init_info(struct dircache_info *i, struct fidc_membh *f,
	   struct dircache_mgr *m)
{
	i->di_dcm = m;
	i->di_fcmh = f;
	INIT_PSCLIST_HEAD(&i->di_list);
	LOCK_INIT(&i->di_lock);
}

static void
dircache_rls_ents(struct dircache_ents *e)
{
	struct dircache_info *i = e->de_info;
	struct dircache_mgr *m = i->di_dcm;
	int locked;

	DEBUG_FCMH(PLL_DEBUG, i->di_fcmh,
		   "rls dircache_ents %p cachesz=%"PRId64, e, m->dcm_alloc);

	locked = reqlock(&m->dcm_lock);
	m->dcm_alloc -= e->de_sz;
	lc_remove(&i->di_dcm->dcm_lc, e);

	spinlock(&i->di_lock);
	psclist_del(&e->de_lentry1);
	freelock(&i->di_lock);

	ureqlock(&m->dcm_lock, locked);

	PSCFREE(e);

	fcmh_op_done_type(i->di_fcmh, FCMH_OPCNT_DIRENTBUF);
}

void
dircache_setfreeable_ents(struct dircache_ents *e)
{
	e->de_freeable = 1;
	if (!e->de_remlookup)
		dircache_rls_ents(e);
}

slfid_t
dircache_lookup(struct dircache_info *i, const char *name, int flag)
{
	struct dircache_ents *e;
	struct dircache_desc desc, *d;
	struct srt_dirent *dirent;
	slfid_t ino = FID_ANY;
	int found=0, pos;

	desc.dd_hash = psc_str_hashify(name);
	desc.dd_len  = strnlen(name, NAME_MAX);
	desc.dd_name = name;

	spinlock(&i->di_lock);
	psclist_for_each_entry(e, &i->di_list, de_lentry1) {
		/* The return code for psc_dynarray_bsearch() isn't quite
		 *    right for our purposes but either way the strings
		 *    must still be compared.
		 */
		pos = psc_dynarray_bsearch(&e->de_dents,
		    &desc, dirent_cmp);
		if (pos >= psc_dynarray_len(&e->de_dents))
			break;
{'fraction_non_alphanumeric': 0.15915037709712174, 'fraction_numerical': 0.007080190857318762, 'mean_word_length': 3.654727793696275, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '61736', 'n_tokens_mistral': 2484, 'n_tokens_neox': 2412, 'n_words': 585}